@@ -4,8 +4,8 @@ module CommentTable = Res_comments_table
4
4
module Parser = Res_parser
5
5
module Printer = Res_printer
6
6
7
- module IO : sig
8
- val readFile : string -> string
7
+ module IO : sig
8
+ val readFile : string -> string
9
9
end = struct
10
10
(* random chunk size: 2^15, TODO: why do we guess randomly? *)
11
11
let chunkSize = 32768
@@ -15,39 +15,40 @@ end = struct
15
15
let buffer = Buffer. create chunkSize in
16
16
let chunk = (Bytes. create [@ doesNotRaise]) chunkSize in
17
17
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
19
21
if len == 0 then (
20
22
close_in_noerr chan;
21
- Buffer. contents buffer
22
- ) else (
23
+ Buffer. contents buffer)
24
+ else (
23
25
Buffer. add_subbytes buffer chunk 0 len;
24
- loop ()
25
- )
26
+ loop () )
26
27
in
27
28
loop ()
28
29
end
29
30
30
- module Time : sig
31
+ module Time : sig
31
32
type t
32
33
33
- val now : unit -> t
34
+ val now : unit -> t
34
35
35
- val toUint64 : t -> int64 [@@ live]
36
+ val toUint64 : t -> int64 [@@ live]
36
37
37
38
(* let of_uint64_ns ns = ns *)
38
39
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]
45
46
46
- val zero : t
47
+ val zero : t
47
48
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
51
52
end = struct
52
53
(* nanoseconds *)
53
54
type t = int64
@@ -64,22 +65,21 @@ end = struct
64
65
let hour = Int64. mul 60L minute
65
66
66
67
(* 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"
70
71
71
72
let diff t1 t2 = Int64. sub t2 t1
72
73
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
75
75
end
76
76
77
- module Benchmark : sig
77
+ module Benchmark : sig
78
78
type t
79
79
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
83
83
end = struct
84
84
type t = {
85
85
name : string ;
@@ -89,7 +89,7 @@ end = struct
89
89
benchFunc : t -> unit ;
90
90
mutable timerOn : bool ;
91
91
(* mutable result: benchmarkResult; *)
92
- (* The initial states *)
92
+ (* The initial states *)
93
93
mutable startAllocs : float ;
94
94
mutable startBytes : float ;
95
95
(* The net total of this test after being run. *)
@@ -100,65 +100,69 @@ end = struct
100
100
let report b =
101
101
print_endline (Format. sprintf " Benchmark: %s" b.name);
102
102
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)));
109
114
110
- print_newline() ;
115
+ (* return (float64(r.Bytes) * float64(r.N) / 1e6) / r.T.Seconds() *)
116
+ print_newline () ;
111
117
()
112
118
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
+ }
125
132
126
133
(* total amount of memory allocated by the program since it started in words *)
127
134
let mallocs () =
128
- let stats = Gc. quick_stat() in
135
+ let stats = Gc. quick_stat () in
129
136
stats.minor_words +. stats.major_words -. stats.promoted_words
130
137
131
138
let startTimer b =
132
139
if not b.timerOn then (
133
- let allocatedWords = mallocs() in
140
+ let allocatedWords = mallocs () in
134
141
b.startAllocs < - allocatedWords;
135
142
b.startBytes < - allocatedWords *. 8. ;
136
- b.start < - Time. now() ;
137
- b.timerOn < - true
138
- )
143
+ b.start < - Time. now () ;
144
+ b.timerOn < - true )
139
145
140
146
let stopTimer b =
141
147
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
144
150
b.duration < - Time. add b.duration diff;
145
151
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 )
149
154
150
155
let resetTimer b =
151
156
if b.timerOn then (
152
- let allocatedWords = mallocs() in
157
+ let allocatedWords = mallocs () in
153
158
b.startAllocs < - allocatedWords;
154
159
b.netAllocs < - allocatedWords *. 8. ;
155
- b.start < - Time. now() ;
156
- );
160
+ b.start < - Time. now () );
157
161
b.netAllocs < - 0. ;
158
162
b.netBytes < - 0.
159
163
160
164
let runIteration b n =
161
- Gc. full_major() ;
165
+ Gc. full_major () ;
162
166
b.n < - n;
163
167
resetTimer b;
164
168
startTimer b;
@@ -167,22 +171,24 @@ end = struct
167
171
168
172
let launch b =
169
173
(* 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
171
175
runIteration b n
172
176
done
173
177
end
174
178
175
- module Benchmarks : sig
176
- val run : unit -> unit
179
+ module Benchmarks : sig
180
+ val run : unit -> unit
177
181
end = struct
178
182
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"
182
187
183
188
(* TODO: we could at Reason here *)
184
189
type lang = Ocaml | Rescript
185
- let string_of_lang lang = match lang with
190
+ let string_of_lang lang =
191
+ match lang with
186
192
| Ocaml -> " ocaml"
187
193
| Rescript -> " rescript"
188
194
@@ -194,33 +200,37 @@ end = struct
194
200
let parseRescript src filename =
195
201
let p = Parser. make src filename in
196
202
let structure = ResParser. parseImplementation p in
197
- assert (p.diagnostics == [] );
203
+ assert (p.diagnostics == [] );
198
204
structure
199
205
200
206
let benchmark filename lang action =
201
207
let src = IO. readFile filename in
202
208
let name =
203
- filename ^ " " ^ ( string_of_lang lang) ^ " " ^ ( string_of_action action)
209
+ filename ^ " " ^ string_of_lang lang ^ " " ^ string_of_action action
204
210
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 _ -> ()
224
234
in
225
235
let b = Benchmark. make ~name ~f: benchmarkFn () in
226
236
Benchmark. launch b;
@@ -236,7 +246,7 @@ end = struct
236
246
benchmark " ./benchmarks/data/Napkinscript.res" Rescript Print ;
237
247
benchmark " ./benchmarks/data/HeroGraphic.res" Rescript Parse ;
238
248
benchmark " ./benchmarks/data/HeroGraphic.ml" Ocaml Parse ;
239
- benchmark " ./benchmarks/data/HeroGraphic.res" Rescript Print ;
249
+ benchmark " ./benchmarks/data/HeroGraphic.res" Rescript Print
240
250
end
241
251
242
- let () = Benchmarks. run()
252
+ let () = Benchmarks. run ()
0 commit comments