Skip to content

Commit 86a9cfe

Browse files
authored
Merge pull request #4556 from BuckleScript/linear_poly_var
turn on linear poly-var compilation scheme
2 parents ac44d2e + fbd64ab commit 86a9cfe

25 files changed

+2632
-2325
lines changed

jscomp/core/bs_conditional_initial.ml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,9 @@ let setup_env () =
3030
Translcore.wrap_single_field_record := Transl_single_field_record.wrap_single_field_record;
3131
Translmod.eval_rec_bindings := Compile_rec_module.eval_rec_bindings;
3232
Typemod.should_hide := Typemod_hide.should_hide;
33-
#if 0 then
3433
Matching.make_test_sequence_variant_constant := Polyvar_pattern_match.make_test_sequence_variant_constant;
3534
Matching.call_switcher_variant_constant := Polyvar_pattern_match.call_switcher_variant_constant;
3635
Matching.call_switcher_variant_constr := Polyvar_pattern_match.call_switcher_variant_constr;
37-
#end
3836
Clflags.no_std_include := true;
3937
Warnings.parse_options false Bsc_warnings.defaults_w;
4038
Warnings.parse_options true Bsc_warnings.defaults_warn_error;

jscomp/core/polyvar_pattern_match.ml

Lines changed: 30 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -24,62 +24,36 @@
2424

2525
type lam = Lambda.lambda
2626

27+
type hash_names = (int * string) list
2728

28-
module Map_lambda = struct
29-
open Map_gen
30-
type key =
31-
| Bottom of int
32-
| Normalized of lam
29+
type input = (int * (string * lam)) list
30+
type output = (hash_names * lam) list
31+
module Coll = Hash.Make(struct
32+
type t = lam
33+
let equal = Pervasives.(=)
34+
let hash = Hashtbl.hash
35+
end)
36+
type value = {
37+
stamp : int ;
38+
hash_names_act : hash_names * lam
39+
}
3340

34-
let bottom_id = ref (-1)
35-
let bottom () = incr bottom_id ; Bottom !bottom_id
36-
let compare_key (x : key) (y : key) = Pervasives.compare x y
37-
let old_order = ref 1
38-
let next_id () =
39-
incr old_order; !old_order
40-
let rec adjust (tree : _ Map_gen.t as 'a) x replace : 'a =
41-
match tree with
42-
| Empty ->
43-
singleton x (replace None)
44-
| Leaf {k ; v} ->
45-
let c = compare_key x k in
46-
if c = 0 then singleton x (replace (Some v)) else
47-
if c < 0 then
48-
Map_gen.unsafe_two_elements x (replace None) k v
49-
else
50-
Map_gen.unsafe_two_elements k v x (replace None)
51-
| Node ({l; k ; r} as tree) ->
52-
let c = compare_key x k in
53-
if c = 0 then
54-
Map_gen.unsafe_node x (replace (Some tree.v)) l r tree.h
55-
else if c < 0 then
56-
bal (adjust l x replace ) k tree.v r
57-
else
58-
bal l k tree.v (adjust r x replace )
59-
60-
61-
let of_list (int_lambda_list : (int * (string * lam)) list) : (key, (int * string) list * lam * int ) t=
62-
Ext_list.fold_left int_lambda_list empty (fun acc (hash,(name,lam)) ->
63-
let key =
64-
match Lambda.make_key lam with
65-
| None -> bottom ()
66-
| Some key -> Normalized key in
67-
adjust acc key (function
68-
| None -> [hash, name], lam, next_id ()
69-
| Some (acc,action,stamp) -> (hash,name) :: acc, action, stamp
70-
))
71-
let rec values_aux s acc =
72-
match s with
73-
| Empty -> acc
74-
| Leaf {v} -> v :: acc
75-
| Node {l;v;r} ->
76-
values_aux l (v ::values_aux r acc)
77-
let values s : ((int * string) list * lam) list =
78-
Ext_list.sort_via_arrayf ( values_aux s [])
79-
(fun (_,_,d0) (_,_,d1) -> compare d0 d1)
80-
(fun (a,b,_) -> (a,b))
81-
82-
end
41+
let convert (xs : input) : output =
42+
let coll = Coll.create 63 in
43+
let os : value list ref = ref [] in
44+
xs |> List.iteri (fun i (hash,(name,act)) ->
45+
match Lambda.make_key act with
46+
| None -> os := { stamp = i; hash_names_act = ([hash,name],act)} :: !os
47+
| Some key ->
48+
Coll.add_or_update coll key
49+
~update:(fun ({hash_names_act = hash_names, act } as acc) ->
50+
{acc with hash_names_act = (hash,name) :: hash_names, act })
51+
{hash_names_act = [hash,name],act; stamp = i }
52+
);
53+
let result =
54+
Coll.to_list coll (fun _ value -> value )
55+
@ !os in
56+
Ext_list.sort_via_arrayf result (fun x y -> compare x.stamp y.stamp ) (fun x -> x.hash_names_act )
8357

8458
let or_list (arg : lam) (hash_names : (int * string) list) =
8559
match hash_names with
@@ -103,7 +77,7 @@ let make_test_sequence_variant_constant
10377
(fail : lam option) (arg : lam)
10478
(int_lambda_list : (int * (string * lam) ) list) : lam =
10579
let int_lambda_list : ((int * string) list * lam) list =
106-
Map_lambda.(values (of_list int_lambda_list)) in
80+
convert int_lambda_list in
10781
match int_lambda_list, fail with
10882
| (_, act) :: rest, None
10983
| rest, Some act ->
@@ -112,19 +86,6 @@ let make_test_sequence_variant_constant
11286
Lifthenelse (predicate,act1, acc))
11387
| [], None -> assert false
11488

