Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit 67fec53

Browse files
authored
Format sources with ocamlformat and enforce formatting in CI (#565)
* Enforce formatting in CI * Reformat sources.
1 parent 71e62b4 commit 67fec53

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+9300
-8895
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ jobs:
5050
- name: Install dependencies
5151
run: opam install . --deps-only
5252

53+
- name: Check format
54+
run: opam exec -- dune build @fmt
55+
5356
- name: Build executables
5457
run: opam exec -- dune build
5558

benchmarks/Benchmark.ml

Lines changed: 103 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ module CommentTable = Res_comments_table
44
module Parser = Res_parser
55
module Printer = Res_printer
66

7-
module IO: sig
8-
val readFile: string -> string
7+
module IO : sig
8+
val readFile : string -> string
99
end = struct
1010
(* random chunk size: 2^15, TODO: why do we guess randomly? *)
1111
let chunkSize = 32768
@@ -15,39 +15,40 @@ end = struct
1515
let buffer = Buffer.create chunkSize in
1616
let chunk = (Bytes.create [@doesNotRaise]) chunkSize in
1717
let rec loop () =
18-
let len = try input chan chunk 0 chunkSize with Invalid_argument _ -> 0 in
18+
let len =
19+
try input chan chunk 0 chunkSize with Invalid_argument _ -> 0
20+
in
1921
if len == 0 then (
2022
close_in_noerr chan;
21-
Buffer.contents buffer
22-
) else (
23+
Buffer.contents buffer)
24+
else (
2325
Buffer.add_subbytes buffer chunk 0 len;
24-
loop ()
25-
)
26+
loop ())
2627
in
2728
loop ()
2829
end
2930

30-
module Time: sig
31+
module Time : sig
3132
type t
3233

33-
val now: unit -> t
34+
val now : unit -> t
3435

35-
val toUint64: t -> int64 [@@live]
36+
val toUint64 : t -> int64 [@@live]
3637

3738
(* let of_uint64_ns ns = ns *)
3839

39-
val nanosecond: t [@@live]
40-
val microsecond: t [@@live]
41-
val millisecond: t [@@live]
42-
val second: t [@@live]
43-
val minute: t [@@live]
44-
val hour: t [@@live]
40+
val nanosecond : t [@@live]
41+
val microsecond : t [@@live]
42+
val millisecond : t [@@live]
43+
val second : t [@@live]
44+
val minute : t [@@live]
45+
val hour : t [@@live]
4546

46-
val zero: t
47+
val zero : t
4748

48-
val diff: t -> t -> t
49-
val add: t -> t -> t
50-
val print: t -> float
49+
val diff : t -> t -> t
50+
val add : t -> t -> t
51+
val print : t -> float
5152
end = struct
5253
(* nanoseconds *)
5354
type t = int64
@@ -64,22 +65,21 @@ end = struct
6465
let hour = Int64.mul 60L minute
6566

6667
(* TODO: we could do this inside caml_absolute_time *)
67-
external init: unit -> unit = "caml_mach_initialize"
68-
let () = init()
69-
external now: unit -> t = "caml_mach_absolute_time"
68+
external init : unit -> unit = "caml_mach_initialize"
69+
let () = init ()
70+
external now : unit -> t = "caml_mach_absolute_time"
7071

7172
let diff t1 t2 = Int64.sub t2 t1
7273
let add t1 t2 = Int64.add t1 t2
73-
let print t =
74-
(Int64.to_float t) *. 1e-6
74+
let print t = Int64.to_float t *. 1e-6
7575
end
7676

77-
module Benchmark: sig
77+
module Benchmark : sig
7878
type t
7979

