Skip to content

Commit 22642ea

Browse files
Optimize compare and equal functions (#238)
* Optimize compare and equal functions * Changelog * Add tests
1 parent 3eb53c8 commit 22642ea

17 files changed

+66
-65
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Next version
44

5+
- Optimize compare and equal functions. https://github.com/rescript-association/rescript-core/pull/238
6+
57
## 1.5.2
68

79
- Remove aliases for runtime modules (`MapperRt`, `Internal`) and `Re`. https://github.com/rescript-association/rescript-core/pull/237

src/Core__Date.mjs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Generated by ReScript, PLEASE EDIT WITH CARE
22

3-
import * as Core__Float from "./Core__Float.mjs";
3+
import * as Caml from "rescript/lib/es6/caml.js";
44

55
var UTC = {};
66

@@ -9,7 +9,7 @@ function equal(a, b) {
99
}
1010

1111
function compare(a, b) {
12-
return Core__Float.compare(a.getTime(), b.getTime());
12+
return Caml.float_compare(a.getTime(), b.getTime());
1313
}
1414

1515
export {

src/Core__Float.mjs

-16
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,6 @@
33

44
var Constants = {};
55

6-
function equal(a, b) {
7-
return a === b;
8-
}
9-
10-
function compare(a, b) {
11-
if (a < b) {
12-
return -1;
13-
} else if (a > b) {
14-
return 1;
15-
} else {
16-
return 0;
17-
}
18-
}
19-
206
function fromString(i) {
217
var i$1 = parseFloat(i);
228
if (isNaN(i$1)) {
@@ -37,8 +23,6 @@ function clamp(min, max, value) {
3723

3824
export {
3925
Constants ,
40-
equal ,
41-
compare ,
4226
fromString ,
4327
clamp ,
4428
}

src/Core__Float.res

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ module Constants = {
77
@val external maxValue: float = "Number.MAX_VALUE"
88
}
99

10-
let equal = (a: float, b: float) => a === b
10+
external equal: (float, float) => bool = "%equal"
1111

12-
let compare = (a: float, b: float) =>
13-
a < b ? Core__Ordering.less : a > b ? Core__Ordering.greater : Core__Ordering.equal
12+
external compare: (float, float) => Core__Ordering.t = "%compare"
1413

1514
@val external isNaN: float => bool = "isNaN"
1615
@val external isFinite: float => bool = "isFinite"

src/Core__Float.resi

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ module Constants: {
109109
external maxValue: float = "Number.MAX_VALUE"
110110
}
111111

112-
let equal: (float, float) => bool
112+
external equal: (float, float) => bool = "%equal"
113113

114-
let compare: (float, float) => Core__Ordering.t
114+
external compare: (float, float) => Core__Ordering.t = "%compare"
115115

116116
/**
117117
`isNaN(v)` tests if the given `v` is `NaN`.

src/Core__Int.mjs

-16
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,6 @@
22

33
import * as Core__Array from "./Core__Array.mjs";
44

5-
function equal(a, b) {
6-
return a === b;
7-
}
8-
9-
function compare(a, b) {
10-
if (a < b) {
11-
return -1;
12-
} else if (a > b) {
13-
return 1;
14-
} else {
15-
return 0;
16-
}
17-
}
18-
195
function fromString(x, radix) {
206
var maybeInt = radix !== undefined ? parseInt(x, radix) : parseInt(x);
217
if (isNaN(maybeInt) || maybeInt > 2147483647 || maybeInt < -2147483648) {
@@ -85,8 +71,6 @@ var Constants = {
8571

8672
export {
8773
Constants ,
88-
equal ,
89-
compare ,
9074
fromString ,
9175
range ,
9276
rangeWithOptions ,

src/Core__Int.res

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ module Constants = {
33
@inline let maxValue = 2147483647
44
}
55

6-
let equal = (a: int, b: int) => a === b
6+
external equal: (int, int) => bool = "%equal"
77

8-
let compare = (a: int, b: int) =>
9-
a < b ? Core__Ordering.less : a > b ? Core__Ordering.greater : Core__Ordering.equal
8+
external compare: (int, int) => Core__Ordering.t = "%compare"
109

1110
@send external toExponential: (int, ~digits: int=?) => string = "toExponential"
1211
@deprecated("Use `toExponential` instead") @send

src/Core__Int.resi

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ module Constants: {
5656
let maxValue: int
5757
}
5858

59-
let equal: (int, int) => bool
59+
external equal: (int, int) => bool = "%equal"
6060

61-
let compare: (int, int) => Core__Ordering.t
61+
external compare: (int, int) => Core__Ordering.t = "%compare"
6262

6363
/**
6464
`toExponential(n, ~digits=?)` return a `string` representing the given value in

src/Core__String.mjs

-16
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,6 @@
11
// Generated by ReScript, PLEASE EDIT WITH CARE
22

33

4-
function equal(a, b) {
5-
return a === b;
6-
}
7-
8-
function compare(a, b) {
9-
if (a < b) {
10-
return -1;
11-
} else if (a > b) {
12-
return 1;
13-
} else {
14-
return 0;
15-
}
16-
}
17-
184
function indexOfOpt(s, search) {
195
var index = s.indexOf(search);
206
if (index !== -1) {
@@ -40,8 +26,6 @@ function searchOpt(s, re) {
4026
}
4127

4228
export {
43-
equal ,
44-
compare ,
4529
indexOfOpt ,
4630
lastIndexOfOpt ,
4731
searchOpt ,

src/Core__String.res

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
@val external fromCodePoint: int => string = "String.fromCodePoint"
77
@variadic @val external fromCodePointMany: array<int> => string = "String.fromCodePoint"
88

9-
let equal = (a: string, b: string) => a === b
9+
external equal: (string, string) => bool = "%equal"
1010

11-
let compare = (a: string, b: string) =>
12-
a < b ? Core__Ordering.less : a > b ? Core__Ordering.greater : Core__Ordering.equal
11+
external compare: (string, string) => Core__Ordering.t = "%compare"
1312

1413
@get external length: string => int = "length"
1514
@get_index external get: (string, int) => option<string> = ""

src/Core__String.resi

+2-2
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ String.fromCodePointMany([0xd55c, 0xae00, 0x1f63a]) == `한글😺`
118118
@val
119119
external fromCodePointMany: array<int> => string = "String.fromCodePoint"
120120

121-
let equal: (string, string) => bool
121+
external equal: (string, string) => bool = "%equal"
122122

123-
let compare: (string, string) => Core__Ordering.t
123+
external compare: (string, string) => Core__Ordering.t = "%compare"
124124

125125
/**
126126
`length(str)` returns the length of the given `string`.

test/FloatTests.mjs

+10
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,16 @@ Test.run([
237237
"clamp - min -infinity, max -infinity"
238238
], Core__Float.clamp(PervasivesU.neg_infinity, PervasivesU.neg_infinity, 4.2), eq, PervasivesU.neg_infinity);
239239

240+
Test.run([
241+
[
242+
"FloatTests.res",
243+
49,
244+
20,
245+
46
246+
],
247+
"Float.equal optimization"
248+
], false, eq, false);
249+
240250
export {
241251
eq ,
242252
}

test/FloatTests.res

+2
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,5 @@ Test.run(
4545
eq,
4646
neg_infinity,
4747
)
48+
49+
Test.run(__POS_OF__("Float.equal optimization"), Float.equal(1., 3.), eq, false)

test/IntTests.mjs

+10
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,16 @@ Test.run([
548548
"clamp - > min, > max"
549549
], Core__Int.clamp(40, 40, 42), eq, 40);
550550

551+
Test.run([
552+
[
553+
"IntTests.res",
554+
170,
555+
20,
556+
44
557+
],
558+
"Int.equal optimization"
559+
], false, eq, false);
560+
551561
export {
552562
eq ,
553563
$$catch ,

test/IntTests.res

+2
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,5 @@ Test.run(__POS_OF__("clamp - < min, < max"), Int.clamp(~min=50, ~max=60, 42), eq
166166
Test.run(__POS_OF__("clamp - < min, > max"), Int.clamp(~min=50, ~max=40, 42), eq, 50) // min wins
167167
Test.run(__POS_OF__("clamp - > min, < max"), Int.clamp(~min=40, ~max=60, 42), eq, 42)
168168
Test.run(__POS_OF__("clamp - > min, > max"), Int.clamp(~min=40, ~max=40, 42), eq, 40)
169+
170+
Test.run(__POS_OF__("Int.equal optimization"), Int.equal(1, 3), eq, false)

test/StringTests.mjs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Generated by ReScript, PLEASE EDIT WITH CARE
2+
3+
import * as Test from "./Test.mjs";
4+
import * as Caml_obj from "rescript/lib/es6/caml_obj.js";
5+
6+
var eq = Caml_obj.equal;
7+
8+
Test.run([
9+
[
10+
"StringTests.res",
11+
5,
12+
20,
13+
47
14+
],
15+
"String.equal optimization"
16+
], false, eq, false);
17+
18+
export {
19+
eq ,
20+
}
21+
/* Not a pure module */

test/StringTests.res

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
open RescriptCore
2+
3+
let eq = (a, b) => a == b
4+
5+
Test.run(__POS_OF__("String.equal optimization"), String.equal("one", "three"), eq, false)

0 commit comments

Comments
 (0)