Skip to content

Commit 3ce2727

Browse files
committed
Compiler: more timing in wasmoo
1 parent 11bbfd9 commit 3ce2727

File tree

6 files changed

+33
-19
lines changed

6 files changed

+33
-19
lines changed

compiler/bin-wasm_of_ocaml/compile.ml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,19 +164,23 @@ let link_and_optimize
164164
@@ fun opt_temp_sourcemap ->
165165
(with_runtime_files ~runtime_wasm_files
166166
@@ fun runtime_inputs ->
167+
let t = Timer.make ~get_time:Unix.time () in
167168
Binaryen.link
168169
~inputs:
169170
(({ Binaryen.module_name = "env"; file = runtime_file } :: runtime_inputs)
170171
@ List.map ~f:(fun file -> { Binaryen.module_name = "OCaml"; file }) wat_files)
171172
~opt_output_sourcemap:opt_temp_sourcemap
172173
~output_file:temp_file
173-
());
174+
();
175+
if times () then Format.eprintf " binaryen link: %a@." Timer.print t);
176+
174177
Fs.with_intermediate_file (Filename.temp_file "wasm-dce" ".wasm")
175178
@@ fun temp_file' ->
176179
opt_with
177180
Fs.with_intermediate_file
178181
(if enable_source_maps then Some (Filename.temp_file "wasm-dce" ".wasm.map") else None)
179182
@@ fun opt_temp_sourcemap' ->
183+
let t = Timer.make ~get_time:Unix.time () in
180184
let primitives =
181185
Binaryen.dead_code_elimination
182186
~dependencies:Runtime_files.dependencies
@@ -185,13 +189,16 @@ let link_and_optimize
185189
~input_file:temp_file
186190
~output_file:temp_file'
187191
in
192+
if times () then Format.eprintf " binaryen dce: %a@." Timer.print t;
193+
let t = Timer.make ~get_time:Unix.time () in
188194
Binaryen.optimize
189195
~profile
190196
~opt_input_sourcemap:opt_temp_sourcemap'
191197
~opt_output_sourcemap:opt_sourcemap
192198
~input_file:temp_file'
193199
~output_file
194200
();
201+
if times () then Format.eprintf " binaryen opt: %a@." Timer.print t;
195202
Option.iter
196203
~f:(update_sourcemap ~sourcemap_root ~sourcemap_don't_inline_content)
197204
opt_sourcemap_file;
@@ -523,6 +530,7 @@ let run
523530
in
524531
let generated_js = output_gen wat_file (output code ~unit_name:None) in
525532
let tmp_wasm_file = Filename.concat tmp_dir "code.wasm" in
533+
let t2 = Timer.make ~get_time:Unix.time () in
526534
let primitives =
527535
link_and_optimize
528536
~profile
@@ -533,6 +541,7 @@ let run
533541
[ wat_file ]
534542
tmp_wasm_file
535543
in
544+
if times () then Format.eprintf " link_and_optimize: %a@." Timer.print t2;
536545
let wasm_name =
537546
Printf.sprintf
538547
"code-%s"
@@ -546,6 +555,7 @@ let run
546555
Link.Wasm_binary.append_source_map_section
547556
~file:tmp_wasm_file'
548557
~url:(wasm_name ^ ".wasm.map"));
558+
if times () then Format.eprintf "Start building js runtime@.";
549559
let js_runtime =
550560
let missing_primitives =
551561
let l = Link.Wasm_binary.read_imports ~file:tmp_wasm_file' in

compiler/bin-wasm_of_ocaml/dune

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
compiler-libs.common
1010
js_of_ocaml-compiler.runtime-files
1111
yojson
12+
unix
1213
(select
1314
findlib_support.ml
1415
from

compiler/lib-wasm/generate.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,11 +1318,13 @@ let add_init_function = G.add_init_function
13181318
let output ch ~context =
13191319
let t = Timer.make () in
13201320
let fields = G.output ~context in
1321+
if times () then Format.eprintf " fields: %a@." Timer.print t;
13211322
Wat_output.f ch fields;
1322-
if times () then Format.eprintf " output: %a@." Timer.print t
1323+
if times () then Format.eprintf " output-wat: %a@." Timer.print t
13231324

13241325
let wasm_output ch ~context =
13251326
let t = Timer.make () in
13261327
let fields = G.output ~context in
1328+
if times () then Format.eprintf " fields: %a@." Timer.print t;
13271329
Wasm_output.f ch fields;
1328-
if times () then Format.eprintf " output: %a@." Timer.print t
1330+
if times () then Format.eprintf " output-wasm: %a@." Timer.print t

compiler/lib-wasm/wat_output.ml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -666,10 +666,13 @@ let field ctx st f =
666666
| Type [ t ] -> [ type_field st t ]
667667
| Type l -> [ List (Atom "rec" :: List.map ~f:(type_field st) l) ]
668668

669+
let times = Debug.find "times"
670+
669671
let f ch fields =
672+
let t = Timer.make () in
670673
let st = build_name_tables fields in
671674
let ctx = { function_refs = Code.Var.Set.empty } in
672-
let other_fields = List.concat (List.map ~f:(fun f -> field ctx st f) fields) in
675+
let other_fields = List.concat_map ~f:(fun f -> field ctx st f) fields in
673676
let funct_decl =
674677
let functions = Code.Var.Set.elements ctx.function_refs in
675678
if List.is_empty functions
@@ -682,12 +685,9 @@ let f ch fields =
682685
:: List.map ~f:(index st.func_names) functions)
683686
]
684687
in
685-
Format.fprintf
686-
(Format.formatter_of_out_channel ch)
687-
"%a@."
688-
format_sexp
689-
(List
690-
(Atom "module"
691-
:: (List.concat (List.map ~f:(fun i -> import st i) fields)
692-
@ funct_decl
693-
@ other_fields)))
688+
let imports = List.concat_map ~f:(fun i -> import st i) fields in
689+
let sexp = List (Atom "module" :: List.concat [ imports; funct_decl; other_fields ]) in
690+
if times () then Format.eprintf " prepare: %a@." Timer.print t;
691+
let t = Timer.make () in
692+
Format.fprintf (Format.formatter_of_out_channel ch) "%a@." format_sexp sexp;
693+
if times () then Format.eprintf " format: %a@." Timer.print t

compiler/lib/timer.ml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@
1818

1919
open! Stdlib
2020

21-
type t = float
21+
type t =
22+
{ get_time : unit -> float
23+
; start : float
24+
}
2225

23-
let timer = Sys.time
26+
let make ?(get_time = Sys.time) () = { get_time; start = get_time () }
2427

25-
let make () = timer ()
26-
27-
let get t = timer () -. t
28+
let get t = t.get_time () -. t.start
2829

2930
let print f t = Format.fprintf f "%.2f" (get t)

compiler/lib/timer.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
type t
2020

21-
val make : unit -> t
21+
val make : ?get_time:(unit -> float) -> unit -> t
2222

2323
val get : t -> float
2424

0 commit comments

Comments
 (0)