Skip to content

Commit ef94cb4

Browse files
committed
avoid functional objects in free_variables
1 parent b85a53d commit ef94cb4

File tree

6 files changed

+136
-102
lines changed

6 files changed

+136
-102
lines changed

jscomp/core/js_analyzer.ml

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@
2929

3030

3131

32+
type idents_stats = {
33+
mutable used_idents : Set_ident.t ;
34+
mutable defined_idents : Set_ident.t;
35+
}
3236

37+
let add_defined_idents (x : idents_stats) ident =
38+
x.defined_idents <- Set_ident.add x.defined_idents ident
3339

3440
(* Assume that functions already calculated closure correctly
3541
Maybe in the future, we should add a dirty flag, to mark the calcuated
@@ -38,45 +44,48 @@
3844
Note such shaking is done in the toplevel, so that it requires us to
3945
flatten the statement first
4046
*)
41-
let free_variables used_idents defined_idents =
47+
let free_variables (stats : idents_stats) : Js_fold.fold =
4248
object (self)
4349
inherit Js_fold.fold as super
44-
val defined_idents = defined_idents
45-
val used_idents = used_idents
46-
method get_depenencies =
47-
Set_ident.diff used_idents defined_idents
4850
method! variable_declaration st =
49-
match st with
50-
| { ident; value = None}
51+
add_defined_idents stats st.ident;
52+
match st.value with
53+
| None
54+
-> self
55+
| Some v
5156
->
52-
{< defined_idents = Set_ident.add defined_idents ident >}
53-
| { ident; value = Some v}
54-
->
55-
{< defined_idents = Set_ident.add defined_idents ident >} # expression v
57+
self # expression v
5658
method! ident id =
57-
if Set_ident.mem defined_idents id then self
58-
else {<used_idents = Set_ident.add used_idents id>}
59+
(if not (Set_ident.mem stats.defined_idents id )then
60+
stats.used_idents <- Set_ident.add stats.used_idents id);
61+
self
5962
method! expression exp =
60-
6163
match exp.expression_desc with
6264
| Fun(_, _,_, env)
6365
(** a optimization to avoid walking into funciton again
6466
if it's already comuted
6567
*)
6668
->
67-
{< used_idents =
68-
Set_ident.union (Js_fun_env.get_unbounded env) used_idents >}
69+
stats.used_idents <-
70+
Set_ident.union (Js_fun_env.get_unbounded env) stats.used_idents;
71+
self
6972

7073
| _
7174
->
7275
super#expression exp
7376
end
7477

75-
let free_variables_of_statement used_idents defined_idents st =
76-
((free_variables used_idents defined_idents)#statement st) # get_depenencies
78+
let free_variables_of_statement st =
79+
let init = {used_idents = Set_ident.empty;
80+
defined_idents = Set_ident.empty} in
81+
let _ = (free_variables init)#statement st in
82+
Set_ident.diff init.used_idents init.defined_idents
7783

78-
let free_variables_of_expression used_idents defined_idents st =
79-
((free_variables used_idents defined_idents)#expression st) # get_depenencies
84+
let free_variables_of_expression st =
85+
let init = {used_idents = Set_ident.empty;
86+
defined_idents = Set_ident.empty} in
87+
let _ = (free_variables init)#expression st in
88+
Set_ident.diff init.used_idents init.defined_idents
8089

8190
let rec no_side_effect_expression_desc (x : J.expression_desc) =
8291
match x with

jscomp/core/js_analyzer.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@
3535
*)
3636

3737
val free_variables_of_statement :
38-
Set_ident.t -> Set_ident.t -> J.statement -> Set_ident.t
38+
J.statement -> Set_ident.t
3939

4040
val free_variables_of_expression :
41-
Set_ident.t -> Set_ident.t -> J.finish_ident_expression -> Set_ident.t
41+
J.finish_ident_expression -> Set_ident.t
4242

4343
(* val no_side_effect_expression_desc :
4444
J.expression_desc -> bool *)

jscomp/core/js_shake.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ let get_initial_exports
4646
TODO: add hashtbl for a cache
4747
*)
4848
Set_ident.(
49-
union (Js_analyzer.free_variables_of_expression empty empty x) acc)
49+
union (Js_analyzer.free_variables_of_expression x) acc)
5050
end
5151
else
5252
begin match value with
@@ -55,14 +55,14 @@ let get_initial_exports
5555
if Js_analyzer.no_side_effect_expression x then acc
5656
else
5757
Set_ident.(
58-
union (Js_analyzer.free_variables_of_expression empty empty x)
58+
union (Js_analyzer.free_variables_of_expression x)
5959
(add acc ident))
6060
end
6161
| _ ->
6262
(* recalcuate again and again ... *)
6363
if Js_analyzer.no_side_effect_statement st || (not count_non_variable_declaration_statement)
6464
then acc
65-
else Set_ident.(union (Js_analyzer.free_variables_of_statement empty empty st) acc)
65+
else Set_ident.(union (Js_analyzer.free_variables_of_statement st) acc)
6666
) in result, Set_ident.(diff result export_set)
6767

