Skip to content

Commit 0561523

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 c09c641 commit 0561523

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
@@ -619,6 +619,33 @@ let link_and_pack ?(standalone = true) ?(wrap_with_fun = `Iife) ?(link = `No) p
619619
|> pack ~wrap_with_fun ~standalone
620620
|> check_js
621621

622+
let all_functions p =
623+
let open Code in
624+
fold_closures
625+
p
626+
(fun name _ _ _ acc ->
627+
match name with
628+
| Some name -> Var.Set.add name acc
629+
| None -> acc)
630+
Var.Set.empty
631+
632+
let effects_or_lambda_lift ~deadcode_sentinal p =
633+
(* If effects are disabled, we lambda-lift aggressively. While not necessary, it results
634+
in a substantial gain in performance for Javascript. *)
635+
match Config.(target (), effects ()) with
636+
| `JavaScript, `Disabled ->
637+
let to_lift = all_functions p in
638+
let p, _ = Lambda_lifting_simple.f ~to_lift p in
639+
( p
640+
, (Code.Var.Set.empty : Effects.trampolined_calls)
641+
, (Code.Var.Set.empty : Effects.in_cps) )
642+
| _, (`Cps | `Double_translation) -> effects ~deadcode_sentinal p
643+
| `Wasm, (`Disabled | `Jspi) ->
644+
( p
645+
, (Code.Var.Set.empty : Effects.trampolined_calls)
646+
, (Code.Var.Set.empty : Effects.in_cps) )
647+
| `JavaScript, `Jspi -> assert false
648+
622649
let optimize ~profile p =
623650
let deadcode_sentinal =
624651
(* If deadcode is disabled, this field is just fresh variable *)
@@ -633,7 +660,7 @@ let optimize ~profile p =
633660
| O3 -> o3)
634661
+> specialize_js_once_after
635662
+> exact_calls ~deadcode_sentinal profile
636-
+> effects ~deadcode_sentinal
663+
+> effects_or_lambda_lift ~deadcode_sentinal
637664
+> map_fst
638665
(match Config.target (), Config.effects () with
639666
| `JavaScript, `Disabled -> Generate_closure.f

0 commit comments

Comments
 (0)