Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/js-ir-mapper-sharing.internal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Return unchanged JS IR nodes from the JS passes instead of copying them, so compiling allocates about 3% less.
53 changes: 27 additions & 26 deletions compiler/backend/js_pass_external_shadow.ml
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ let program (js : J.program) : J.program =
let super = Js_record_map.super in
let map_cases (self : Js_record_map.iter) clauses default =
with_bindings (add_switch_declarations clauses default) (fun () ->
( List.map
(fun (key, (c : J.case_clause)) ->
(key, {c with switch_body = self.block self c.switch_body}))
clauses,
match default with
| None -> None
| Some block -> Some (self.block self block) ))
(* The default case is mapped before the clauses, as the pair this
function used to build evaluated its second component first. *)
let default = Js_record_map.option self.block self default in
let clauses =
Ext_list.map_snd_sharing clauses (Js_record_map.case_clause self)
in
(clauses, default))
in
let self =
{
Expand Down Expand Up @@ -131,31 +131,32 @@ let program (js : J.program) : J.program =
one scope. The discriminant is evaluated before that scope is
entered. *)
| Int_switch (e, clauses, default) ->
let e = self.expression self e in
let clauses, default = map_cases self clauses default in
{st with statement_desc = Int_switch (e, clauses, default)}
let e' = self.expression self e in
let clauses', default' = map_cases self clauses default in
if e' == e && clauses' == clauses && default' == default then st
else {st with statement_desc = Int_switch (e', clauses', default')}
| String_switch (e, clauses, default) ->
let e = self.expression self e in
let clauses, default = map_cases self clauses default in
{st with statement_desc = String_switch (e, clauses, default)}
let e' = self.expression self e in
let clauses', default' = map_cases self clauses default in
if e' == e && clauses' == clauses && default' == default then st
else
{st with statement_desc = String_switch (e', clauses', default')}
(* The catch parameter is only in scope in the handler. *)
| Try (body, catch, finally) ->
let body = self.block self body in
let catch =
let body' = self.block self body in
let catch' =
match catch with
| None -> None
| None -> catch
| Some (ident, handler) ->
Some
( ident,
with_bindings (add_binding ident) (fun () ->
self.block self handler) )
let handler' =
with_bindings (add_binding ident) (fun () ->
self.block self handler)
in
if handler' == handler then catch else Some (ident, handler')
in
let finally =
match finally with
| None -> None
| Some block -> Some (self.block self block)
in
{st with statement_desc = Try (body, catch, finally)}
let finally' = Js_record_map.option self.block self finally in
if body' == body && catch' == catch && finally' == finally then st
else {st with statement_desc = Try (body', catch', finally')}
| _ -> super.statement self st);
}
in
Expand Down
2 changes: 1 addition & 1 deletion compiler/backend/js_pass_flatten.ml
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ let flatten_map =
let block = self.block self rest in
match st.statement_desc with
| Block bs -> bs @ block
| _ -> st :: block)
| _ -> if st == x && block == rest then b else st :: block)
| [] -> []);
}

Expand Down
9 changes: 8 additions & 1 deletion compiler/backend/js_pass_record_rest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,14 @@ let pass =
S.define_variable ~kind:Strict rest
{rest_expr with expression_desc = Record_rest (fields, source)}
:: (return @ tail)
| statement :: tail -> self.statement self statement :: self.block self tail
| statement :: tail as b ->
(* The tail is mapped before the head, as in the
[self.statement self statement :: self.block self tail] this
replaces. *)
let tail' = self.block self tail in
let statement' = self.statement self statement in
if statement' == statement && tail' == tail then b
else statement' :: tail'
| [] -> []
in
{super with block}
Expand Down
21 changes: 16 additions & 5 deletions compiler/backend/js_pass_tailcall_inline.ml
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,15 @@ let inline_call (assignable : Set_ident.t) (immutable_list : bool list) params
*)
let super = Js_record_map.super

(* [map_cons self bs x xs] maps [bs = x :: xs] like
[self.statement self x :: self.block self xs]: the tail first, then the
head (the pass records statistics as it goes). It returns [bs] itself
when neither changes. *)
let map_cons (self : Js_record_map.iter) bs x xs =
let xs' = self.block self xs in
let x' = self.statement self x in
if x' == x && xs' == xs then bs else x' :: xs'

let subst (export_set : Set_ident.t) (assignable : Set_ident.t) stats =
{
super with
Expand Down Expand Up @@ -211,15 +220,15 @@ let subst (export_set : Set_ident.t) (assignable : Set_ident.t) stats =
} as st)
:: rest -> (
let is_export = Set_ident.mem vd.ident export_set in
if is_export then self.statement self st :: self.block self rest
if is_export then map_cons self bs st rest
else
match Hash_ident.find_opt stats vd.ident with
(* TODO: could be improved as [mem] *)
| None ->
if Js_analyzer.no_side_effect_expression v then
S.exp v :: self.block self rest
else self.block self rest
| Some _ -> self.statement self st :: self.block self rest)
| Some _ -> map_cons self bs st rest)
| [
({
statement_desc =
Expand Down Expand Up @@ -270,7 +279,9 @@ let subst (export_set : Set_ident.t) (assignable : Set_ident.t) stats =
(* Mark a function as dead means it will never be scanned,
here we inline the function
*)
| None | Some _ -> [self.statement self st])
| None | Some _ ->
let st' = self.statement self st in
if st' == st then bs else [st'])
| [
{
statement_desc =
Expand Down Expand Up @@ -302,8 +313,8 @@ let subst (export_set : Set_ident.t) (assignable : Set_ident.t) stats =
(* see #278 before changes*)
in
inline_call assignable no_tailcall params args processed_blocks
| x :: xs -> self.statement self x :: self.block self xs
| [] -> []);
| x :: xs -> map_cons self bs x xs
| [] -> bs);
}

let tailcall_inline ~assignable (program : J.program) =
Expand Down
Loading
Loading