Skip to content

Commit

Permalink
Optimize compare and equal functions (#238)
Browse files Browse the repository at this point in the history
* Optimize compare and equal functions

* Changelog

* Add tests
  • Loading branch information
fhammerschmidt authored Jul 1, 2024
1 parent 3eb53c8 commit 22642ea
Show file tree
Hide file tree
Showing 17 changed files with 66 additions and 65 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Next version

- Optimize compare and equal functions. https://github.com/rescript-association/rescript-core/pull/238

## 1.5.2

- Remove aliases for runtime modules (`MapperRt`, `Internal`) and `Re`. https://github.com/rescript-association/rescript-core/pull/237
Expand Down
4 changes: 2 additions & 2 deletions src/Core__Date.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Generated by ReScript, PLEASE EDIT WITH CARE

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

var UTC = {};

Expand All @@ -9,7 +9,7 @@ function equal(a, b) {
}

function compare(a, b) {
return Core__Float.compare(a.getTime(), b.getTime());
return Caml.float_compare(a.getTime(), b.getTime());
}

export {
Expand Down
16 changes: 0 additions & 16 deletions src/Core__Float.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,6 @@

var Constants = {};

function equal(a, b) {
return a === b;
}

function compare(a, b) {
if (a < b) {
return -1;
} else if (a > b) {
return 1;
} else {
return 0;
}
}

function fromString(i) {
var i$1 = parseFloat(i);
if (isNaN(i$1)) {
Expand All @@ -37,8 +23,6 @@ function clamp(min, max, value) {

export {
Constants ,
equal ,
compare ,
fromString ,
clamp ,
}
Expand Down
5 changes: 2 additions & 3 deletions src/Core__Float.res
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ module Constants = {
@val external maxValue: float = "Number.MAX_VALUE"
}

let equal = (a: float, b: float) => a === b
external equal: (float, float) => bool = "%equal"

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

@val external isNaN: float => bool = "isNaN"
@val external isFinite: float => bool = "isFinite"
Expand Down
4 changes: 2 additions & 2 deletions src/Core__Float.resi
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ module Constants: {
external maxValue: float = "Number.MAX_VALUE"
}

let equal: (float, float) => bool
external equal: (float, float) => bool = "%equal"

let compare: (float, float) => Core__Ordering.t
external compare: (float, float) => Core__Ordering.t = "%compare"

/**
`isNaN(v)` tests if the given `v` is `NaN`.
Expand Down
16 changes: 0 additions & 16 deletions src/Core__Int.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,6 @@

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

function equal(a, b) {
return a === b;
}

function compare(a, b) {
if (a < b) {
return -1;
} else if (a > b) {
return 1;
} else {
return 0;
}
}

function fromString(x, radix) {
var maybeInt = radix !== undefined ? parseInt(x, radix) : parseInt(x);
if (isNaN(maybeInt) || maybeInt > 2147483647 || maybeInt < -2147483648) {
Expand Down Expand Up @@ -85,8 +71,6 @@ var Constants = {

export {
Constants ,
equal ,
compare ,
fromString ,
range ,
rangeWithOptions ,
Expand Down
5 changes: 2 additions & 3 deletions src/Core__Int.res
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ module Constants = {
@inline let maxValue = 2147483647
}

let equal = (a: int, b: int) => a === b
external equal: (int, int) => bool = "%equal"

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

@send external toExponential: (int, ~digits: int=?) => string = "toExponential"
@deprecated("Use `toExponential` instead") @send
Expand Down
4 changes: 2 additions & 2 deletions src/Core__Int.resi
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ module Constants: {
let maxValue: int
}

let equal: (int, int) => bool
external equal: (int, int) => bool = "%equal"

let compare: (int, int) => Core__Ordering.t
external compare: (int, int) => Core__Ordering.t = "%compare"

/**
`toExponential(n, ~digits=?)` return a `string` representing the given value in
Expand Down
16 changes: 0 additions & 16 deletions src/Core__String.mjs
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
// Generated by ReScript, PLEASE EDIT WITH CARE


function equal(a, b) {
return a === b;
}

function compare(a, b) {
if (a < b) {
return -1;
} else if (a > b) {
return 1;
} else {
return 0;
}
}

function indexOfOpt(s, search) {
var index = s.indexOf(search);
if (index !== -1) {
Expand All @@ -40,8 +26,6 @@ function searchOpt(s, re) {
}

export {
equal ,
compare ,
indexOfOpt ,
lastIndexOfOpt ,
searchOpt ,
Expand Down
5 changes: 2 additions & 3 deletions src/Core__String.res
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
@val external fromCodePoint: int => string = "String.fromCodePoint"
@variadic @val external fromCodePointMany: array<int> => string = "String.fromCodePoint"

let equal = (a: string, b: string) => a === b
external equal: (string, string) => bool = "%equal"

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

@get external length: string => int = "length"
@get_index external get: (string, int) => option<string> = ""
Expand Down
4 changes: 2 additions & 2 deletions src/Core__String.resi
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ String.fromCodePointMany([0xd55c, 0xae00, 0x1f63a]) == `한글😺`
@val
external fromCodePointMany: array<int> => string = "String.fromCodePoint"

let equal: (string, string) => bool
external equal: (string, string) => bool = "%equal"

let compare: (string, string) => Core__Ordering.t
external compare: (string, string) => Core__Ordering.t = "%compare"

/**
`length(str)` returns the length of the given `string`.
Expand Down
10 changes: 10 additions & 0 deletions test/FloatTests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,16 @@ Test.run([
"clamp - min -infinity, max -infinity"
], Core__Float.clamp(PervasivesU.neg_infinity, PervasivesU.neg_infinity, 4.2), eq, PervasivesU.neg_infinity);

Test.run([
[
"FloatTests.res",
49,
20,
46
],
"Float.equal optimization"
], false, eq, false);

export {
eq ,
}
Expand Down
2 changes: 2 additions & 0 deletions test/FloatTests.res
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,5 @@ Test.run(
eq,
neg_infinity,
)

Test.run(__POS_OF__("Float.equal optimization"), Float.equal(1., 3.), eq, false)
10 changes: 10 additions & 0 deletions test/IntTests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,16 @@ Test.run([
"clamp - > min, > max"
], Core__Int.clamp(40, 40, 42), eq, 40);

Test.run([
[
"IntTests.res",
170,
20,
44
],
"Int.equal optimization"
], false, eq, false);

export {
eq ,
$$catch ,
Expand Down
2 changes: 2 additions & 0 deletions test/IntTests.res
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,5 @@ Test.run(__POS_OF__("clamp - < min, < max"), Int.clamp(~min=50, ~max=60, 42), eq
Test.run(__POS_OF__("clamp - < min, > max"), Int.clamp(~min=50, ~max=40, 42), eq, 50) // min wins
Test.run(__POS_OF__("clamp - > min, < max"), Int.clamp(~min=40, ~max=60, 42), eq, 42)
Test.run(__POS_OF__("clamp - > min, > max"), Int.clamp(~min=40, ~max=40, 42), eq, 40)

Test.run(__POS_OF__("Int.equal optimization"), Int.equal(1, 3), eq, false)
21 changes: 21 additions & 0 deletions test/StringTests.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Generated by ReScript, PLEASE EDIT WITH CARE

import * as Test from "./Test.mjs";
import * as Caml_obj from "rescript/lib/es6/caml_obj.js";

var eq = Caml_obj.equal;

Test.run([
[
"StringTests.res",
5,
20,
47
],
"String.equal optimization"
], false, eq, false);

export {
eq ,
}
/* Not a pure module */
5 changes: 5 additions & 0 deletions test/StringTests.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
open RescriptCore

let eq = (a, b) => a == b

Test.run(__POS_OF__("String.equal optimization"), String.equal("one", "three"), eq, false)

0 comments on commit 22642ea

Please sign in to comment.