Skip to content

Commit 418e0ed

Browse files
OlivierNicolevouillon
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 7439373 commit 418e0ed

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
@@ -682,6 +682,33 @@ let link_and_pack ?(standalone = true) ?(wrap_with_fun = `Iife) ?(link = `No) p
682682
|> pack ~wrap_with_fun ~standalone
683683
|> check_js
684684

685+
let all_functions p =
686+
let open Code in
687+
fold_closures
688+
p
689+
(fun name _ _ acc ->
690+
match name with
691+
| Some name -> Var.Set.add name acc
692+
| None -> acc)
693+
Var.Set.empty
694+
695+
let effects_or_lambda_lift ~deadcode_sentinal p =
696+
(* If effects are disabled, we lambda-lift aggressively. While not necessary, it results
697+
in a substantial gain in performance for Javascript. *)
698+
match Config.(target (), effects ()) with
699+
| `JavaScript, `Disabled ->
700+
let to_lift = all_functions p in
701+
let p, _ = Lambda_lifting_simple.f ~to_lift p in
702+
( p
703+
, (Code.Var.Set.empty : Effects.trampolined_calls)
704+
, (Code.Var.Set.empty : Effects.in_cps) )
705+
| _, (`Cps | `Double_translation) -> effects ~deadcode_sentinal p
706+
| `Wasm, (`Disabled | `Jspi) ->
707+
( p
708+
, (Code.Var.Set.empty : Effects.trampolined_calls)
709+
, (Code.Var.Set.empty : Effects.in_cps) )
710+
| `JavaScript, `Jspi -> assert false
711+
685712
let optimize ~profile p =
686713
let deadcode_sentinal =
687714
(* If deadcode is disabled, this field is just fresh variable *)
@@ -694,7 +721,7 @@ let optimize ~profile p =
694721
| O2 -> o2
695722
| O3 -> o3)
696723
+> exact_calls ~deadcode_sentinal profile
697-
+> effects ~deadcode_sentinal
724+
+> effects_or_lambda_lift ~deadcode_sentinal
698725
+> map_fst
699726
(match Config.target (), Config.effects () with
700727
| `JavaScript, `Disabled -> Generate_closure.f

0 commit comments

Comments
 (0)