115-
let make_test_sequence_variant_constant_2
116-
(fail : lam option) (arg : lam)
117-
(int_lambda_list : (int * (string * lam) ) list) : lam=
118-
match int_lambda_list, fail with
119-
| (_, (_,act)) :: rest, None
120-
| rest , Some act ->
121-
Ext_list.fold_right rest act (fun (hash1,(name,act1)) acc ->
122-
Lifthenelse (Lprim(Pintcomp Ceq,
123-
[arg; Lconst (Const_pointer(hash1, Pt_variant{name}))], Location.none),
124-
act1, acc
125-
)
126-
)
127-
| [], None -> assert false
12889

12990
let call_switcher_variant_constant
13091
(_loc : Location.t)
@@ -133,8 +94,7 @@ let call_switcher_variant_constant
13394
(int_lambda_list : (int * (string * lam)) list)
13495
(_names : Lambda.switch_names option) =
13596

136-
let int_lambda_list : ((int * string) list * lam) list =
137-
Map_lambda.(values (of_list int_lambda_list)) in
97+
let int_lambda_list = convert int_lambda_list in
13898
match int_lambda_list, fail with
13999
| (_,act) :: rest, None
140100
| rest, Some act ->

jscomp/test/bb.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22

33

44
function f(x) {
5-
if (x !== 98) {
6-
if (x >= 99) {
7-
return "c";
8-
} else {
9-
return "a";
10-
}
11-
} else {
5+
if (x === /* b */98) {
126
return "b";
7+
} else if (x === /* c */99) {
8+
return "c";
9+
} else {
10+
return "a";
1311
}
1412
}
1513

@@ -57,14 +55,12 @@ function test(x) {
5755
Error: new Error()
5856
};
5957
}
60-
if (match !== 98) {
61-
if (match >= 99) {
62-
return "c";
63-
} else {
64-
return "a";
65-
}
66-
} else {
58+
if (match === /* b */98) {
6759
return "b";
60+
} else if (match === /* c */99) {
61+
return "c";
62+
} else {
63+
return "a";
6864
}
6965
}
7066

jscomp/test/const_test.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,12 @@ function fff(x) {
2626
}
2727