6868
let shake_program (program : J.program) =

lib/4.06.1/unstable/js_compiler.ml

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -86983,10 +86983,10 @@ module Js_analyzer : sig
8698386983
*)
8698486984

8698586985
val free_variables_of_statement :
86986-
Set_ident.t -> Set_ident.t -> J.statement -> Set_ident.t
86986+
J.statement -> Set_ident.t
8698786987

8698886988
val free_variables_of_expression :
86989-
Set_ident.t -> Set_ident.t -> J.finish_ident_expression -> Set_ident.t
86989+
J.finish_ident_expression -> Set_ident.t
8699086990

8699186991
(* val no_side_effect_expression_desc :
8699286992
J.expression_desc -> bool *)
@@ -87068,7 +87068,13 @@ end = struct
8706887068

8706987069

8707087070

87071+
type idents_stats = {
87072+
mutable used_idents : Set_ident.t ;
87073+
mutable defined_idents : Set_ident.t;
87074+
}
8707187075

87076+
let add_defined_idents (x : idents_stats) ident =
87077+
x.defined_idents <- Set_ident.add x.defined_idents ident
8707287078

8707387079
(* Assume that functions already calculated closure correctly
8707487080
Maybe in the future, we should add a dirty flag, to mark the calcuated
@@ -87077,24 +87083,21 @@ end = struct
8707787083
Note such shaking is done in the toplevel, so that it requires us to
8707887084
flatten the statement first
8707987085
*)
87080-
let free_variables used_idents defined_idents =
87086+
let free_variables (stats : idents_stats) : Js_fold.fold =
8708187087
object (self)
8708287088
inherit Js_fold.fold as super
87083-
val defined_idents = defined_idents
87084-
val used_idents = used_idents
87085-
method get_depenencies =
87086-
Set_ident.diff used_idents defined_idents
8708787089
method! variable_declaration st =
87088-
match st with
87089-
| { ident; value = None}
87090-
->
87091-
{< defined_idents = Set_ident.add defined_idents ident >}
87092-
| { ident; value = Some v}
87090+
add_defined_idents stats st.ident;
87091+
match st.value with
87092+
| None
87093+
-> self
87094+
| Some v
8709387095
->
87094-
{< defined_idents = Set_ident.add defined_idents ident >} # expression v
87096+
self # expression v
8709587097
method! ident id =
87096-
if Set_ident.mem defined_idents id then self
87097-
else {<used_idents = Set_ident.add used_idents id>}
87098+
(if not (Set_ident.mem stats.defined_idents id )then
87099+
stats.used_idents <- Set_ident.add stats.used_idents id);
87100+
self
8709887101
method! expression exp =
8709987102

8710087103
match exp.expression_desc with
@@ -87103,19 +87106,26 @@ let free_variables used_idents defined_idents =
8710387106
if it's already comuted
8710487107
*)
8710587108
->
87106-
{< used_idents =
87107-
Set_ident.union (Js_fun_env.get_unbounded env) used_idents >}
87109+
stats.used_idents <-
87110+
Set_ident.union (Js_fun_env.get_unbounded env) stats.used_idents;
87111+
self
8710887112

