Skip to content

Commit cc442bc

Browse files
authored
Merge pull request #655 from bloomberg/better_autogen
Better autogen: preparing for the ultimate curry optimization
2 parents 70bf0f7 + a72bbb6 commit cc442bc

File tree

12 files changed

+800
-295
lines changed

12 files changed

+800
-295
lines changed

jscomp/gen_slots.sh

Lines changed: 0 additions & 3 deletions
This file was deleted.

jscomp/ocaml_stdlib_slots.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
2+
(* Generated by scripts/gen_slots.ml, should be updated everytime when we upgrade the compiler *)
13
let pervasives = [| "invalid_arg";"failwith";"Exit";"min";"max";"abs";"max_int";"min_int";"lnot";"infinity";"neg_infinity";"nan";"max_float";"min_float";"epsilon_float";"^";"char_of_int";"string_of_bool";"bool_of_string";"string_of_int";"string_of_float";"@";"stdin";"stdout";"stderr";"print_char";"print_string";"print_bytes";"print_int";"print_float";"print_endline";"print_newline";"prerr_char";"prerr_string";"prerr_bytes";"prerr_int";"prerr_float";"prerr_endline";"prerr_newline";"read_line";"read_int";"read_float";"open_out";"open_out_bin";"open_out_gen";"flush";"flush_all";"output_char";"output_string";"output_bytes";"output";"output_substring";"output_byte";"output_binary_int";"output_value";"seek_out";"pos_out";"out_channel_length";"close_out";"close_out_noerr";"set_binary_mode_out";"open_in";"open_in_bin";"open_in_gen";"input_char";"input_line";"input";"really_input";"really_input_string";"input_byte";"input_binary_int";"input_value";"seek_in";"pos_in";"in_channel_length";"close_in";"close_in_noerr";"set_binary_mode_in";"LargeFile";"string_of_format";"^^";"exit";"at_exit";"valid_float_lexem";"unsafe_really_input";"do_at_exit" |]
24
let camlinternalOO = [| "public_method_label";"new_method";"new_variable";"new_methods_variables";"get_variable";"get_variables";"get_method_label";"get_method_labels";"get_method";"set_method";"set_methods";"narrow";"widen";"add_initializer";"dummy_table";"create_table";"init_class";"inherits";"make_class";"make_class_store";"dummy_class";"copy";"create_object";"create_object_opt";"run_initializers";"run_initializers_opt";"create_object_and_run_initializers";"lookup_tables";"params";"stats" |]
35
let camlinternalMod = [| "init_mod";"update_mod" |]
46
let string = [| "make";"init";"copy";"sub";"fill";"blit";"concat";"iter";"iteri";"map";"mapi";"trim";"escaped";"index";"rindex";"index_from";"rindex_from";"contains";"contains_from";"rcontains_from";"uppercase";"lowercase";"capitalize";"uncapitalize";"compare" |]
57
let array = [| "init";"make_matrix";"create_matrix";"append";"concat";"sub";"copy";"fill";"blit";"to_list";"of_list";"iter";"map";"iteri";"mapi";"fold_left";"fold_right";"sort";"stable_sort";"fast_sort" |]
68
let list = [| "length";"hd";"tl";"nth";"rev";"append";"rev_append";"concat";"flatten";"iter";"iteri";"map";"mapi";"rev_map";"fold_left";"fold_right";"iter2";"map2";"rev_map2";"fold_left2";"fold_right2";"for_all";"exists";"for_all2";"exists2";"mem";"memq";"find";"filter";"find_all";"partition";"assoc";"assq";"mem_assoc";"mem_assq";"remove_assoc";"remove_assq";"split";"combine";"sort";"stable_sort";"fast_sort";"sort_uniq";"merge" |]
9+

jscomp/runtime/curry.ml

Lines changed: 180 additions & 177 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
23
*
34
* This program is free software: you can redistribute it and/or modify
@@ -22,62 +23,9 @@
2223
* along with this program; if not, write to the Free Software
2324
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2425

