Skip to content

Commit 310d9b3

Browse files
committed
clean up reducer pattern
most reducer could be replaced with iterator in imperative settings
1 parent 2df69f1 commit 310d9b3

File tree

4 files changed

+131
-108
lines changed

4 files changed

+131
-108
lines changed

jscomp/core/js_pass_get_used.ml

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,39 @@
2424

2525
let add_use stats id =
2626
Hash_ident.add_or_update stats id 1 ~update:succ
27-
27+
let post_process_stats my_export_set (defined_idents : J.variable_declaration Hash_ident.t) stats =
28+
Hash_ident.iter defined_idents (fun ident v ->
29+
if Set_ident.mem my_export_set ident then
30+
Js_op_util.update_used_stats v.ident_info Exported
31+
else
32+
let pure =
33+
match v.value with
34+
| None -> false (* can not happen *)
35+
| Some x -> Js_analyzer.no_side_effect_expression x in
36+
match Hash_ident.find_opt stats ident with
37+
| None ->
38+
Js_op_util.update_used_stats v.ident_info
39+
(if pure then Dead_pure else Dead_non_pure)
40+
| Some num ->
41+
if num = 1 then
42+
Js_op_util.update_used_stats v.ident_info
43+
(if pure then Once_pure else Used)
44+
) ; defined_idents
2845
(** Update ident info use cases, it is a non pure function,
2946
it will annotate [program] with some meta data
3047
TODO: Ident Hash could be improved,
3148
since in this case it can not be global?
3249
3350
*)
34-
let count_collects my_export_set =
51+
let count_collects
52+
(* collect used status*)
53+
(stats : int Hash_ident.t)
54+
(* collect all def sites *)
55+
(defined_idents : J.variable_declaration Hash_ident.t)
56+
=
3557
object (self)
3658
inherit Js_fold.fold
37-
(* collect used status*)
38-
val stats : int Hash_ident.t = Hash_ident.create 83
39-
(* collect all def sites *)
40-
val defined_idents : J.variable_declaration Hash_ident.t = Hash_ident.create 83
41-
59+
4260
method! variable_declaration
4361
({ident; value ; property = _ ; ident_info = _} as v)
4462
=
@@ -49,27 +67,14 @@ let count_collects my_export_set =
4967
| Some x
5068
-> self#expression x
5169
method! ident id = add_use stats id; self
52-
method get_stats =
53-
Hash_ident.iter defined_idents (fun ident v ->
54-
if Set_ident.mem my_export_set ident then
55-
Js_op_util.update_used_stats v.ident_info Exported
56-
else
57-
let pure =
58-
match v.value with
59-
| None -> false (* can not happen *)
60-
| Some x -> Js_analyzer.no_side_effect_expression x in
61-
match Hash_ident.find_opt stats ident with
62-
| None ->
63-
Js_op_util.update_used_stats v.ident_info
64-
(if pure then Dead_pure else Dead_non_pure)
65-
| Some num ->
66-
if num = 1 then
67-
Js_op_util.update_used_stats v.ident_info
68-
(if pure then Once_pure else Used)
69-
) ; defined_idents
70+
7071
end
7172

7273

