diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b4deda7..222470b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/Core__Date.mjs b/src/Core__Date.mjs index bb84ed38..38e6e98f 100644 --- a/src/Core__Date.mjs +++ b/src/Core__Date.mjs @@ -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 = {}; @@ -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 { diff --git a/src/Core__Float.mjs b/src/Core__Float.mjs index 44a3d032..9fb41d4c 100644 --- a/src/Core__Float.mjs +++ b/src/Core__Float.mjs @@ -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)) { @@ -37,8 +23,6 @@ function clamp(min, max, value) { export { Constants , - equal , - compare , fromString , clamp , } diff --git a/src/Core__Float.res b/src/Core__Float.res index ada4de71..c701db92 100644 --- a/src/Core__Float.res +++ b/src/Core__Float.res @@ -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" diff --git a/src/Core__Float.resi b/src/Core__Float.resi index 9609147e..806d8df9 100644 --- a/src/Core__Float.resi +++ b/src/Core__Float.resi @@ -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`. diff --git a/src/Core__Int.mjs b/src/Core__Int.mjs index 96f246a0..7900b26f 100644 --- a/src/Core__Int.mjs +++ b/src/Core__Int.mjs @@ -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) { @@ -85,8 +71,6 @@ var Constants = { export { Constants , - equal , - compare , fromString , range , rangeWithOptions , diff --git a/src/Core__Int.res b/src/Core__Int.res index 94d57c62..b1a0f22e 100644 --- a/src/Core__Int.res +++ b/src/Core__Int.res @@ -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 diff --git a/src/Core__Int.resi b/src/Core__Int.resi index 37c44ac3..bb74f75a 100644 --- a/src/Core__Int.resi +++ b/src/Core__Int.resi @@ -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 diff --git a/src/Core__String.mjs b/src/Core__String.mjs index c6f60393..1b8a0b9f 100644 --- a/src/Core__String.mjs +++ b/src/Core__String.mjs @@ -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) { @@ -40,8 +26,6 @@ function searchOpt(s, re) { } export { - equal , - compare , indexOfOpt , lastIndexOfOpt , searchOpt , diff --git a/src/Core__String.res b/src/Core__String.res index 22db37b5..392c3165 100644 --- a/src/Core__String.res +++ b/src/Core__String.res @@ -6,10 +6,9 @@ @val external fromCodePoint: int => string = "String.fromCodePoint" @variadic @val external fromCodePointMany: array => 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 = "" diff --git a/src/Core__String.resi b/src/Core__String.resi index b3e429ba..99e5b6df 100644 --- a/src/Core__String.resi +++ b/src/Core__String.resi @@ -118,9 +118,9 @@ String.fromCodePointMany([0xd55c, 0xae00, 0x1f63a]) == `한글😺` @val external fromCodePointMany: array => 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`. diff --git a/test/FloatTests.mjs b/test/FloatTests.mjs index 013aaabb..10d8de3a 100644 --- a/test/FloatTests.mjs +++ b/test/FloatTests.mjs @@ -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 , } diff --git a/test/FloatTests.res b/test/FloatTests.res index c9f933ca..8ca2d201 100644 --- a/test/FloatTests.res +++ b/test/FloatTests.res @@ -45,3 +45,5 @@ Test.run( eq, neg_infinity, ) + +Test.run(__POS_OF__("Float.equal optimization"), Float.equal(1., 3.), eq, false) diff --git a/test/IntTests.mjs b/test/IntTests.mjs index 593fb9eb..2ab9a0ce 100644 --- a/test/IntTests.mjs +++ b/test/IntTests.mjs @@ -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 , diff --git a/test/IntTests.res b/test/IntTests.res index 4535b18e..ebbb7e8c 100644 --- a/test/IntTests.res +++ b/test/IntTests.res @@ -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) diff --git a/test/StringTests.mjs b/test/StringTests.mjs new file mode 100644 index 00000000..9bb62166 --- /dev/null +++ b/test/StringTests.mjs @@ -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 */ diff --git a/test/StringTests.res b/test/StringTests.res new file mode 100644 index 00000000..a476d408 --- /dev/null +++ b/test/StringTests.res @@ -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)