8710987113
| _
8711087114
->
8711187115
super#expression exp
8711287116
end
8711387117

87114-
let free_variables_of_statement used_idents defined_idents st =
87115-
((free_variables used_idents defined_idents)#statement st) # get_depenencies
87118+
let free_variables_of_statement st =
87119+
let init = {used_idents = Set_ident.empty;
87120+
defined_idents = Set_ident.empty} in
87121+
let _ = (free_variables init)#statement st in
87122+
Set_ident.diff init.used_idents init.defined_idents
8711687123

87117-
let free_variables_of_expression used_idents defined_idents st =
87118-
((free_variables used_idents defined_idents)#expression st) # get_depenencies
87124+
let free_variables_of_expression st =
87125+
let init = {used_idents = Set_ident.empty;
87126+
defined_idents = Set_ident.empty} in
87127+
let _ = (free_variables init)#expression st in
87128+
Set_ident.diff init.used_idents init.defined_idents
8711987129

8712087130
let rec no_side_effect_expression_desc (x : J.expression_desc) =
8712187131
match x with
@@ -103015,7 +103025,7 @@ let get_initial_exports
103015103025
TODO: add hashtbl for a cache
103016103026
*)
103017103027
Set_ident.(
103018-
union (Js_analyzer.free_variables_of_expression empty empty x) acc)
103028+
union (Js_analyzer.free_variables_of_expression x) acc)
103019103029
end
103020103030
else
103021103031
begin match value with
@@ -103024,14 +103034,14 @@ let get_initial_exports
103024103034
if Js_analyzer.no_side_effect_expression x then acc
103025103035
else
103026103036
Set_ident.(
103027-
union (Js_analyzer.free_variables_of_expression empty empty x)
103037+
union (Js_analyzer.free_variables_of_expression x)
103028103038
(add acc ident))
103029103039
end
103030103040
| _ ->
103031103041
(* recalcuate again and again ... *)
103032103042
if Js_analyzer.no_side_effect_statement st || (not count_non_variable_declaration_statement)
103033103043
then acc
103034-
else Set_ident.(union (Js_analyzer.free_variables_of_statement empty empty st) acc)
103044+
else Set_ident.(union (Js_analyzer.free_variables_of_statement st) acc)
103035103045
) in result, Set_ident.(diff result export_set)
103036103046

103037103047
let shake_program (program : J.program) =

lib/4.06.1/unstable/js_refmt_compiler.ml

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -86983,10 +86983,10 @@ module Js_analyzer : sig
8698386983
*)
8698486984

8698586985
val free_variables_of_statement :
86986-
Set_ident.t -> Set_ident.t -> J.statement -> Set_ident.t
86986+
J.statement -> Set_ident.t
8698786987

8698886988
val free_variables_of_expression :
86989-
Set_ident.t -> Set_ident.t -> J.finish_ident_expression -> Set_ident.t
86989+
J.finish_ident_expression -> Set_ident.t
8699086990

8699186991
(* val no_side_effect_expression_desc :
8699286992
J.expression_desc -> bool *)
@@ -87068,7 +87068,13 @@ end = struct
8706887068

8706987069

8707087070

87071+
type idents_stats = {
87072+
mutable used_idents : Set_ident.t ;
87073+
mutable defined_idents : Set_ident.t;
87074+
}
8707187075

87076+
let add_defined_idents (x : idents_stats) ident =
87077+
x.defined_idents <- Set_ident.add x.defined_idents ident
8707287078

