Skip to content

Commit

Permalink
Merge pull request #153 from Leonidas-from-XIV/ocamlformat-enable
Browse files Browse the repository at this point in the history
Enable `ocamlformat` on the part of the codebase that doesn't use CPPO
  • Loading branch information
Leonidas-from-XIV authored Aug 22, 2022
2 parents 37e0014 + 0237393 commit a3fbc94
Show file tree
Hide file tree
Showing 27 changed files with 450 additions and 646 deletions.
2 changes: 2 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Introduced ocamlformat
3101ec295fbc791a1e9384f50178846198a607c7
2 changes: 2 additions & 0 deletions .ocamlformat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
version=0.24.1
profile=conventional
6 changes: 6 additions & 0 deletions .ocamlformat-ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# disable ocamlformat on files that are processed by CPPO
lib/*.cppo.ml*
lib/monomorphic.ml
lib/write.ml*
lib/pretty.ml
lib/type.ml
96 changes: 43 additions & 53 deletions bench/bench.ml
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
open Core
open Core_bench

let data =
In_channel.read_all "bench.json"

let data = In_channel.read_all "bench.json"
let yojson_data = Yojson.Safe.from_string data

(* chosen by fair dice roll, guaranteed to be large *)
let large = 10_000

let large_int_assoc =
let ints = List.init large ~f:(fun n ->
(string_of_int n, `Int n))
in
let large_int_assoc =
let ints = List.init large ~f:(fun n -> (string_of_int n, `Int n)) in
`Assoc ints

let large_int_list =
let large_int_list =
let ints = List.init large ~f:(fun n -> `Int n) in
`List ints

let large_string_list =
let strings = List.init large ~f:(fun n ->
`String (string_of_int n))
in
let strings = List.init large ~f:(fun n -> `String (string_of_int n)) in
`List strings

let streamable_string =
Expand All @@ -33,53 +27,49 @@ let streamable_string =
Buffer.contents buf

let generic =
Bench.make_command [
Bench.Test.create ~name:"JSON reading" (fun () ->
ignore (Yojson.Safe.from_string data));
Bench.Test.create ~name:"JSON writing" (fun () ->
ignore (Yojson.Safe.to_string yojson_data));
Bench.Test.create ~name:"JSON writing assoc" (fun () ->
ignore (Yojson.Safe.to_string large_int_assoc));
Bench.Test.create ~name:"JSON writing int list" (fun () ->
ignore (Yojson.Safe.to_string large_int_list));
Bench.Test.create ~name:"JSON writing string list" (fun () ->
ignore (Yojson.Safe.to_string large_string_list));
Bench.Test.create ~name:"JSON writing int list to channel" (fun () ->
Out_channel.with_file "/dev/null" ~f:(fun oc ->
ignore (Yojson.Safe.to_channel oc large_int_list)));
Bench.Test.create ~name:"JSON writing string list to channel" (fun () ->
Out_channel.with_file "/dev/null" ~f:(fun oc ->
ignore (Yojson.Safe.to_channel oc large_string_list)));
Bench.Test.create ~name:"JSON writing assoc to channel" (fun () ->
Out_channel.with_file "/dev/null" ~f:(fun oc ->
ignore (Yojson.Safe.to_channel oc large_int_assoc)));
begin
let buf = Buffer.create 1000 in
Bench.Test.create ~name:"JSON seq roundtrip" (fun () ->
let stream = Yojson.Safe.seq_from_string ~buf streamable_string in
ignore (Yojson.Safe.seq_to_string ~buf stream)
)
end;
]
Bench.make_command
[
Bench.Test.create ~name:"JSON reading" (fun () ->
ignore (Yojson.Safe.from_string data));
Bench.Test.create ~name:"JSON writing" (fun () ->
ignore (Yojson.Safe.to_string yojson_data));
Bench.Test.create ~name:"JSON writing assoc" (fun () ->
ignore (Yojson.Safe.to_string large_int_assoc));
Bench.Test.create ~name:"JSON writing int list" (fun () ->
ignore (Yojson.Safe.to_string large_int_list));
Bench.Test.create ~name:"JSON writing string list" (fun () ->
ignore (Yojson.Safe.to_string large_string_list));
Bench.Test.create ~name:"JSON writing int list to channel" (fun () ->
Out_channel.with_file "/dev/null" ~f:(fun oc ->
ignore (Yojson.Safe.to_channel oc large_int_list)));
Bench.Test.create ~name:"JSON writing string list to channel" (fun () ->
Out_channel.with_file "/dev/null" ~f:(fun oc ->
ignore (Yojson.Safe.to_channel oc large_string_list)));
Bench.Test.create ~name:"JSON writing assoc to channel" (fun () ->
Out_channel.with_file "/dev/null" ~f:(fun oc ->
ignore (Yojson.Safe.to_channel oc large_int_assoc)));
(let buf = Buffer.create 1000 in
Bench.Test.create ~name:"JSON seq roundtrip" (fun () ->
let stream = Yojson.Safe.seq_from_string ~buf streamable_string in
ignore (Yojson.Safe.seq_to_string ~buf stream)));
]

let buffer =
let buf = Buffer.create 4096 in
let data = large_int_assoc in
Bench.make_command [
Bench.Test.create ~name:"JSON writing with internal buffer" (fun () ->
Out_channel.with_file "/dev/null" ~f:(fun oc ->
ignore (Yojson.Safe.to_channel oc data)));
Bench.Test.create ~name:"JSON writing with provided buffer" (fun () ->
Out_channel.with_file "/dev/null" ~f:(fun oc ->
ignore (Yojson.Safe.to_channel ~buf oc data)));
]
Bench.make_command
[
Bench.Test.create ~name:"JSON writing with internal buffer" (fun () ->
Out_channel.with_file "/dev/null" ~f:(fun oc ->
ignore (Yojson.Safe.to_channel oc data)));
Bench.Test.create ~name:"JSON writing with provided buffer" (fun () ->
Out_channel.with_file "/dev/null" ~f:(fun oc ->
ignore (Yojson.Safe.to_channel ~buf oc data)));
]

let main () =
Command.group ~summary:"Benchmark" [
("generic", generic);
("buffer", buffer)
]
Command.group ~summary:"Benchmark"
[ ("generic", generic); ("buffer", buffer) ]
|> Command_unix.run

let () =
main ()
let () = main ()
Loading

0 comments on commit a3fbc94

Please sign in to comment.