From f4782fc6c9165950fb585698035ce93fe50b83fd Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Wed, 10 Sep 2025 17:08:06 +0200 Subject: [PATCH 1/2] Use `%array_length` for `Array.length` to bring back lost optimizations --- packages/@rescript/runtime/Js_array.res | 3 +-- packages/@rescript/runtime/Js_array2.res | 3 +-- packages/@rescript/runtime/Stdlib_Array.res | 2 +- packages/@rescript/runtime/Stdlib_Array.resi | 3 +-- tests/docstring_tests/DocTest.res.js | 2 +- tests/tests/src/array_subtle_test.mjs | 12 ++++-------- tests/tests/src/js_array_test.mjs | 6 +----- 7 files changed, 10 insertions(+), 21 deletions(-) diff --git a/packages/@rescript/runtime/Js_array.res b/packages/@rescript/runtime/Js_array.res index 3319d8ede9..0d352afe7a 100644 --- a/packages/@rescript/runtime/Js_array.res +++ b/packages/@rescript/runtime/Js_array.res @@ -97,8 +97,7 @@ Js.Array.isArray("abcd") == false /** Returns the number of elements in the array. See [`Array.length`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length) on MDN. */ -@get -external length: array<'a> => int = "length" +external length: array<'a> => int = "%array_length" /* Mutator functions */ diff --git a/packages/@rescript/runtime/Js_array2.res b/packages/@rescript/runtime/Js_array2.res index 78b7afdf3a..04215f24cf 100644 --- a/packages/@rescript/runtime/Js_array2.res +++ b/packages/@rescript/runtime/Js_array2.res @@ -120,8 +120,7 @@ Returns the number of elements in the array. See [`Array.length`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length) on MDN. */ -@get -external length: array<'a> => int = "length" +external length: array<'a> => int = "%array_length" /* Mutator functions */ diff --git a/packages/@rescript/runtime/Stdlib_Array.res b/packages/@rescript/runtime/Stdlib_Array.res index 6591ae8928..b73ecd0587 100644 --- a/packages/@rescript/runtime/Stdlib_Array.res +++ b/packages/@rescript/runtime/Stdlib_Array.res @@ -42,7 +42,7 @@ let fromInitializer = (~length, f) => @val external isArray: 'a => bool = "Array.isArray" -@get external length: array<'a> => int = "length" +external length: array<'a> => int = "%array_length" let isEmpty = arr => arr->length === 0 diff --git a/packages/@rescript/runtime/Stdlib_Array.resi b/packages/@rescript/runtime/Stdlib_Array.resi index 99dc5aecfd..0a52fcc888 100644 --- a/packages/@rescript/runtime/Stdlib_Array.resi +++ b/packages/@rescript/runtime/Stdlib_Array.resi @@ -77,8 +77,7 @@ let someArray = ["hi", "hello"] someArray->Array.length == 2 ``` */ -@get -external length: array<'a> => int = "length" +external length: array<'a> => int = "%array_length" /** `isEmpty(array)` returns `true` if the array is empty (has length 0), `false` otherwise. diff --git a/tests/docstring_tests/DocTest.res.js b/tests/docstring_tests/DocTest.res.js index d71a5f1cdc..cd00b651ac 100644 --- a/tests/docstring_tests/DocTest.res.js +++ b/tests/docstring_tests/DocTest.res.js @@ -135,7 +135,7 @@ async function main() { })`; } }); - if (codeExamples.length <= 0) { + if (codeExamples.length === 0) { return; } let content = `describe("` + key + `", () => { diff --git a/tests/tests/src/array_subtle_test.mjs b/tests/tests/src/array_subtle_test.mjs index d8e279257c..9e6a7a51f8 100644 --- a/tests/tests/src/array_subtle_test.mjs +++ b/tests/tests/src/array_subtle_test.mjs @@ -21,7 +21,7 @@ function f(v) { } function fff(x) { - return x.length >= 0; + return true; } function fff2(x) { @@ -33,15 +33,11 @@ function fff2(x) { } function fff3(x) { - if (x.length >= 0) { - return 1; - } else { - return 2; - } + return 1; } function fff4(x) { - if (x.length > 0) { + if (x.length !== 0) { return 1; } else { return 2; @@ -87,7 +83,7 @@ Mocha.describe("Array_subtle_test", () => { 3, 3 ]; - while (v.length > 0) { + while (v.length !== 0) { v.pop(); }; Test_utils.eq("File \"array_subtle_test.res\", line 60, characters 7-14", 0, v.length); diff --git a/tests/tests/src/js_array_test.mjs b/tests/tests/src/js_array_test.mjs index 068b95d1fa..5e31136af2 100644 --- a/tests/tests/src/js_array_test.mjs +++ b/tests/tests/src/js_array_test.mjs @@ -6,11 +6,7 @@ import * as Test_utils from "./test_utils.mjs"; Mocha.describe("Js_array_test", () => { Mocha.test("isArray_array", () => Test_utils.eq("File \"js_array_test.res\", line 25, characters 7-14", true, Array.isArray([]))); Mocha.test("isArray_int", () => Test_utils.eq("File \"js_array_test.res\", line 28, characters 7-14", false, Array.isArray(34))); - Mocha.test("length", () => Test_utils.eq("File \"js_array_test.res\", line 31, characters 7-14", 3, [ - 1, - 2, - 3 - ].length)); + Mocha.test("length", () => Test_utils.eq("File \"js_array_test.res\", line 31, characters 7-14", 3, 3)); Mocha.test("copyWithin", () => Test_utils.eq("File \"js_array_test.res\", line 35, characters 7-14", [ 1, 2, From 77db0347cebeffb3e445c238f7871b69e7243e25 Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Wed, 10 Sep 2025 18:02:16 +0200 Subject: [PATCH 2/2] CHANGELOG --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index afa8aceb45..0a69f1052b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,8 @@ #### :nail_care: Polish +- Reactivate optimization for length of array literals. https://github.com/rescript-lang/rescript/pull/7872 + #### :house: Internal - Playground: Add config options for experimental features and jsx preserve mode. https://github.com/rescript-lang/rescript/pull/7865