Skip to content

Commit 9e363d1

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 1fa0eca commit 9e363d1

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
@@ -689,6 +689,33 @@ let link_and_pack ?(standalone = true) ?(wrap_with_fun = `Iife) ?(link = `No) p
689689
|> pack ~wrap_with_fun ~standalone
690690
|> check_js
691691

692+
let all_functions p =
693+
let open Code in
694+
fold_closures
695+
p
696+
(fun name _ _ acc ->
697+
match name with
698+
| Some name -> Var.Set.add name acc
699+
| None -> acc)
700+
Var.Set.empty
701+
702+
let effects_or_lambda_lift ~deadcode_sentinal p =
703+
(* If effects are disabled, we lambda-lift aggressively. While not necessary, it results
704+
in a substantial gain in performance for Javascript. *)
705+
match Config.(target (), effects ()) with
706+
| `JavaScript, `Disabled ->
707+
let to_lift = all_functions p in
708+
let p, _ = Lambda_lifting_simple.f ~to_lift p in
709+
( p
710+
, (Code.Var.Set.empty : Effects.trampolined_calls)
711+
, (Code.Var.Set.empty : Effects.in_cps) )
712+
| _, (`Cps | `Double_translation) -> effects ~deadcode_sentinal p
713+
| `Wasm, (`Disabled | `Jspi) ->
714+
( p
715+
, (Code.Var.Set.empty : Effects.trampolined_calls)
716+
, (Code.Var.Set.empty : Effects.in_cps) )
717+
| `JavaScript, `Jspi -> assert false
718+
692719
let optimize ~profile p =
693720
let deadcode_sentinal =
694721
(* If deadcode is disabled, this field is just fresh variable *)
@@ -702,7 +729,7 @@ let optimize ~profile p =
702729
| O2 -> o2
703730
| O3 -> o3)
704731
+> exact_calls ~deadcode_sentinal profile
705-
+> effects ~deadcode_sentinal
732+
+> effects_or_lambda_lift ~deadcode_sentinal
706733
+> map_fst
707734
(match Config.target (), Config.effects () with
708735
| `JavaScript, `Disabled -> Generate_closure.f

0 commit comments

Comments
 (0)