8707387079
(* Assume that functions already calculated closure correctly
8707487080
Maybe in the future, we should add a dirty flag, to mark the calcuated
@@ -87077,24 +87083,21 @@ end = struct
8707787083
Note such shaking is done in the toplevel, so that it requires us to
8707887084
flatten the statement first
8707987085
*)
87080-
let free_variables used_idents defined_idents =
87086+
let free_variables (stats : idents_stats) : Js_fold.fold =
8708187087
object (self)
8708287088
inherit Js_fold.fold as super
87083-
val defined_idents = defined_idents
87084-
val used_idents = used_idents
87085-
method get_depenencies =
87086-
Set_ident.diff used_idents defined_idents
8708787089
method! variable_declaration st =
87088-
match st with
87089-
| { ident; value = None}
87090-
->
87091-
{< defined_idents = Set_ident.add defined_idents ident >}
87092-
| { ident; value = Some v}
87090+
add_defined_idents stats st.ident;
87091+
match st.value with
87092+
| None
87093+
-> self
87094+
| Some v
8709387095
->
87094-
{< defined_idents = Set_ident.add defined_idents ident >} # expression v
87096+
self # expression v
8709587097
method! ident id =
87096-
if Set_ident.mem defined_idents id then self
87097-
else {<used_idents = Set_ident.add used_idents id>}
87098+
(if not (Set_ident.mem stats.defined_idents id )then
87099+
stats.used_idents <- Set_ident.add stats.used_idents id);
87100+
self
8709887101
method! expression exp =
8709987102

8710087103
match exp.expression_desc with
@@ -87103,19 +87106,26 @@ let free_variables used_idents defined_idents =
8710387106
if it's already comuted
8710487107
*)
8710587108
->
87106-
{< used_idents =
87107-
Set_ident.union (Js_fun_env.get_unbounded env) used_idents >}
87109+
stats.used_idents <-
87110+
Set_ident.union (Js_fun_env.get_unbounded env) stats.used_idents;
87111+
self
8710887112

8710987113
| _
8711087114
->
8711187115
super#expression exp
8711287116
end
8711387117

87114-
let free_variables_of_statement used_idents defined_idents st =
87115-
((free_variables used_idents defined_idents)#statement st) # get_depenencies
87118+
let free_variables_of_statement st =
87119+
let init = {used_idents = Set_ident.empty;
87120+
defined_idents = Set_ident.empty} in
87121+
let _ = (free_variables init)#statement st in
87122+
Set_ident.diff init.used_idents init.defined_idents
8711687123

87117-
let free_variables_of_expression used_idents defined_idents st =
87118-
((free_variables used_idents defined_idents)#expression st) # get_depenencies
87124+
let free_variables_of_expression st =
87125+
let init = {used_idents = Set_ident.empty;
87126+
defined_idents = Set_ident.empty} in
87127+
let _ = (free_variables init)#expression st in
87128+
Set_ident.diff init.used_idents init.defined_idents
8711987129

8712087130
let rec no_side_effect_expression_desc (x : J.expression_desc) =
8712187131
match x with
@@ -87190,11 +87200,6 @@ let no_side_effect clean : Js_fold.fold =
8719087200
(if !clean then
8719187201
clean := no_side_effect_expression s);
8719287202
self
87193-
87194-
87195-
87196-
(* {< no_side_effect = no_side_effect_expression s >} *)
87197-
8719887203
(** only expression would cause side effec *)
8719987204
end
8720087205
let no_side_effect_statement st =
@@ -103020,7 +103025,7 @@ let get_initial_exports
103020103025
TODO: add hashtbl for a cache
103021103026
*)
103022103027
Set_ident.(
103023-
union (Js_analyzer.free_variables_of_expression empty empty x) acc)
103028+
union (Js_analyzer.free_variables_of_expression x) acc)
103024103029
end
103025103030
else
103026103031
begin match value with
@@ -103029,14 +103034,14 @@ let get_initial_exports
103029103034
if Js_analyzer.no_side_effect_expression x then acc
103030103035
else
103031103036
Set_ident.(
103032-
union (Js_analyzer.free_variables_of_expression empty empty x)
103037+
union (Js_analyzer.free_variables_of_expression x)
103033103038
(add acc ident))
103034103039
end
103035103040
| _ ->
103036103041
(* recalcuate again and again ... *)
103037103042
if Js_analyzer.no_side_effect_statement st || (not count_non_variable_declaration_statement)
103038103043
then acc
103039-
else Set_ident.(union (Js_analyzer.free_variables_of_statement empty empty st) acc)
103044+
else Set_ident.(union (Js_analyzer.free_variables_of_statement st) acc)
103040103045
) in result, Set_ident.(diff result export_set)
103041103046

103042103047
let shake_program (program : J.program) =

0 commit comments

Comments
 (0)