80-
val make: name:string -> f:(t -> unit) -> unit -> t
81-
val launch: t -> unit
82-
val report: t -> unit
80+
val make : name:string -> f:(t -> unit) -> unit -> t
81+
val launch : t -> unit
82+
val report : t -> unit
8383
end = struct
8484
type t = {
8585
name: string;
@@ -89,7 +89,7 @@ end = struct
8989
benchFunc: t -> unit;
9090
mutable timerOn: bool;
9191
(* mutable result: benchmarkResult; *)
92-
(* The initial states *)
92+
(* The initial states *)
9393
mutable startAllocs: float;
9494
mutable startBytes: float;
9595
(* The net total of this test after being run. *)
@@ -100,65 +100,69 @@ end = struct
100100
let report b =
101101
print_endline (Format.sprintf "Benchmark: %s" b.name);
102102
print_endline (Format.sprintf "Nbr of iterations: %d" b.n);
103-
print_endline (Format.sprintf "Benchmark ran during: %fms" (Time.print b.duration));
104-
print_endline (Format.sprintf "Avg time/op: %fms" ((Time.print b.duration) /. (float_of_int b.n)));
105-
print_endline (Format.sprintf "Allocs/op: %d" (int_of_float (b.netAllocs /. (float_of_int b.n))));
106-
print_endline (Format.sprintf "B/op: %d" (int_of_float (b.netBytes /. (float_of_int b.n))));
107-
(* return (float64(r.Bytes) * float64(r.N) / 1e6) / r.T.Seconds() *)
108-
103+
print_endline
104+
(Format.sprintf "Benchmark ran during: %fms" (Time.print b.duration));
105+
print_endline
106+
(Format.sprintf "Avg time/op: %fms"
107+
(Time.print b.duration /. float_of_int b.n));
108+
print_endline
109+
(Format.sprintf "Allocs/op: %d"
110+
(int_of_float (b.netAllocs /. float_of_int b.n)));
111+
print_endline
112+
(Format.sprintf "B/op: %d"
113+
(int_of_float (b.netBytes /. float_of_int b.n)));
109114

110-
print_newline();
115+
(* return (float64(r.Bytes) * float64(r.N) / 1e6) / r.T.Seconds() *)
116+
print_newline ();
111117
()
112118

113-
let make ~name ~f () = {
114-
name;
115-
start = Time.zero;
116-
n = 0;
117-
benchFunc = f;
118-
duration = Time.zero;
119-
timerOn = false;
120-
startAllocs = 0.;
121-
startBytes = 0.;
122-
netAllocs = 0.;
123-
netBytes = 0.;
124-
}
119+
let make ~name ~f () =
120+
{
121+
name;
122+
start = Time.zero;
123+
n = 0;
124+
benchFunc = f;
125+
duration = Time.zero;
126+
timerOn = false;
127+
startAllocs = 0.;
128+
startBytes = 0.;
129+
netAllocs = 0.;
130+
netBytes = 0.;
131+
}
125132

126133
(* total amount of memory allocated by the program since it started in words *)
127134
let mallocs () =
128-
let stats = Gc.quick_stat() in
135+
let stats = Gc.quick_stat () in
129136
stats.minor_words +. stats.major_words -. stats.promoted_words
130137

131138
let startTimer b =
132139
if not b.timerOn then (
133-
let allocatedWords = mallocs() in
140+
let allocatedWords = mallocs () in
134141
b.startAllocs <- allocatedWords;
135142
b.startBytes <- allocatedWords *. 8.;
136-
b.start <- Time.now();
137-
b.timerOn <- true
138-
)
143+
b.start <- Time.now ();
144+
b.timerOn <- true)
139145

140146
let stopTimer b =
141147
if b.timerOn then (
142-
let allocatedWords = mallocs() in
143-
let diff = (Time.diff b.start (Time.now())) in
148+
let allocatedWords = mallocs () in
149+
let diff = Time.diff b.start (Time.now ()) in
144150
b.duration <- Time.add b.duration diff;
145151
b.netAllocs <- b.netAllocs +. (allocatedWords -. b.startAllocs);
146-
b.netBytes <- b.netBytes +. (allocatedWords *. 8. -. b.startBytes);
147-
b.timerOn <- false
148-
)
152+
b.netBytes <- b.netBytes +. ((allocatedWords *. 8.) -. b.startBytes);
153+
b.timerOn <- false)
149154

150155
let resetTimer b =
151156
if b.timerOn then (
152-
let allocatedWords = mallocs() in
157+
let allocatedWords = mallocs () in
153158
b.startAllocs <- allocatedWords;
154159
b.netAllocs <- allocatedWords *. 8.;
155-
b.start <- Time.now();
156-
);
160+
b.start <- Time.now ());
157161
b.netAllocs <- 0.;
158162
b.netBytes <- 0.
159163

