Skip to content

Commit 77bb5d2

Browse files
committed
Finish the optimization around visitor pattern using objects
1 parent 64be639 commit 77bb5d2

File tree

9 files changed

+286
-287
lines changed

9 files changed

+286
-287
lines changed

jscomp/core/js_fold.ml

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11

22
open J
33
let [@inline] unknown _self _ = _self
4+
let [@inline] option sub self = fun v ->
5+
match v with
6+
| None -> self
7+
| Some x -> sub x
48
class fold =
59
object ((_self : 'self_type))
6-
method option :
7-
'a. ('self_type -> 'a -> 'self_type) -> 'a option -> 'self_type =
8-
fun _f_a -> function | None -> _self | Some _x -> let _self = _f_a _self _x in _self
910
method list :
1011
'a. ('self_type -> 'a -> 'self_type) -> 'a list -> 'self_type =
1112
fun _f_a ->
@@ -22,7 +23,6 @@ let _self = _self#ident _x0 in
2223
_self
2324
|Qualified ( _x0,_x1) ->
2425
let _self = _self#module_id _x0 in
25-
let _self = _self#option (fun _self -> unknown _self) _x1 in
2626
_self
2727
method exception_ident : exception_ident -> 'self_type = _self#ident
2828
method for_ident : for_ident -> 'self_type = _self#ident
@@ -85,11 +85,10 @@ let _self = _self#expression _x1 in
8585
_self
8686
|Static_index ( _x0,_x1,_x2) ->
8787
let _self = _self#expression _x0 in
88-
let _self = _self#option (fun _self -> unknown _self) _x2 in
8988
_self
9089
|New ( _x0,_x1) ->
9190
let _self = _self#expression _x0 in
92-
let _self = _self#option (fun _self -> _self#list (fun _self -> _self#expression)) _x1 in
91+
let _self = option (_self#list (fun _self -> _self#expression)) _self _x1 in
9392
_self
9493
|Var ( _x0) ->
9594
let _self = _self#vident _x0 in
@@ -138,12 +137,12 @@ let _self = _self#block _x1 in
138137
let _self = _self#block _x2 in
139138
_self
140139
|While ( _x0,_x1,_x2,_x3) ->
141-
let _self = _self#option (fun _self -> _self#label) _x0 in
140+
let _self = option (_self#label) _self _x0 in
142141
let _self = _self#expression _x1 in
143142
let _self = _self#block _x2 in
144143
_self
145144
|ForRange ( _x0,_x1,_x2,_x3,_x4,_x5) ->
146-
let _self = _self#option (fun _self -> _self#for_ident_expression) _x0 in
145+
let _self = option (_self#for_ident_expression) _self _x0 in
147146
let _self = _self#finish_ident_expression _x1 in
148147
let _self = _self#for_ident _x2 in
149148
let _self = _self#for_direction _x3 in
@@ -159,36 +158,32 @@ let _self = _self#expression _x0 in
159158
|Int_switch ( _x0,_x1,_x2) ->
160159
let _self = _self#expression _x0 in
161160
let _self = _self#list (fun _self -> _self#int_clause) _x1 in
162-
let _self = _self#option (fun _self -> _self#block) _x2 in
161+
let _self = option (_self#block) _self _x2 in
163162
_self
164163
|String_switch ( _x0,_x1,_x2) ->
165164
let _self = _self#expression _x0 in
166165
let _self = _self#list (fun _self -> _self#string_clause) _x1 in
167-
let _self = _self#option (fun _self -> _self#block) _x2 in
166+
let _self = option (_self#block) _self _x2 in
168167
_self
169168
|Throw ( _x0) ->
170169
let _self = _self#expression _x0 in
171170
_self
172171
|Try ( _x0,_x1,_x2) ->
173172
let _self = _self#block _x0 in
174-
let _self = _self#option (fun _self -> fun ( _x0,_x1) -> let _self = _self#exception_ident _x0 in let _self = _self#block _x1 in _self) _x1 in
175-
let _self = _self#option (fun _self -> _self#block) _x2 in
173+
let _self = option (fun ( _x0,_x1) -> let _self = _self#exception_ident _x0 in let _self = _self#block _x1 in _self) _self _x1 in
174+
let _self = option (_self#block) _self _x2 in
176175
_self
177176
|Debugger -> _self
178-
method expression : expression -> 'self_type = fun { expression_desc = _x0;comment = _x1} -> let _self = _self#expression_desc _x0 in
179-
let _self = _self#option (fun _self -> unknown _self) _x1 in _self
180-
method statement : statement -> 'self_type = fun { statement_desc = _x0;comment = _x1} -> let _self = _self#statement_desc _x0 in
181-
let _self = _self#option (fun _self -> unknown _self) _x1 in _self
177+
method expression : expression -> 'self_type = fun { expression_desc = _x0;comment = _x1} -> let _self = _self#expression_desc _x0 in _self
178+
method statement : statement -> 'self_type = fun { statement_desc = _x0;comment = _x1} -> let _self = _self#statement_desc _x0 in _self
182179
method variable_declaration : variable_declaration -> 'self_type = fun { ident = _x0;value = _x1;property = _x2;ident_info = _x3} -> let _self = _self#ident _x0 in
183-
let _self = _self#option (fun _self -> _self#expression) _x1 in _self
180+
let _self = option (_self#expression) _self _x1 in _self
184181
method string_clause : string_clause -> 'self_type = fun ( _x0,_x1) -> let _self = _self#case_clause _x1 in _self
185182
method int_clause : int_clause -> 'self_type = fun ( _x0,_x1) -> let _self = _self#case_clause _x1 in _self
186-
method case_clause : case_clause -> 'self_type = fun { switch_body = _x0;should_break = _x1;comment = _x2} -> let _self = _self#block _x0 in
187-
let _self = _self#option (fun _self -> unknown _self) _x2 in _self
183+
method case_clause : case_clause -> 'self_type = fun { switch_body = _x0;should_break = _x1;comment = _x2} -> let _self = _self#block _x0 in _self
188184
method block : block -> 'self_type = _self#list (fun _self -> _self#statement)
189185
method program : program -> 'self_type = fun { block = _x0;exports = _x1;export_set = _x2} -> let _self = _self#block _x0 in _self
190186
method deps_program : deps_program -> 'self_type = fun { program = _x0;modules = _x1;side_effect = _x2} -> let _self = _self#program _x0 in
191-
let _self = _self#required_modules _x1 in
192-
let _self = _self#option (fun _self -> unknown _self) _x2 in _self
187+
let _self = _self#required_modules _x1 in _self
193188
end
194189

jscomp/core/js_iter.ml

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
11

22
open J
3-
let unknown _self _ = ()
3+
4+
let option sub = fun v ->
5+
match v with
6+
| None -> ()
7+
| Some v -> sub v
48
class iter =
59
object ((_self : 'self_type))
6-
method option :
7-
'a. ('self_type -> 'a -> unit) -> 'a option -> unit =
8-
fun _f_a -> function | None -> () | Some _x -> _f_a _self _x
910
method list :
1011
'a. ('self_type -> 'a -> unit) -> 'a list -> unit =
1112
fun _f_a ->
1213
function
1314
| [] -> ()
1415
| _x :: _x_i1 -> _f_a _self _x ; _self#list _f_a _x_i1
15-
method label : label -> unit = unknown _self
16+
method label : label -> unit = ignore
1617
method required_modules : required_modules -> unit = _self#list (fun _self -> _self#module_id)
17-
method ident : ident -> unit = unknown _self
18+
method ident : ident -> unit = ignore
1819
method module_id : module_id -> unit = fun { id = _x0;kind = _x1} -> begin _self#ident _x0 end
1920
method vident : vident -> unit = function
2021
| Id ( _x0) ->
2122
begin _self#ident _x0 end
2223
|Qualified ( _x0,_x1) ->
23-
begin _self#module_id _x0;_self#option (fun _self -> unknown _self) _x1 end
24+
begin _self#module_id _x0 end
2425
method exception_ident : exception_ident -> unit = _self#ident
2526
method for_ident : for_ident -> unit = _self#ident
26-
method for_direction : for_direction -> unit = unknown _self
27+
method for_direction : for_direction -> unit = ignore
2728
method property_map : property_map -> unit = _self#list (fun _self -> fun ( _x0,_x1) -> begin _self#expression _x1 end)
28-
method length_object : length_object -> unit = unknown _self
29+
method length_object : length_object -> unit = ignore
2930
method expression_desc : expression_desc -> unit = function
3031
| Length ( _x0,_x1) ->
3132
begin _self#expression _x0;_self#length_object _x1 end
@@ -57,9 +58,9 @@ method expression_desc : expression_desc -> unit = function
5758
|Array_index ( _x0,_x1) ->
5859
begin _self#expression _x0;_self#expression _x1 end
5960
|Static_index ( _x0,_x1,_x2) ->
60-
begin _self#expression _x0;_self#option (fun _self -> unknown _self) _x2 end
61+
begin _self#expression _x0 end
6162
|New ( _x0,_x1) ->
62-
begin _self#expression _x0;_self#option (fun _self -> _self#list (fun _self -> _self#expression)) _x1 end
63+
begin _self#expression _x0;option (_self#list (fun _self -> _self#expression)) _x1 end
6364
|Var ( _x0) ->
6465
begin _self#vident _x0 end
6566
|Fun ( _x0,_x1,_x2,_x3) ->
@@ -92,31 +93,31 @@ method statement_desc : statement_desc -> unit = function
9293
|If ( _x0,_x1,_x2) ->
9394
begin _self#expression _x0;_self#block _x1;_self#block _x2 end
9495
|While ( _x0,_x1,_x2,_x3) ->
95-
begin _self#option (fun _self -> _self#label) _x0;_self#expression _x1;_self#block _x2 end
96+
begin option (_self#label) _x0;_self#expression _x1;_self#block _x2 end
9697
|ForRange ( _x0,_x1,_x2,_x3,_x4,_x5) ->
97-
begin _self#option (fun _self -> _self#for_ident_expression) _x0;_self#finish_ident_expression _x1;_self#for_ident _x2;_self#for_direction _x3;_self#block _x4 end
98+
begin option (_self#for_ident_expression) _x0;_self#finish_ident_expression _x1;_self#for_ident _x2;_self#for_direction _x3;_self#block _x4 end
9899
|Continue ( _x0) ->
99100
begin _self#label _x0 end
100101
|Break -> ()
101102
|Return ( _x0) ->
102103
begin _self#expression _x0 end
103104
|Int_switch ( _x0,_x1,_x2) ->
104-
begin _self#expression _x0;_self#list (fun _self -> _self#int_clause) _x1;_self#option (fun _self -> _self#block) _x2 end
105+
begin _self#expression _x0;_self#list (fun _self -> _self#int_clause) _x1;option (_self#block) _x2 end
105106
|String_switch ( _x0,_x1,_x2) ->
106-
begin _self#expression _x0;_self#list (fun _self -> _self#string_clause) _x1;_self#option (fun _self -> _self#block) _x2 end
107+
begin _self#expression _x0;_self#list (fun _self -> _self#string_clause) _x1;option (_self#block) _x2 end
107108
|Throw ( _x0) ->
108109
begin _self#expression _x0 end
109110
|Try ( _x0,_x1,_x2) ->
110-
begin _self#block _x0;_self#option (fun _self -> fun ( _x0,_x1) -> begin _self#exception_ident _x0;_self#block _x1 end) _x1;_self#option (fun _self -> _self#block) _x2 end
111+
begin _self#block _x0;option (fun ( _x0,_x1) -> begin _self#exception_ident _x0;_self#block _x1 end) _x1;option (_self#block) _x2 end
111112
|Debugger -> ()
112-
method expression : expression -> unit = fun { expression_desc = _x0;comment = _x1} -> begin _self#expression_desc _x0;_self#option (fun _self -> unknown _self) _x1 end
113-
method statement : statement -> unit = fun { statement_desc = _x0;comment = _x1} -> begin _self#statement_desc _x0;_self#option (fun _self -> unknown _self) _x1 end
114-
method variable_declaration : variable_declaration -> unit = fun { ident = _x0;value = _x1;property = _x2;ident_info = _x3} -> begin _self#ident _x0;_self#option (fun _self -> _self#expression) _x1 end
113+
method expression : expression -> unit = fun { expression_desc = _x0;comment = _x1} -> begin _self#expression_desc _x0 end
114+
method statement : statement -> unit = fun { statement_desc = _x0;comment = _x1} -> begin _self#statement_desc _x0 end
115+
method variable_declaration : variable_declaration -> unit = fun { ident = _x0;value = _x1;property = _x2;ident_info = _x3} -> begin _self#ident _x0;option (_self#expression) _x1 end
115116
method string_clause : string_clause -> unit = fun ( _x0,_x1) -> begin _self#case_clause _x1 end
116117
method int_clause : int_clause -> unit = fun ( _x0,_x1) -> begin _self#case_clause _x1 end
117-
method case_clause : case_clause -> unit = fun { switch_body = _x0;should_break = _x1;comment = _x2} -> begin _self#block _x0;_self#option (fun _self -> unknown _self) _x2 end
118+
method case_clause : case_clause -> unit = fun { switch_body = _x0;should_break = _x1;comment = _x2} -> begin _self#block _x0 end
118119
method block : block -> unit = _self#list (fun _self -> _self#statement)
119120
method program : program -> unit = fun { block = _x0;exports = _x1;export_set = _x2} -> begin _self#block _x0 end
120-
method deps_program : deps_program -> unit = fun { program = _x0;modules = _x1;side_effect = _x2} -> begin _self#program _x0;_self#required_modules _x1;_self#option (fun _self -> unknown _self) _x2 end
121+
method deps_program : deps_program -> unit = fun { program = _x0;modules = _x1;side_effect = _x2} -> begin _self#program _x0;_self#required_modules _x1 end
121122
end
122123

jscomp/core/js_map.ml

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11

22
open J
33
let unknown : 'a. 'a -> 'a = fun x -> x
4+
let option sub = fun v ->
5+
match v with
6+
| None -> None
7+
| Some v -> Some (sub v)
48
class map = object
59
((_self : 'self_type))
6-
method option :
7-
'a 'a_out. ('self_type -> 'a -> 'a_out) -> 'a option -> 'a_out option =
8-
fun _f_a ->
9-
function | None -> None | Some _x -> let _x = _f_a _self _x in Some _x
1010
method list :
1111
'a 'a_out. ('self_type -> 'a -> 'a_out) -> 'a list -> 'a_out list =
1212
fun _f_a ->
@@ -25,7 +25,6 @@ let _x0 = _self#ident _x0 in
2525
Id ( _x0)
2626
|Qualified ( _x0,_x1) ->
2727
let _x0 = _self#module_id _x0 in
28-
let _x1 = _self#option (fun _self -> unknown) _x1 in
2928
Qualified ( _x0,_x1)
3029
method exception_ident : exception_ident -> exception_ident = _self#ident
3130
method for_ident : for_ident -> for_ident = _self#ident
@@ -88,11 +87,10 @@ let _x1 = _self#expression _x1 in
8887
Array_index ( _x0,_x1)
8988
|Static_index ( _x0,_x1,_x2) ->
9089
let _x0 = _self#expression _x0 in
91-
let _x2 = _self#option (fun _self -> unknown) _x2 in
9290
Static_index ( _x0,_x1,_x2)
9391
|New ( _x0,_x1) ->
9492
let _x0 = _self#expression _x0 in
95-
let _x1 = _self#option (fun _self -> _self#list (fun _self -> _self#expression)) _x1 in
93+
let _x1 = option (_self#list (fun _self -> _self#expression)) _x1 in
9694
New ( _x0,_x1)
9795
|Var ( _x0) ->
9896
let _x0 = _self#vident _x0 in
@@ -141,12 +139,12 @@ let _x1 = _self#block _x1 in
141139
let _x2 = _self#block _x2 in
142140
If ( _x0,_x1,_x2)
143141
|While ( _x0,_x1,_x2,_x3) ->
144-
let _x0 = _self#option (fun _self -> _self#label) _x0 in
142+
let _x0 = option (_self#label) _x0 in
145143
let _x1 = _self#expression _x1 in
146144
let _x2 = _self#block _x2 in
147145
While ( _x0,_x1,_x2,_x3)
148146
|ForRange ( _x0,_x1,_x2,_x3,_x4,_x5) ->
149-
let _x0 = _self#option (fun _self -> _self#for_ident_expression) _x0 in
147+
let _x0 = option (_self#for_ident_expression) _x0 in
150148
let _x1 = _self#finish_ident_expression _x1 in
151149
let _x2 = _self#for_ident _x2 in
152150
let _x3 = _self#for_direction _x3 in
@@ -162,35 +160,31 @@ Return ( _x0)
162160
|Int_switch ( _x0,_x1,_x2) ->
163161
let _x0 = _self#expression _x0 in
164162
let _x1 = _self#list (fun _self -> _self#int_clause) _x1 in
165-
let _x2 = _self#option (fun _self -> _self#block) _x2 in
163+
let _x2 = option (_self#block) _x2 in
166164
Int_switch ( _x0,_x1,_x2)
167165
|String_switch ( _x0,_x1,_x2) ->
168166
let _x0 = _self#expression _x0 in
169167
let _x1 = _self#list (fun _self -> _self#string_clause) _x1 in
170-
let _x2 = _self#option (fun _self -> _self#block) _x2 in
168+
let _x2 = option (_self#block) _x2 in
171169
String_switch ( _x0,_x1,_x2)
172170
|Throw ( _x0) ->
173171
let _x0 = _self#expression _x0 in
174172
Throw ( _x0)
175173
|Try ( _x0,_x1,_x2) ->
176174
let _x0 = _self#block _x0 in
177-
let _x1 = _self#option (fun _self -> fun ( _x0,_x1) -> let _x0 = _self#exception_ident _x0 in let _x1 = _self#block _x1 in _x0,_x1) _x1 in
178-
let _x2 = _self#option (fun _self -> _self#block) _x2 in
175+
let _x1 = option (fun ( _x0,_x1) -> let _x0 = _self#exception_ident _x0 in let _x1 = _self#block _x1 in _x0,_x1) _x1 in
176+
let _x2 = option (_self#block) _x2 in
179177
Try ( _x0,_x1,_x2)
180178
|Debugger as v -> v
181-
method expression : expression -> expression = fun { expression_desc = _x0;comment = _x1} -> let _x0 = _self#expression_desc _x0 in
182-
let _x1 = _self#option (fun _self -> unknown) _x1 in {expression_desc = _x0;comment = _x1}
183-
method statement : statement -> statement = fun { statement_desc = _x0;comment = _x1} -> let _x0 = _self#statement_desc _x0 in
184-
let _x1 = _self#option (fun _self -> unknown) _x1 in {statement_desc = _x0;comment = _x1}
179+
method expression : expression -> expression = fun { expression_desc = _x0;comment = _x1} -> let _x0 = _self#expression_desc _x0 in {expression_desc = _x0;comment = _x1}
180+
method statement : statement -> statement = fun { statement_desc = _x0;comment = _x1} -> let _x0 = _self#statement_desc _x0 in {statement_desc = _x0;comment = _x1}
185181
method variable_declaration : variable_declaration -> variable_declaration = fun { ident = _x0;value = _x1;property = _x2;ident_info = _x3} -> let _x0 = _self#ident _x0 in
186-
let _x1 = _self#option (fun _self -> _self#expression) _x1 in {ident = _x0;value = _x1;property = _x2;ident_info = _x3}
182+
let _x1 = option (_self#expression) _x1 in {ident = _x0;value = _x1;property = _x2;ident_info = _x3}
187183
method string_clause : string_clause -> string_clause = fun ( _x0,_x1) -> let _x1 = _self#case_clause _x1 in _x0,_x1
188184
method int_clause : int_clause -> int_clause = fun ( _x0,_x1) -> let _x1 = _self#case_clause _x1 in _x0,_x1
189-
method case_clause : case_clause -> case_clause = fun { switch_body = _x0;should_break = _x1;comment = _x2} -> let _x0 = _self#block _x0 in
190-
let _x2 = _self#option (fun _self -> unknown) _x2 in {switch_body = _x0;should_break = _x1;comment = _x2}
185+
method case_clause : case_clause -> case_clause = fun { switch_body = _x0;should_break = _x1;comment = _x2} -> let _x0 = _self#block _x0 in {switch_body = _x0;should_break = _x1;comment = _x2}
191186
method block : block -> block = _self#list (fun _self -> _self#statement)
192187
method program : program -> program = fun { block = _x0;exports = _x1;export_set = _x2} -> let _x0 = _self#block _x0 in {block = _x0;exports = _x1;export_set = _x2}
193188
method deps_program : deps_program -> deps_program = fun { program = _x0;modules = _x1;side_effect = _x2} -> let _x0 = _self#program _x0 in
194-
let _x1 = _self#required_modules _x1 in
195-
let _x2 = _self#option (fun _self -> unknown) _x2 in {program = _x0;modules = _x1;side_effect = _x2}
189+
let _x1 = _self#required_modules _x1 in {program = _x0;modules = _x1;side_effect = _x2}
196190
end

0 commit comments

Comments
 (0)