Skip to content

Commit 3f94285

Browse files
committed
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 4bafb2e commit 3f94285

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
@@ -673,6 +673,33 @@ let link_and_pack ?(standalone = true) ?(wrap_with_fun = `Iife) ?(link = `No) p
673673
|> pack ~wrap_with_fun ~standalone
674674
|> check_js
675675

676+
let all_functions p =
677+
let open Code in
678+
fold_closures
679+
p
680+
(fun name _ _ _ acc ->
681+
match name with
682+
| Some name -> Var.Set.add name acc
683+
| None -> acc)
684+
Var.Set.empty
685+
686+
let effects_or_lambda_lift ~deadcode_sentinal p =
687+
(* If effects are disabled, we lambda-lift aggressively. While not necessary, it results
688+
in a substantial gain in performance for Javascript. *)
689+
match Config.(target (), effects ()) with
690+
| `JavaScript, `Disabled ->
691+
let to_lift = all_functions p in
692+
let p, _ = Lambda_lifting_simple.f ~to_lift p in
693+
( p
694+
, (Code.Var.Set.empty : Effects.trampolined_calls)
695+
, (Code.Var.Set.empty : Effects.in_cps) )
696+
| _, (`Cps | `Double_translation) -> effects ~deadcode_sentinal p
697+
| `Wasm, (`Disabled | `Jspi) ->
698+
( p
699+
, (Code.Var.Set.empty : Effects.trampolined_calls)
700+
, (Code.Var.Set.empty : Effects.in_cps) )
701+
| `JavaScript, `Jspi -> assert false
702+
676703
let optimize ~profile p =
677704
let deadcode_sentinal =
678705
(* If deadcode is disabled, this field is just fresh variable *)
@@ -687,7 +714,7 @@ let optimize ~profile p =
687714
| O3 -> o3)
688715
+> specialize_js_once_after
689716
+> exact_calls ~deadcode_sentinal profile
690-
+> effects ~deadcode_sentinal
717+
+> effects_or_lambda_lift ~deadcode_sentinal
691718
+> map_fst
692719
(match Config.target (), Config.effects () with
693720
| `JavaScript, `Disabled -> Generate_closure.f

0 commit comments

Comments
 (0)