Skip to content

Commit 6d9eedf

Browse files
authored
Use %array_length for Array.length to bring back lost optimizations (#7872)
* Use `%array_length` for `Array.length` to bring back lost optimizations * CHANGELOG
1 parent 34d78c4 commit 6d9eedf

File tree

8 files changed

+12
-21
lines changed

8 files changed

+12
-21
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
#### :nail_care: Polish
3333

34+
- Reactivate optimization for length of array literals. https://github.com/rescript-lang/rescript/pull/7872
35+
3436
#### :house: Internal
3537

3638
- Playground: Add config options for experimental features and jsx preserve mode. https://github.com/rescript-lang/rescript/pull/7865

packages/@rescript/runtime/Js_array.res

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@ Js.Array.isArray("abcd") == false
9797
/**
9898
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.
9999
*/
100-
@get
101-
external length: array<'a> => int = "length"
100+
external length: array<'a> => int = "%array_length"
102101

103102
/* Mutator functions */
104103

packages/@rescript/runtime/Js_array2.res

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,7 @@ Returns the number of elements in the array. See
120120
[`Array.length`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length)
121121
on MDN.
122122
*/
123-
@get
124-
external length: array<'a> => int = "length"
123+
external length: array<'a> => int = "%array_length"
125124

126125
/* Mutator functions */
127126

packages/@rescript/runtime/Stdlib_Array.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ let fromInitializer = (~length, f) =>
4242

4343
@val external isArray: 'a => bool = "Array.isArray"
4444

45-
@get external length: array<'a> => int = "length"
45+
external length: array<'a> => int = "%array_length"
4646

4747
let isEmpty = arr => arr->length === 0
4848

packages/@rescript/runtime/Stdlib_Array.resi

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ let someArray = ["hi", "hello"]
7777
someArray->Array.length == 2
7878
```
7979
*/
80-
@get
81-
external length: array<'a> => int = "length"
80+
external length: array<'a> => int = "%array_length"
8281

8382
/**
8483
`isEmpty(array)` returns `true` if the array is empty (has length 0), `false` otherwise.

tests/docstring_tests/DocTest.res.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/tests/src/array_subtle_test.mjs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function f(v) {
2121
}
2222

2323
function fff(x) {
24-
return x.length >= 0;
24+
return true;
2525
}
2626

2727
function fff2(x) {
@@ -33,15 +33,11 @@ function fff2(x) {
3333
}
3434

3535
function fff3(x) {
36-
if (x.length >= 0) {
37-
return 1;
38-
} else {
39-
return 2;
40-
}
36+
return 1;
4137
}
4238

4339
function fff4(x) {
44-
if (x.length > 0) {
40+
if (x.length !== 0) {
4541
return 1;
4642
} else {
4743
return 2;
@@ -87,7 +83,7 @@ Mocha.describe("Array_subtle_test", () => {
8783
3,
8884
3
8985
];
90-
while (v.length > 0) {
86+
while (v.length !== 0) {
9187
v.pop();
9288
};
9389
Test_utils.eq("File \"array_subtle_test.res\", line 60, characters 7-14", 0, v.length);

tests/tests/src/js_array_test.mjs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@ import * as Test_utils from "./test_utils.mjs";
66
Mocha.describe("Js_array_test", () => {
77
Mocha.test("isArray_array", () => Test_utils.eq("File \"js_array_test.res\", line 25, characters 7-14", true, Array.isArray([])));
88
Mocha.test("isArray_int", () => Test_utils.eq("File \"js_array_test.res\", line 28, characters 7-14", false, Array.isArray(34)));
9-
Mocha.test("length", () => Test_utils.eq("File \"js_array_test.res\", line 31, characters 7-14", 3, [
10-
1,
11-
2,
12-
3
13-
].length));
9+
Mocha.test("length", () => Test_utils.eq("File \"js_array_test.res\", line 31, characters 7-14", 3, 3));
1410
Mocha.test("copyWithin", () => Test_utils.eq("File \"js_array_test.res\", line 35, characters 7-14", [
1511
1,
1612
2,

0 commit comments

Comments
 (0)