Skip to content

Commit 65cf622

Browse files
OlivierNicolehhugo
authored andcommitted
Do more aggressive lambda lifting
With @vouillon we realized that the `Lambda_lifting_simple` pass that is performed by double translation makes some programs significantly faster. We measured roughly a 1.45 speedup on a large (proprietary) Bonsai benchmark. Presumably V8 is much faster with more toplevel functions and less nested closures.
1 parent 3d05b15 commit 65cf622

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

compiler/lib/driver.ml

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,33 @@ let link_and_pack ?(standalone = true) ?(wrap_with_fun = `Iife) ?(link = `No) p
665665
|> pack ~wrap_with_fun ~standalone
666666
|> check_js
667667

668+
let all_functions p =
669+
let open Code in
670+
fold_closures
671+
p
672+
(fun name _ _ _ acc ->
673+
match name with
674+
| Some name -> Var.Set.add name acc
675+
| None -> acc)
676+
Var.Set.empty
677+
678+
let effects_or_lambda_lift ~deadcode_sentinal p =
679+
(* If effects are disabled, we lambda-lift aggressively. While not necessary, it results
680+
in a substantial gain in performance for Javascript. *)
681+
match Config.(target (), effects ()) with
682+
| `JavaScript, `Disabled ->
683+
let to_lift = all_functions p in
684+
let p, _ = Lambda_lifting_simple.f ~to_lift p in
685+
( p
686+
, (Code.Var.Set.empty : Effects.trampolined_calls)
687+
, (Code.Var.Set.empty : Effects.in_cps) )
688+
| _, (`Cps | `Double_translation) -> effects ~deadcode_sentinal p
689+
| `Wasm, (`Disabled | `Jspi) ->
690+
( p
691+
, (Code.Var.Set.empty : Effects.trampolined_calls)
692+
, (Code.Var.Set.empty : Effects.in_cps) )
693+
| `JavaScript, `Jspi -> assert false
694+
668695
let optimize ~profile p =
669696
let deadcode_sentinal =
670697
(* If deadcode is disabled, this field is just fresh variable *)
@@ -679,7 +706,7 @@ let optimize ~profile p =
679706
| O3 -> o3)
680707
+> specialize_js_once_after
681708
+> exact_calls ~deadcode_sentinal profile
682-
+> effects ~deadcode_sentinal
709+
+> effects_or_lambda_lift ~deadcode_sentinal
683710
+> map_fst
684711
(match Config.target (), Config.effects () with
685712
| `JavaScript, `Disabled -> Generate_closure.f

0 commit comments

Comments
 (0)