160164
let runIteration b n =
161-
Gc.full_major();
165+
Gc.full_major ();
162166
b.n <- n;
163167
resetTimer b;
164168
startTimer b;
@@ -167,22 +171,24 @@ end = struct
167171

168172
let launch b =
169173
(* 150 runs * all the benchmarks means around 1m of benchmark time *)
170-
for n=1 to 150 do
174+
for n = 1 to 150 do
171175
runIteration b n
172176
done
173177
end
174178

175-
module Benchmarks: sig
176-
val run: unit -> unit
179+
module Benchmarks : sig
180+
val run : unit -> unit
177181
end = struct
178182
type action = Parse | Print
179-
let string_of_action action = match action with
180-
| Parse -> "parser"
181-
| Print -> "printer"
183+
let string_of_action action =
184+
match action with
185+
| Parse -> "parser"
186+
| Print -> "printer"
182187

183188
(* TODO: we could at Reason here *)
184189
type lang = Ocaml | Rescript
185-
let string_of_lang lang = match lang with
190+
let string_of_lang lang =
191+
match lang with
186192
| Ocaml -> "ocaml"
187193
| Rescript -> "rescript"
188194

@@ -194,33 +200,37 @@ end = struct
194200
let parseRescript src filename =
195201
let p = Parser.make src filename in
196202
let structure = ResParser.parseImplementation p in
197-
assert(p.diagnostics == []);
203+
assert (p.diagnostics == []);
198204
structure
199205

200206
let benchmark filename lang action =
201207
let src = IO.readFile filename in
202208
let name =
203-
filename ^ " " ^ (string_of_lang lang) ^ " " ^ (string_of_action action)
209+
filename ^ " " ^ string_of_lang lang ^ " " ^ string_of_action action
204210
in
205-
let benchmarkFn = match (lang, action) with
206-
| (Rescript, Parse) -> (fun _ ->
207-
let _ = Sys.opaque_identity (parseRescript src filename) in ()
208-
)
209-
| (Ocaml, Parse) -> (fun _ ->
210-
let _ = Sys.opaque_identity (parseOcaml src filename) in ()
211-
)
212-
| (Rescript, Print) ->
213-
let p = Parser.make src filename in
214-
let ast = ResParser.parseImplementation p in
215-
(fun _ ->
216-
let _ = Sys.opaque_identity (
217-
let cmtTbl = CommentTable.make () in
218-
let comments = List.rev p.Parser.comments in
219-
let () = CommentTable.walkStructure ast cmtTbl comments in
220-
Doc.toString ~width:80 (Printer.printStructure ast cmtTbl)
221-
) in ()
222-
)
223-
| _ -> (fun _ -> ())
211+
let benchmarkFn =
212+
match (lang, action) with
213+
| Rescript, Parse ->
214+
fun _ ->
215+
let _ = Sys.opaque_identity (parseRescript src filename) in
216+
()
217+
| Ocaml, Parse ->
218+
fun _ ->
219+
let _ = Sys.opaque_identity (parseOcaml src filename) in
220+
()
221+
| Rescript, Print ->
222+
let p = Parser.make src filename in
223+
let ast = ResParser.parseImplementation p in
224+
fun _ ->
225+
let _ =
226+
Sys.opaque_identity
227+
(let cmtTbl = CommentTable.make () in
228+
let comments = List.rev p.Parser.comments in
229+
let () = CommentTable.walkStructure ast cmtTbl comments in
230+
Doc.toString ~width:80 (Printer.printStructure ast cmtTbl))
231+
in
232+
()
233+
| _ -> fun _ -> ()
224234
in
225235
let b = Benchmark.make ~name ~f:benchmarkFn () in
226236
Benchmark.launch b;
@@ -236,7 +246,7 @@ end = struct
236246
benchmark "./benchmarks/data/Napkinscript.res" Rescript Print;
237247
benchmark "./benchmarks/data/HeroGraphic.res" Rescript Parse;
238248
benchmark "./benchmarks/data/HeroGraphic.ml" Ocaml Parse;
239-
benchmark "./benchmarks/data/HeroGraphic.res" Rescript Print;
249+
benchmark "./benchmarks/data/HeroGraphic.res" Rescript Print
240250
end
241251

242-
let () = Benchmarks.run()
252+
let () = Benchmarks.run ()

0 commit comments

Comments
 (0)