Skip to content

Commit 65c5706

Browse files
committed
reduce intermediate names
1 parent 018e503 commit 65c5706

File tree

4 files changed

+49
-11
lines changed

4 files changed

+49
-11
lines changed

jscomp/core/js_fun_env.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ let make ?immutable_mask n = {
6868
bound_loop_mutable_values =Set_ident.empty;
6969
}
7070

71-
(* let is_tailcalled x =
72-
x.immutable_mask <> All_immutable_and_no_tail_call *)
71+
let no_tailcall x =
72+
x.immutable_mask = All_immutable_and_no_tail_call
7373

7474
let mark_unused t i =
7575
t.used_mask.(i) <- true

jscomp/core/js_fun_env.mli

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ type t
3737

3838
val make : ?immutable_mask:bool array -> int -> t
3939

40-
(* val is_tailcalled : t -> bool
40+
val no_tailcall : t -> bool
4141

42-
val is_empty : t -> bool *)
42+
(* val is_empty : t -> bool *)
4343

4444
val set_unbounded : t -> Set_ident.t -> unit
4545

jscomp/core/js_pass_tailcall_inline.ml

+43-5
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ module S = Js_stmt_make
4141
(* module E = Js_exp_make *)
4242

4343

44-
44+
let substitue_variables (map : Ident.t Map_ident.t) =
45+
object (self)
46+
inherit Js_map.map
47+
method! ident id =
48+
Map_ident.find_default map id id
49+
end
4550

4651
(* 1. recursive value ? let rec x = 1 :: x
4752
non-terminating
@@ -52,6 +57,36 @@ module S = Js_stmt_make
5257
we already have this? in [defined_idents]
5358
*)
5459

60+
let inline_call
61+
no_tailcall
62+
params (args : J.expression list) processed_blocks =
63+
if no_tailcall then
64+
let map, block =
65+
Ext_list.fold_right2
66+
params args (Map_ident.empty, processed_blocks)
67+
(fun param arg (map,acc) ->
68+
match arg.expression_desc with
69+
| Var (Id id) ->
70+
Map_ident.add map param id, acc
71+
| _ ->
72+
map, S.define_variable ~kind:Variable param arg :: acc) in
73+
if Map_ident.is_empty map then block
74+
else (substitue_variables map) # block block
75+
else
76+
(*
77+
At this time, when tailcall happened, the parameter can be assigned
78+
for example {[
79+
function (_x,y){
80+
_x = u
81+
}
82+
]}
83+
if it is substitued, the assignment will align the value which is incorrect
84+
*)
85+
Ext_list.fold_right2
86+
params args processed_blocks
87+
(fun param arg acc ->
88+
S.define_variable ~kind:Variable param arg :: acc)
89+
5590
(** There is a side effect when traversing dead code, since
5691
we assume that substitue a node would mark a node as dead node,
5792
@@ -132,7 +167,7 @@ let subst (export_set : Set_ident.t) stats =
132167
begin match Hash_ident.find_opt stats id with
133168

134169
| Some ({ value =
135-
Some {expression_desc = Fun (false, params, block, _env) ; comment = _};
170+
Some {expression_desc = Fun (false, params, block, env) ; comment = _};
136171
(*TODO: don't inline method tail call yet,
137172
[this] semantics are weird
138173
*)
@@ -143,10 +178,13 @@ let subst (export_set : Set_ident.t) stats =
143178
when Ext_list.same_length params args
144179
->
145180
Js_op_util.update_used_stats v.ident_info Dead_pure;
146-
Ext_list.fold_right2
147-
params args ( self#block block) (* see #278 before changes*)
181+
let no_tailcall = Js_fun_env.no_tailcall env in
182+
let processed_blocks = ( self#block block) (* see #278 before changes*) in
183+
inline_call no_tailcall params args processed_blocks
184+
(* Ext_list.fold_right2
185+
params args processed_blocks
148186
(fun param arg acc ->
149-
S.define_variable ~kind:Variable param arg :: acc)
187+
S.define_variable ~kind:Variable param arg :: acc) *)
150188
(* Mark a function as dead means it will never be scanned,
151189
here we inline the function
152190
*)

jscomp/others/belt_MutableQueue.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ let rec copyAux qRes prev cell =
127127
| None -> lastSet qRes prev; qRes
128128
| Some x ->
129129
let content = contentGet x in
130-
let res = return @@ node ~content ~next:null in
130+
let res = node ~content ~next:null |. return in
131131
begin match Js.nullToOption prev with
132132
| None -> firstSet qRes res
133133
| Some p -> nextSet p res
@@ -144,7 +144,7 @@ let rec copyMapAux qRes prev cell f =
144144
| None -> lastSet qRes prev; qRes
145145
| Some x ->
146146
let content = f (contentGet x) [@bs] in
147-
let res = return @@ node ~content ~next:null in
147+
let res = node ~content ~next:null |. return in
148148
begin match Js.nullToOption prev with (*TODO: optimize to remove such check*)
149149
| None -> firstSet qRes res
150150
| Some p -> nextSet p res

0 commit comments

Comments
 (0)