25-
26-
27-
28-
29-
30-
31-
32-
33-
34-
35-
external function_length : 'a -> int = "js_function_length"
36-
37-
external apply1 :
38-
('a -> 'b) -> 'a -> 'b
39-
= "js_apply1"
40-
external apply2 :
41-
('a -> 'b -> 'c) -> 'a -> 'b -> 'c
42-
= "js_apply2"
43-
44-
external apply3 :
45-
('a -> 'b -> 'c -> 'd)
46-
-> 'a -> 'b -> 'c -> 'd
47-
= "js_apply3"
48-
49-
external apply4 :
50-
('a -> 'b -> 'c -> 'd -> 'e)
51-
-> 'a -> 'b -> 'c -> 'd -> 'e
52-
= "js_apply4"
53-
54-
external apply5 :
55-
('a -> 'b -> 'c -> 'd -> 'e -> 'f )
56-
-> 'a -> 'b -> 'c -> 'd -> 'e -> 'f =
57-
"js_apply5"
58-
59-
external apply6 :
60-
('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g )
61-
-> 'a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g =
62-
"js_apply6"
63-
64-
65-
external apply7 :
66-
('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h )
67-
-> 'a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h =
68-
"js_apply7"
69-
70-
71-
external apply8 :
72-
('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h -> 'i)
73-
-> 'a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h -> 'i =
74-
"js_apply8"
75-
76-
external apply_args :
77-
('a -> 'b) -> _ array -> 'b = "js_apply"
78-
79-
80-
26+
(** Generated by scripts/curry_gen.ml *)
27+
external function_length : 'a -> int = "js_function_length"
28+
external apply_args : ('a -> 'b) -> _ array -> 'b = "js_apply"
8129
external sub : 'a array -> int -> int -> 'a array = "caml_array_sub"
8230

8331
let rec app f args =
@@ -94,131 +42,186 @@ let rec app f args =
9442
else
9543
Obj.magic (fun x -> app f (Caml_array.append args [|x|] ))
9644

97-
(* Generated code
98-
[if/else]
99-
Here it would be nice to just generate
100-
[switch .. default]
101-
*)
102-
let curry1 o x arity =
103-
(match arity with
104-
| 0 -> apply1 (Obj.magic o) x
105-
| 1 -> apply1 (Obj.magic o) x
106-
| 2 -> apply2 (Obj.magic o) x
107-
| 3 -> apply3 (Obj.magic o) x
108-
| 4 -> apply4 (Obj.magic o) x
109-
| 5 -> apply5 (Obj.magic o) x
110-
| 6 -> apply6 (Obj.magic o) x
111-
| 7 -> apply7 (Obj.magic o) x
112-
| _ -> (fun a -> app o [|x; a |]))
113-
114-
115-
116-
(** in practice we can unify
117-
{[_1, _2, _3 ]} into a single function
118-
{[_]}, however, it might slow down a bit
119-
*)
120-
let _1 o x =
121-
let len = function_length o in
122-
if len = 1 || len = 0 then apply1 o x
123-
else Obj.magic (curry1 o x len )
124-
125-
126-
let _2 o x y =
127-
let len = function_length o in
128-
if len = 2 then apply2 o x y
129-
else Obj.magic (app o [|x; y|])
130-
131-
132-
133-
let _3 o a0 a1 a2 =
134-
let len = function_length o in
135-
if len = 3 then apply3 o a0 a1 a2
136-
else
137-
Obj.magic (app o [|a0;a1;a2|])
138-
139-
140-
let _4 o a0 a1 a2 a3 =
141-
let len = function_length o in
142-
if len = 4 then apply4 o a0 a1 a2 a3
143-
else
144-
Obj.magic (app o [|a0;a1;a2; a3 |])
145-
146-
let _5 o a0 a1 a2 a3 a4 =
147-
let len = function_length o in
148-
if len = 5 then apply5 o a0 a1 a2 a3 a4
149-
else
150-
Obj.magic (app o [|a0;a1;a2; a3; a4 |])
151-
152-
153-
let _6 o a0 a1 a2 a3 a4 a5 =
154-
let len = function_length o in
155-
if len = 6 then apply6 o a0 a1 a2 a3 a4 a5
156-
else
157-
Obj.magic (app o [|a0;a1;a2; a3; a4; a5 |])
158-
159-
let _7 o a0 a1 a2 a3 a4 a5 a6 =
160-
let len = function_length o in
161-
if len = 7 then apply7 o a0 a1 a2 a3 a4 a5 a6
162-
else
163-
Obj.magic (app o [|a0;a1;a2; a3; a4; a5; a6 |])
164-
165-
let _8 o a0 a1 a2 a3 a4 a5 a6 a7 =
166-
let len = function_length o in
167-
if len = 8 then apply8 o a0 a1 a2 a3 a4 a5 a6 a7
168-
else
169-
Obj.magic (app o [|a0;a1;a2; a3; a4; a5; a6; a7|])
170-
17145
(** For efficiency, [args.(0)] would contain obj as well *)
17246
let js label cacheid obj args =
17347
let meth =
17448
(Obj.magic Caml_oo.caml_get_public_method obj label cacheid) in
17549
app meth args
50+
17651

177-
178-
(* example like [x#hi] *)
179-
let js1 label cacheid obj =
180-
let meth =
181-
(Obj.magic Caml_oo.caml_get_public_method obj label cacheid) in
182-
_1 meth obj
183-
184-
let js2 label cacheid obj a1 =
185-
let meth =
186-
(Obj.magic Caml_oo.caml_get_public_method obj label cacheid) in
187-
_2 meth obj a1
188-
189-
let js3 label cacheid obj a1 a2 =
190-
let meth =
191-
(Obj.magic Caml_oo.caml_get_public_method obj label cacheid) in
192-
_3 meth obj a1 a2
193-
194-
let js4 label cacheid obj a1 a2 a3 =
195-
let meth =
196-
(Obj.magic Caml_oo.caml_get_public_method obj label cacheid) in
197-
_4 meth obj a1 a2 a3
198-
199-
200-
let js5 label cacheid obj a1 a2 a3 a4 =
201-
let meth =
202-
(Obj.magic Caml_oo.caml_get_public_method obj label cacheid) in
203-
_5 meth obj a1 a2 a3 a4
204-
205-
let js6 label cacheid obj a1 a2 a3 a4 a5 =
206-
let meth =
207-
(Obj.magic Caml_oo.caml_get_public_method obj label cacheid) in
208-
_6 meth obj a1 a2 a3 a4 a5
209-
210-
let js7 label cacheid obj a1 a2 a3 a4 a5 a6=
211-
let meth =
212-
(Obj.magic Caml_oo.caml_get_public_method obj label cacheid) in
213-
_7 meth obj a1 a2 a3 a4 a5 a6
214-
215-
let js8 label cacheid obj a1 a2 a3 a4 a5 a6 a7 =
216-
let meth =
217-
(Obj.magic Caml_oo.caml_get_public_method obj label cacheid) in
218-
_8 meth obj a1 a2 a3 a4 a5 a6 a7
219-
220-
221-
222-
223-
52+
external apply1 : ('a0 -> 'a1) -> 'a0 -> 'a1 = "js_apply1"
53+
external apply2 : ('a0 -> 'a1 -> 'a2) -> 'a0 -> 'a1 -> 'a2 = "js_apply2"
54+
external apply3 : ('a0 -> 'a1 -> 'a2 -> 'a3) -> 'a0 -> 'a1 -> 'a2 -> 'a3 = "js_apply3"
55+
external apply4 : ('a0 -> 'a1 -> 'a2 -> 'a3 -> 'a4) -> 'a0 -> 'a1 -> 'a2 -> 'a3 -> 'a4 = "js_apply4"
56+
external apply5 : ('a0 -> 'a1 -> 'a2 -> 'a3 -> 'a4 -> 'a5) -> 'a0 -> 'a1 -> 'a2 -> 'a3 -> 'a4 -> 'a5 = "js_apply5"
57+
external apply6 : ('a0 -> 'a1 -> 'a2 -> 'a3 -> 'a4 -> 'a5 -> 'a6) -> 'a0 -> 'a1 -> 'a2 -> 'a3 -> 'a4 -> 'a5 -> 'a6 = "js_apply6"
58+
external apply7 : ('a0 -> 'a1 -> 'a2 -> 'a3 -> 'a4 -> 'a5 -> 'a6 -> 'a7) -> 'a0 -> 'a1 -> 'a2 -> 'a3 -> 'a4 -> 'a5 -> 'a6 -> 'a7 = "js_apply7"
59+
external apply8 : ('a0 -> 'a1 -> 'a2 -> 'a3 -> 'a4 -> 'a5 -> 'a6 -> 'a7 -> 'a8) -> 'a0 -> 'a1 -> 'a2 -> 'a3 -> 'a4 -> 'a5 -> 'a6 -> 'a7 -> 'a8 = "js_apply8"
60+
61+
let curry_1 o a0 arity =
62+
match arity with
63+
| 0 -> apply1 (Obj.magic o) a0
64+
| 1 -> apply1 (Obj.magic o) a0
65+
| 2 -> apply2 (Obj.magic o) a0
66+
| 3 -> apply3 (Obj.magic o) a0
67+
| 4 -> apply4 (Obj.magic o) a0
68+
| 5 -> apply5 (Obj.magic o) a0
69+
| 6 -> apply6 (Obj.magic o) a0
70+
| 7 -> apply7 (Obj.magic o) a0
71+
| _ -> Obj.magic (app o [|a0|])
72+
73+
let _1 o a0 =
74+
let arity = function_length o in
75+
if arity = 1 then apply1 o a0
76+
else curry_1 o a0 arity
77+
78+
let js1 label cacheid a0 =
79+
_1 (Obj.magic Caml_oo.caml_get_public_method a0 label cacheid) a0
80+
81+
82+
let curry_2 o a0 a1 arity =
83+
match arity with
84+
| 0 -> app (apply1 (Obj.magic o) a0) [|a1|]
85+
| 1 -> app (apply1 (Obj.magic o) a0) [|a1|]
86+
| 2 -> apply2 (Obj.magic o) a0 a1
87+
| 3 -> apply3 (Obj.magic o) a0 a1
88+
| 4 -> apply4 (Obj.magic o) a0 a1
89+
| 5 -> apply5 (Obj.magic o) a0 a1
90+
| 6 -> apply6 (Obj.magic o) a0 a1
91+
| 7 -> apply7 (Obj.magic o) a0 a1
92+
| _ -> Obj.magic (app o [|a0;a1|])
93+
94+
let _2 o a0 a1 =
95+
let arity = function_length o in
96+
if arity = 2 then apply2 o a0 a1
97+
else curry_2 o a0 a1 arity
98+
99+
let js2 label cacheid a0 a1 =
100+
_2 (Obj.magic Caml_oo.caml_get_public_method a0 label cacheid) a0 a1
101+
102+
103+
let curry_3 o a0 a1 a2 arity =
104+
match arity with
105+
| 0 -> app (apply1 (Obj.magic o) a0) [|a1;a2|]
106+
| 1 -> app (apply1 (Obj.magic o) a0) [|a1;a2|]
107+
| 2 -> app (apply2 (Obj.magic o) a0 a1) [|a2|]
108+
| 3 -> apply3 (Obj.magic o) a0 a1 a2
109+
| 4 -> apply4 (Obj.magic o) a0 a1 a2
110+
| 5 -> apply5 (Obj.magic o) a0 a1 a2
111+
| 6 -> apply6 (Obj.magic o) a0 a1 a2
112+
| 7 -> apply7 (Obj.magic o) a0 a1 a2
113+
| _ -> Obj.magic (app o [|a0;a1;a2|])
114+
115+
let _3 o a0 a1 a2 =
116+
let arity = function_length o in
117+
if arity = 3 then apply3 o a0 a1 a2
118+
else curry_3 o a0 a1 a2 arity
119+
120+
let js3 label cacheid a0 a1 a2 =
121+
_3 (Obj.magic Caml_oo.caml_get_public_method a0 label cacheid) a0 a1 a2
122+
123+
124+
let curry_4 o a0 a1 a2 a3 arity =
125+
match arity with
126+
| 0 -> app (apply1 (Obj.magic o) a0) [|a1;a2;a3|]
127+
| 1 -> app (apply1 (Obj.magic o) a0) [|a1;a2;a3|]
128+
| 2 -> app (apply2 (Obj.magic o) a0 a1) [|a2;a3|]
129+
| 3 -> app (apply3 (Obj.magic o) a0 a1 a2) [|a3|]
130+
| 4 -> apply4 (Obj.magic o) a0 a1 a2 a3
131+
| 5 -> apply5 (Obj.magic o) a0 a1 a2 a3
132+
| 6 -> apply6 (Obj.magic o) a0 a1 a2 a3
133+
| 7 -> apply7 (Obj.magic o) a0 a1 a2 a3
134+
| _ -> Obj.magic (app o [|a0;a1;a2;a3|])
135+
136+
let _4 o a0 a1 a2 a3 =
137+
let arity = function_length o in
138+
if arity = 4 then apply4 o a0 a1 a2 a3
139+
else curry_4 o a0 a1 a2 a3 arity
140+
141+
let js4 label cacheid a0 a1 a2 a3 =
142+
_4 (Obj.magic Caml_oo.caml_get_public_method a0 label cacheid) a0 a1 a2 a3
143+
144+
145+
let curry_5 o a0 a1 a2 a3 a4 arity =
146+
match arity with
147+
| 0 -> app (apply1 (Obj.magic o) a0) [|a1;a2;a3;a4|]
148+
| 1 -> app (apply1 (Obj.magic o) a0) [|a1;a2;a3;a4|]
149+
| 2 -> app (apply2 (Obj.magic o) a0 a1) [|a2;a3;a4|]
150+
| 3 -> app (apply3 (Obj.magic o) a0 a1 a2) [|a3;a4|]
151+
| 4 -> app (apply4 (Obj.magic o) a0 a1 a2 a3) [|a4|]
152+
| 5 -> apply5 (Obj.magic o) a0 a1 a2 a3 a4
153+
| 6 -> apply6 (Obj.magic o) a0 a1 a2 a3 a4
154+
| 7 -> apply7 (Obj.magic o) a0 a1 a2 a3 a4
155+
| _ -> Obj.magic (app o [|a0;a1;a2;a3;a4|])
156+
157+
let _5 o a0 a1 a2 a3 a4 =
158+
let arity = function_length o in
159+
if arity = 5 then apply5 o a0 a1 a2 a3 a4
160+
else curry_5 o a0 a1 a2 a3 a4 arity
161+
162+
let js5 label cacheid a0 a1 a2 a3 a4 =
163+
_5 (Obj.magic Caml_oo.caml_get_public_method a0 label cacheid) a0 a1 a2 a3 a4
164+
165+
166+
let curry_6 o a0 a1 a2 a3 a4 a5 arity =
167+
match arity with
168+
| 0 -> app (apply1 (Obj.magic o) a0) [|a1;a2;a3;a4;a5|]
169+
| 1 -> app (apply1 (Obj.magic o) a0) [|a1;a2;a3;a4;a5|]
170+
| 2 -> app (apply2 (Obj.magic o) a0 a1) [|a2;a3;a4;a5|]
171+
| 3 -> app (apply3 (Obj.magic o) a0 a1 a2) [|a3;a4;a5|]
172+
| 4 -> app (apply4 (Obj.magic o) a0 a1 a2 a3) [|a4;a5|]
173+
| 5 -> app (apply5 (Obj.magic o) a0 a1 a2 a3 a4) [|a5|]
174+
| 6 -> apply6 (Obj.magic o) a0 a1 a2 a3 a4 a5
175+
| 7 -> apply7 (Obj.magic o) a0 a1 a2 a3 a4 a5
176+
| _ -> Obj.magic (app o [|a0;a1;a2;a3;a4;a5|])
177+
178+
let _6 o a0 a1 a2 a3 a4 a5 =
179+
let arity = function_length o in
180+
if arity = 6 then apply6 o a0 a1 a2 a3 a4 a5
181+
else curry_6 o a0 a1 a2 a3 a4 a5 arity
182+
183+
let js6 label cacheid a0 a1 a2 a3 a4 a5 =
184+
_6 (Obj.magic Caml_oo.caml_get_public_method a0 label cacheid) a0 a1 a2 a3 a4 a5
185+
186+
187+
let curry_7 o a0 a1 a2 a3 a4 a5 a6 arity =
188+
match arity with
189+
| 0 -> app (apply1 (Obj.magic o) a0) [|a1;a2;a3;a4;a5;a6|]
190+
| 1 -> app (apply1 (Obj.magic o) a0) [|a1;a2;a3;a4;a5;a6|]
191+
| 2 -> app (apply2 (Obj.magic o) a0 a1) [|a2;a3;a4;a5;a6|]
192+
| 3 -> app (apply3 (Obj.magic o) a0 a1 a2) [|a3;a4;a5;a6|]
193+
| 4 -> app (apply4 (Obj.magic o) a0 a1 a2 a3) [|a4;a5;a6|]
194+
| 5 -> app (apply5 (Obj.magic o) a0 a1 a2 a3 a4) [|a5;a6|]
195+
| 6 -> app (apply6 (Obj.magic o) a0 a1 a2 a3 a4 a5) [|a6|]
196+
| 7 -> apply7 (Obj.magic o) a0 a1 a2 a3 a4 a5 a6
197+
| _ -> Obj.magic (app o [|a0;a1;a2;a3;a4;a5;a6|])
198+
199+
let _7 o a0 a1 a2 a3 a4 a5 a6 =
200+
let arity = function_length o in
201+
if arity = 7 then apply7 o a0 a1 a2 a3 a4 a5 a6
202+
else curry_7 o a0 a1 a2 a3 a4 a5 a6 arity
203+
204+
let js7 label cacheid a0 a1 a2 a3 a4 a5 a6 =
205+
_7 (Obj.magic Caml_oo.caml_get_public_method a0 label cacheid) a0 a1 a2 a3 a4 a5 a6
206+
207+
208+
let curry_8 o a0 a1 a2 a3 a4 a5 a6 a7 arity =
209+
match arity with
210+
| 0 -> app (apply1 (Obj.magic o) a0) [|a1;a2;a3;a4;a5;a6;a7|]
211+
| 1 -> app (apply1 (Obj.magic o) a0) [|a1;a2;a3;a4;a5;a6;a7|]
212+
| 2 -> app (apply2 (Obj.magic o) a0 a1) [|a2;a3;a4;a5;a6;a7|]
213+
| 3 -> app (apply3 (Obj.magic o) a0 a1 a2) [|a3;a4;a5;a6;a7|]
214+
| 4 -> app (apply4 (Obj.magic o) a0 a1 a2 a3) [|a4;a5;a6;a7|]
215+
| 5 -> app (apply5 (Obj.magic o) a0 a1 a2 a3 a4) [|a5;a6;a7|]
216+
| 6 -> app (apply6 (Obj.magic o) a0 a1 a2 a3 a4 a5) [|a6;a7|]
217+
| 7 -> app (apply7 (Obj.magic o) a0 a1 a2 a3 a4 a5 a6) [|a7|]
218+
| _ -> Obj.magic (app o [|a0;a1;a2;a3;a4;a5;a6;a7|])
219+
220+
let _8 o a0 a1 a2 a3 a4 a5 a6 a7 =
221+
let arity = function_length o in
222+
if arity = 8 then apply8 o a0 a1 a2 a3 a4 a5 a6 a7
223+
else curry_8 o a0 a1 a2 a3 a4 a5 a6 a7 arity
224+
225+
let js8 label cacheid a0 a1 a2 a3 a4 a5 a6 a7 =
226+
_8 (Obj.magic Caml_oo.caml_get_public_method a0 label cacheid) a0 a1 a2 a3 a4 a5 a6 a7
224227

0 commit comments

Comments
 (0)