2828
function h(x) {
29-
if (x !== 66) {
30-
if (x >= 67) {
31-
return 2;
32-
} else {
33-
return 0;
34-
}
35-
} else {
29+
if (x === /* B */66) {
3630
return 1;
31+
} else if (x === /* C */67) {
32+
return 2;
33+
} else {
34+
return 0;
3735
}
3836
}
3937

jscomp/test/ext_filename_test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ function chop_extension_if_any(fname) {
115115
var os_path_separator_char = Filename.dir_sep.charCodeAt(0);
116116

117117
function relative_path(file_or_dir_1, file_or_dir_2) {
118-
var relevant_dir1 = file_or_dir_1.HASH >= 781515420 ? Curry._1(Filename.dirname, file_or_dir_1.VAL) : file_or_dir_1.VAL;
119-
var relevant_dir2 = file_or_dir_2.HASH >= 781515420 ? Curry._1(Filename.dirname, file_or_dir_2.VAL) : file_or_dir_2.VAL;
118+
var relevant_dir1 = file_or_dir_1.HASH === /* File */781515420 ? Curry._1(Filename.dirname, file_or_dir_1.VAL) : file_or_dir_1.VAL;
119+
var relevant_dir2 = file_or_dir_2.HASH === /* File */781515420 ? Curry._1(Filename.dirname, file_or_dir_2.VAL) : file_or_dir_2.VAL;
120120
var dir1 = Ext_string_test.split(undefined, relevant_dir1, os_path_separator_char);
121121
var dir2 = Ext_string_test.split(undefined, relevant_dir2, os_path_separator_char);
122122
var go = function (_dir1, _dir2) {
@@ -149,13 +149,13 @@ function node_relative_path(node_modules_shorten, file1, dep_file) {
149149
var v = Ext_string_test.find(undefined, Test_literals.node_modules, file2);
150150
var len = file2.length;
151151
if (!(node_modules_shorten && v >= 0)) {
152-
return relative_path(dep_file.HASH >= 781515420 ? ({
152+
return relative_path(dep_file.HASH === /* File */781515420 ? ({
153153
HASH: /* File */781515420,
154154
VAL: absolute_path(dep_file.VAL)
155155
}) : ({
156156
HASH: /* Dir */3405101,
157157
VAL: absolute_path(dep_file.VAL)
158-
}), file1.HASH >= 781515420 ? ({
158+
}), file1.HASH === /* File */781515420 ? ({
159159
HASH: /* File */781515420,
160160
VAL: absolute_path(file1.VAL)
161161
}) : ({

jscomp/test/gpr_1891_test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
var Curry = require("../../lib/js/curry.js");
44

55
function foo(x) {
6-
if (typeof x === "number" || x.HASH !== 3505894 || x.VAL !== 3) {
6+
if (typeof x === "number" || !(x.HASH === /* Foo */3505894 && x.VAL === 3)) {
77
console.log("2");
88
} else {
99
console.log("1");
@@ -12,31 +12,31 @@ function foo(x) {
1212
}
1313

1414
function foo2(x) {
15-
if (typeof x === "number" || x.HASH !== 3505894 || x.VAL !== 3) {
15+
if (typeof x === "number" || !(x.HASH === /* Foo */3505894 && x.VAL === 3)) {
1616
return "xxx";
1717
} else {
1818
return "xxxx";
1919
}
2020
}
2121

2222
function foo3(x) {
23-
if (typeof x === "number" || x.HASH !== 3505894 || x.VAL !== 3) {
23+
if (typeof x === "number" || !(x.HASH === /* Foo */3505894 && x.VAL === 3)) {
2424
return 2;
2525
} else {
2626
return 1;
2727
}
2828
}
2929

3030
function foo4(x, h) {
31-
if (typeof x === "number" || x.HASH !== 3505894 || x.VAL !== 3) {
31+
if (typeof x === "number" || !(x.HASH === /* Foo */3505894 && x.VAL === 3)) {
3232
return ;
3333
} else {
3434
return Curry._1(h, undefined);
3535
}
3636
}
3737

3838
function foo5(x) {
39-
if (typeof x === "number" || x.HASH !== 3505894 || x.VAL !== 3) {
39+
if (typeof x === "number" || !(x.HASH === /* Foo */3505894 && x.VAL === 3)) {
4040
console.log("x");
4141
} else {
4242
console.log("hi");

jscomp/test/gpr_4280_test.js

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -35,29 +35,24 @@ function string(s) {
3535
}
3636

3737
function fn(authState, route) {
38-
var exit = 0;
3938
var onboardingRoute;
4039
if (typeof authState === "number") {
41-
var exit$1 = 0;
40+
var exit = 0;
4241
if (typeof route === "number") {
43-
if (route >= -730831382) {
44-
if (route !== -384135774 && route !== -384133096) {
45-
exit$1 = 3;
46-
} else {
47-
exit = 2;
48-
}
49-
} else if (route !== -799423340 && route < -730831383) {
50-
exit$1 = 3;
51-
} else {
52-
exit = 2;
42+
if (route === /* SignUp */-384133096 || route === /* SignIn */-384135774 || route === /* Invite */-730831383 || route === /* PasswordReset */-799423340) {
43+
div({
44+
hd: string("LoggedOut"),
45+
tl: /* [] */0
46+
}, undefined);
47+
return 1;
5348
}
54-
} else if (route.HASH !== 378129979) {
55-
exit$1 = 3;
56-
} else {
49+
exit = 2;
50+
} else if (route.HASH === /* Onboarding */378129979) {
5751
onboardingRoute = route.VAL;
58-
exit = 1;
52+
} else {
53+
exit = 2;
5954
}
60-
if (exit$1 === 3) {
55+
if (exit === 2) {
6156
div({
6257
hd: string("Redirect"),
6358
tl: /* [] */0
@@ -66,14 +61,13 @@ function fn(authState, route) {
6661
}
6762

6863
} else {
69-
var exit$2 = 0;
70-
if (typeof route === "number" || route.HASH !== 378129979) {
71-
exit$2 = 3;
64+
var exit$1 = 0;
65+
if (typeof route === "number" || route.HASH !== /* Onboarding */378129979) {
66+
exit$1 = 2;
7267
} else {
7368
onboardingRoute = route.VAL;
74-
exit = 1;
7569
}
76-
if (exit$2 === 3) {
70+
if (exit$1 === 2) {
7771
console.log(authState.VAL);
7872
div({
7973
hd: string("VerifyEmail"),
@@ -83,22 +77,12 @@ function fn(authState, route) {
8377
}
8478

8579
}
86-
switch (exit) {
87-
case 1 :
88-
console.log(onboardingRoute);
89-
div({
90-
hd: string("Onboarding"),
91-
tl: /* [] */0
92-
}, undefined);
93-
return 0;
94-
case 2 :
95-
div({
96-
hd: string("LoggedOut"),
97-
tl: /* [] */0
98-
}, undefined);
99-
return 1;
100-
101-
}
80+
console.log(onboardingRoute);
81+
div({
82+
hd: string("Onboarding"),
83+
tl: /* [] */0
84+
}, undefined);
85+
return 0;
10286
}
10387

10488
eq("File \"gpr_4280_test.ml\", line 46, characters 6-13", fn(/* Unauthenticated */-54822762, /* Invite */-730831383), 1);

0 commit comments

Comments
 (0)