Skip to content

Commit a9f650a

Browse files
committed
[Belt] Save allocations by manually inlining
1 parent 5ef875b commit a9f650a

File tree

2 files changed

+35
-27
lines changed

2 files changed

+35
-27
lines changed

jscomp/others/belt_Id.ml

+23-9
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ module MakeComparable (M : sig
6060
struct
6161
type identity
6262
type t = M.t
63-
let cmp = (fun[@bs] a b -> M.cmp a b)
63+
(* see https://github.com/BuckleScript/bucklescript/pull/2589/files/5ef875b7665ee08cfdc59af368fc52bac1fe9130#r173330825 *)
64+
let cmp =
65+
let cmp = M.cmp in fun[@bs] a b -> cmp a b
6466
end
6567

6668
let comparableU
@@ -73,8 +75,15 @@ let comparableU
7375
end) in
7476
(module N : Comparable with type t = key)
7577

76-
let comparable cmp =
77-
comparableU (fun[@bs] a b -> cmp a b)
78+
let comparable
79+
(type key)
80+
cmp
81+
=
82+
let module N = MakeComparable(struct
83+
type t = key
84+
let cmp = cmp
85+
end) in
86+
(module N : Comparable with type t = key)
7887

7988
module type Hashable = sig
8089
type identity
@@ -105,8 +114,10 @@ module MakeHashable (M : sig
105114
struct
106115
type identity
107116
type t = M.t
108-
let hash = (fun[@bs] a -> M.hash a)
109-
let eq = (fun[@bs] a b -> M.eq a b)
117+
let hash =
118+
let hash = M.hash in fun[@bs] a -> hash a
119+
let eq =
120+
let eq = M.eq in fun[@bs] a b -> eq a b
110121
end
111122

112123
let hashableU (type key) ~hash ~eq =
@@ -117,7 +128,10 @@ let hashableU (type key) ~hash ~eq =
117128
end) in
118129
(module N : Hashable with type t = key)
119130

120-
let hashable ~hash ~eq =
121-
hashableU
122-
~hash:(fun [@bs] a -> hash a)
123-
~eq:(fun [@bs] a b -> eq a b)
131+
let hashable (type key) ~hash ~eq =
132+
let module N = MakeHashable(struct
133+
type t = key
134+
let hash = hash
135+
let eq = eq
136+
end) in
137+
(module N : Hashable with type t = key)

lib/js/belt_Id.js

+12-18
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,17 @@ function MakeComparableU(M) {
88
}
99

1010
function MakeComparable(M) {
11-
var cmp = function (a, b) {
12-
return Curry._2(M[/* cmp */0], a, b);
13-
};
14-
return /* module */[/* cmp */cmp];
11+
var cmp = M[/* cmp */0];
12+
var cmp$1 = Curry.__2(cmp);
13+
return /* module */[/* cmp */cmp$1];
1514
}
1615

1716
function comparableU(cmp) {
1817
return /* module */[/* cmp */cmp];
1918
}
2019

2120
function comparable(cmp) {
22-
var M = /* module */[/* cmp */Curry.__2(cmp)];
23-
var cmp$1 = M[/* cmp */0];
21+
var cmp$1 = Curry.__2(cmp);
2422
return /* module */[/* cmp */cmp$1];
2523
}
2624

@@ -34,15 +32,13 @@ function MakeHashableU(M) {
3432
}
3533

3634
function MakeHashable(M) {
37-
var hash = function (a) {
38-
return Curry._1(M[/* hash */0], a);
39-
};
40-
var eq = function (a, b) {
41-
return Curry._2(M[/* eq */1], a, b);
42-
};
35+
var hash = M[/* hash */0];
36+
var hash$1 = Curry.__1(hash);
37+
var eq = M[/* eq */1];
38+
var eq$1 = Curry.__2(eq);
4339
return /* module */[
44-
/* hash */hash,
45-
/* eq */eq
40+
/* hash */hash$1,
41+
/* eq */eq$1
4642
];
4743
}
4844

@@ -54,10 +50,8 @@ function hashableU(hash, eq) {
5450
}
5551

5652
function hashable(hash, eq) {
57-
var M_000 = Curry.__1(hash);
58-
var M_001 = Curry.__2(eq);
59-
var hash$1 = M_000;
60-
var eq$1 = M_001;
53+
var hash$1 = Curry.__1(hash);
54+
var eq$1 = Curry.__2(eq);
6155
return /* module */[
6256
/* hash */hash$1,
6357
/* eq */eq$1

0 commit comments

Comments
 (0)