7374
let get_stats (program : J.program) : J.variable_declaration Hash_ident.t
74-
= ((count_collects program.export_set) #program program) #get_stats
75-
75+
=
76+
let stats : int Hash_ident.t = Hash_ident.create 83 in
77+
let defined_idents : J.variable_declaration Hash_ident.t = Hash_ident.create 83 in
78+
let my_export_set = program.export_set in
79+
let _ : Js_fold.fold = (count_collects stats defined_idents) #program program in
80+
post_process_stats my_export_set defined_idents stats

lib/4.06.1/unstable/js_compiler.ml

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -102597,21 +102597,39 @@ end = struct
102597102597

102598102598
let add_use stats id =
102599102599
Hash_ident.add_or_update stats id 1 ~update:succ
102600-
102600+
let post_process_stats my_export_set (defined_idents : J.variable_declaration Hash_ident.t) stats =
102601+
Hash_ident.iter defined_idents (fun ident v ->
102602+
if Set_ident.mem my_export_set ident then
102603+
Js_op_util.update_used_stats v.ident_info Exported
102604+
else
102605+
let pure =
102606+
match v.value with
102607+
| None -> false (* can not happen *)
102608+
| Some x -> Js_analyzer.no_side_effect_expression x in
102609+
match Hash_ident.find_opt stats ident with
102610+
| None ->
102611+
Js_op_util.update_used_stats v.ident_info
102612+
(if pure then Dead_pure else Dead_non_pure)
102613+
| Some num ->
102614+
if num = 1 then
102615+
Js_op_util.update_used_stats v.ident_info
102616+
(if pure then Once_pure else Used)
102617+
) ; defined_idents
102601102618
(** Update ident info use cases, it is a non pure function,
102602102619
it will annotate [program] with some meta data
102603102620
TODO: Ident Hash could be improved,
102604102621
since in this case it can not be global?
102605102622

102606102623
*)
102607-
let count_collects my_export_set =
102624+
let count_collects
102625+
(* collect used status*)
102626+
(stats : int Hash_ident.t)
102627+
(* collect all def sites *)
102628+
(defined_idents : J.variable_declaration Hash_ident.t)
102629+
=
102608102630
object (self)
102609102631
inherit Js_fold.fold
102610-
(* collect used status*)
102611-
val stats : int Hash_ident.t = Hash_ident.create 83
102612-
(* collect all def sites *)
102613-
val defined_idents : J.variable_declaration Hash_ident.t = Hash_ident.create 83
102614-
102632+
102615102633
method! variable_declaration
102616102634
({ident; value ; property = _ ; ident_info = _} as v)
102617102635
=
@@ -102622,30 +102640,18 @@ let count_collects my_export_set =
102622102640
| Some x
102623102641
-> self#expression x
102624102642
method! ident id = add_use stats id; self
102625-
method get_stats =
102626-
Hash_ident.iter defined_idents (fun ident v ->
102627-
if Set_ident.mem my_export_set ident then
102628-
Js_op_util.update_used_stats v.ident_info Exported
102629-
else
102630-
let pure =
102631-
match v.value with
102632-
| None -> false (* can not happen *)
102633-
| Some x -> Js_analyzer.no_side_effect_expression x in
102634-
match Hash_ident.find_opt stats ident with
102635-
| None ->
102636-
Js_op_util.update_used_stats v.ident_info
102637-
(if pure then Dead_pure else Dead_non_pure)
102638-
| Some num ->
102639-
if num = 1 then
102640-
Js_op_util.update_used_stats v.ident_info
102641-
(if pure then Once_pure else Used)
102642-
) ; defined_idents
102643+
102643102644
end
102644102645

102645102646

102646102647
let get_stats (program : J.program) : J.variable_declaration Hash_ident.t
102647-
= ((count_collects program.export_set) #program program) #get_stats
102648-
102648+
=
102649+
let stats : int Hash_ident.t = Hash_ident.create 83 in
102650+
let defined_idents : J.variable_declaration Hash_ident.t = Hash_ident.create 83 in
102651+
let my_export_set = program.export_set in
102652+
let _ : Js_fold.fold = (count_collects stats defined_idents) #program program in
102653+
post_process_stats my_export_set defined_idents stats
102654+
102649102655
end
102650102656
module Js_pass_tailcall_inline : sig
102651102657
#1 "js_pass_tailcall_inline.mli"

lib/4.06.1/unstable/js_refmt_compiler.ml

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -102597,21 +102597,39 @@ end = struct
102597102597

102598102598
let add_use stats id =
102599102599
Hash_ident.add_or_update stats id 1 ~update:succ
102600-
102600+
let post_process_stats my_export_set (defined_idents : J.variable_declaration Hash_ident.t) stats =
102601+
Hash_ident.iter defined_idents (fun ident v ->
102602+
if Set_ident.mem my_export_set ident then
102603+
Js_op_util.update_used_stats v.ident_info Exported
102604+
else
102605+
let pure =
102606+
match v.value with
102607+
| None -> false (* can not happen *)
102608+
| Some x -> Js_analyzer.no_side_effect_expression x in
102609+
match Hash_ident.find_opt stats ident with
102610+
| None ->
102611+
Js_op_util.update_used_stats v.ident_info
102612+
(if pure then Dead_pure else Dead_non_pure)
102613+
| Some num ->
102614+
if num = 1 then
102615+
Js_op_util.update_used_stats v.ident_info
102616+
(if pure then Once_pure else Used)
102617+
) ; defined_idents
102601102618
(** Update ident info use cases, it is a non pure function,
102602102619
it will annotate [program] with some meta data
102603102620
TODO: Ident Hash could be improved,
102604102621
since in this case it can not be global?
102605102622

102606102623
*)
102607-
let count_collects my_export_set =
102624+
let count_collects
102625+
(* collect used status*)
102626+
(stats : int Hash_ident.t)
102627+
(* collect all def sites *)
102628+
(defined_idents : J.variable_declaration Hash_ident.t)
102629+
=
102608102630
object (self)
102609102631
inherit Js_fold.fold
102610-
(* collect used status*)
102611-
val stats : int Hash_ident.t = Hash_ident.create 83
102612-
(* collect all def sites *)
102613-
val defined_idents : J.variable_declaration Hash_ident.t = Hash_ident.create 83
102614-
102632+
102615102633
method! variable_declaration
102616102634
({ident; value ; property = _ ; ident_info = _} as v)
102617102635
=
@@ -102622,30 +102640,18 @@ let count_collects my_export_set =
102622102640
| Some x
102623102641
-> self#expression x
102624102642
method! ident id = add_use stats id; self
102625-
method get_stats =
102626-
Hash_ident.iter defined_idents (fun ident v ->
102627-
if Set_ident.mem my_export_set ident then
102628-
Js_op_util.update_used_stats v.ident_info Exported
102629-
else
102630-
let pure =
102631-
match v.value with
102632-
| None -> false (* can not happen *)
102633-
| Some x -> Js_analyzer.no_side_effect_expression x in
102634-
match Hash_ident.find_opt stats ident with
102635-
| None ->
102636-
Js_op_util.update_used_stats v.ident_info
102637-
(if pure then Dead_pure else Dead_non_pure)
102638-
| Some num ->
102639-
if num = 1 then
102640-
Js_op_util.update_used_stats v.ident_info
102641-
(if pure then Once_pure else Used)
102642-
) ; defined_idents
102643+
102643102644
end
102644102645

102645102646

102646102647
let get_stats (program : J.program) : J.variable_declaration Hash_ident.t
102647-
= ((count_collects program.export_set) #program program) #get_stats
102648-
102648+
=
102649+
let stats : int Hash_ident.t = Hash_ident.create 83 in
102650+
let defined_idents : J.variable_declaration Hash_ident.t = Hash_ident.create 83 in
102651+
let my_export_set = program.export_set in
102652+
let _ : Js_fold.fold = (count_collects stats defined_idents) #program program in
102653+
post_process_stats my_export_set defined_idents stats
102654+
102649102655
end
102650102656
module Js_pass_tailcall_inline : sig
102651102657
#1 "js_pass_tailcall_inline.mli"

lib/4.06.1/whole_compiler.ml

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -385079,21 +385079,39 @@ end = struct
385079385079

385080385080
let add_use stats id =
385081385081
Hash_ident.add_or_update stats id 1 ~update:succ
385082-
385082+
let post_process_stats my_export_set (defined_idents : J.variable_declaration Hash_ident.t) stats =
385083+
Hash_ident.iter defined_idents (fun ident v ->
385084+
if Set_ident.mem my_export_set ident then
385085+
Js_op_util.update_used_stats v.ident_info Exported
385086+
else
385087+
let pure =
385088+
match v.value with
385089+
| None -> false (* can not happen *)
385090+
| Some x -> Js_analyzer.no_side_effect_expression x in
385091+
match Hash_ident.find_opt stats ident with
385092+
| None ->
385093+
Js_op_util.update_used_stats v.ident_info
385094+
(if pure then Dead_pure else Dead_non_pure)
385095+
| Some num ->
385096+
if num = 1 then
385097+
Js_op_util.update_used_stats v.ident_info
385098+
(if pure then Once_pure else Used)
385099+
) ; defined_idents
385083385100
(** Update ident info use cases, it is a non pure function,
385084385101
it will annotate [program] with some meta data
385085385102
TODO: Ident Hash could be improved,
385086385103
since in this case it can not be global?
385087385104

385088385105
*)
385089-
let count_collects my_export_set =
385106+
let count_collects
385107+
(* collect used status*)
385108+
(stats : int Hash_ident.t)
385109+
(* collect all def sites *)
385110+
(defined_idents : J.variable_declaration Hash_ident.t)
385111+
=
385090385112
object (self)
385091385113
inherit Js_fold.fold
385092-
(* collect used status*)
385093-
val stats : int Hash_ident.t = Hash_ident.create 83
385094-
(* collect all def sites *)
385095-
val defined_idents : J.variable_declaration Hash_ident.t = Hash_ident.create 83
385096-
385114+
385097385115
method! variable_declaration
385098385116
({ident; value ; property = _ ; ident_info = _} as v)
385099385117
=
@@ -385104,30 +385122,18 @@ let count_collects my_export_set =
385104385122
| Some x
385105385123
-> self#expression x
385106385124
method! ident id = add_use stats id; self
385107-
method get_stats =
385108-
Hash_ident.iter defined_idents (fun ident v ->
385109-
if Set_ident.mem my_export_set ident then
385110-
Js_op_util.update_used_stats v.ident_info Exported
385111-
else
385112-
let pure =
385113-
match v.value with
385114-
| None -> false (* can not happen *)
385115-
| Some x -> Js_analyzer.no_side_effect_expression x in
385116-
match Hash_ident.find_opt stats ident with
385117-
| None ->
385118-
Js_op_util.update_used_stats v.ident_info
385119-
(if pure then Dead_pure else Dead_non_pure)
385120-
| Some num ->
385121-
if num = 1 then
385122-
Js_op_util.update_used_stats v.ident_info
385123-
(if pure then Once_pure else Used)
385124-
) ; defined_idents
385125+
385125385126
end
385126385127

385127385128

385128385129
let get_stats (program : J.program) : J.variable_declaration Hash_ident.t
385129-
= ((count_collects program.export_set) #program program) #get_stats
385130-
385130+
=
385131+
let stats : int Hash_ident.t = Hash_ident.create 83 in
385132+
let defined_idents : J.variable_declaration Hash_ident.t = Hash_ident.create 83 in
385133+
let my_export_set = program.export_set in
385134+
let _ : Js_fold.fold = (count_collects stats defined_idents) #program program in
385135+
post_process_stats my_export_set defined_idents stats
385136+
385131385137
end
385132385138
module Js_pass_tailcall_inline : sig
385133385139
#1 "js_pass_tailcall_inline.mli"

0 commit comments

Comments
 (0)