Skip to content

Commit 825e963

Browse files
committed
[JSC] Array literals with only holes should use new_array_with_size
https://bugs.webkit.org/show_bug.cgi?id=328184 Reviewed by Yusuke Suzuki. `[,,]` was compiled to `new_array` with no elements followed by `put_by_id` to `length`. Growing `length` always takes the slow path, so it was much slower than `new Array(2)`. Some minifiers rewrite `new Array(2)` to `[,,]` [1]. This patch emits `new_array_with_size` for array literals which have only holes. Baseline Patched array-literal-with-only-holes 87.5817+-3.9440 ^ 53.0662+-2.5819 ^ definitely 1.6504x faster [1]: oxc-project/oxc#27853 Tests: JSTests/microbenchmarks/array-literal-with-only-holes.js JSTests/stress/array-literal-with-only-holes-having-a-bad-time.js JSTests/stress/array-literal-with-only-holes.js * JSTests/microbenchmarks/array-literal-with-only-holes.js: Added. (test): * JSTests/stress/array-literal-with-only-holes-having-a-bad-time.js: Added. (shouldBe): (test): * JSTests/stress/array-literal-with-only-holes.js: Added. (shouldBe): (one): (two): (seven): (fillInt32): (fillDouble): (fillContiguous): (fillMixed): (ignored): (grow): (leading): (trailing): (checkHoles): (globalThis.Array): * Source/JavaScriptCore/bytecompiler/NodesCodegen.cpp: (JSC::ArrayNode::emitBytecode): Canonical link: https://commits.webkit.org/324314@main
1 parent de4efbb commit 825e963

4 files changed

Lines changed: 201 additions & 0 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function test(x) {
2+
let pair = [,,];
3+
pair[0] = x + 0.5;
4+
pair[1] = x - 0.5;
5+
let quad = [,,,,];
6+
quad[0] = x;
7+
quad[3] = x;
8+
return pair[0] + pair[1] + quad.length;
9+
}
10+
noInline(test);
11+
12+
let result = 0;
13+
for (let i = 0; i < 2e6; ++i)
14+
result += test(i);
15+
if (result !== 3999998000000 + 8e6)
16+
throw new Error("bad result " + result);
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
function shouldBe(actual, expected) {
2+
if (actual !== expected)
3+
throw new Error("bad value: " + actual + " expected: " + expected);
4+
}
5+
6+
function test(value) {
7+
let array = [,,,];
8+
array[0] = value;
9+
array[1] = value;
10+
return array;
11+
}
12+
noInline(test);
13+
14+
for (let i = 0; i < testLoopCount; ++i) {
15+
let array = test(i);
16+
shouldBe(array.length, 3);
17+
shouldBe(array[0], i);
18+
shouldBe(array[1], i);
19+
shouldBe(2 in array, false);
20+
}
21+
22+
let poked = 0;
23+
Object.defineProperty(Array.prototype, 1, {
24+
get() { return "getter"; },
25+
set(value) { poked++; },
26+
configurable: true,
27+
});
28+
29+
for (let i = 0; i < testLoopCount; ++i) {
30+
let array = test(i);
31+
shouldBe(array.length, 3);
32+
shouldBe(array[0], i);
33+
shouldBe(array[1], "getter");
34+
shouldBe(array.hasOwnProperty(1), false);
35+
shouldBe(2 in array, false);
36+
shouldBe(poked, i + 1);
37+
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
function shouldBe(actual, expected) {
2+
if (actual !== expected)
3+
throw new Error("bad value: " + actual + " expected: " + expected);
4+
}
5+
6+
function one() { return [,]; }
7+
noInline(one);
8+
9+
function two() { return [,,]; }
10+
noInline(two);
11+
12+
function seven() { return [,,,,,,,]; }
13+
noInline(seven);
14+
15+
function fillInt32(value) {
16+
let array = [,,,,];
17+
array[1] = value;
18+
return array;
19+
}
20+
noInline(fillInt32);
21+
22+
function fillDouble(value) {
23+
let array = [,,,,];
24+
array[1] = value + 0.5;
25+
return array;
26+
}
27+
noInline(fillDouble);
28+
29+
function fillContiguous(value) {
30+
let array = [,,,,];
31+
array[1] = String(value);
32+
return array;
33+
}
34+
noInline(fillContiguous);
35+
36+
function fillMixed(value) {
37+
let array = [,,,,];
38+
array[1] = value;
39+
return array;
40+
}
41+
noInline(fillMixed);
42+
43+
function ignored() {
44+
[,,];
45+
return [,,,].length;
46+
}
47+
noInline(ignored);
48+
49+
function grow(value) {
50+
let array = [,,];
51+
array.push(value);
52+
array[5] = value;
53+
return array;
54+
}
55+
noInline(grow);
56+
57+
function leading(value) { return [,,value]; }
58+
noInline(leading);
59+
60+
function trailing(value) { return [value,,,]; }
61+
noInline(trailing);
62+
63+
function checkHoles(array, length, filled) {
64+
shouldBe(Array.isArray(array), true);
65+
shouldBe(Object.getPrototypeOf(array), Array.prototype);
66+
shouldBe(array.length, length);
67+
for (let i = 0; i < length; ++i) {
68+
if (filled.includes(i))
69+
continue;
70+
shouldBe(i in array, false);
71+
shouldBe(array[i], undefined);
72+
}
73+
shouldBe(Object.keys(array).length, filled.length);
74+
}
75+
76+
const mixed = [1, 1.5, "string", { }, undefined, null, 2n];
77+
78+
for (let i = 0; i < testLoopCount; ++i) {
79+
checkHoles(one(), 1, []);
80+
checkHoles(two(), 2, []);
81+
checkHoles(seven(), 7, []);
82+
83+
let int32 = fillInt32(i);
84+
shouldBe(int32[1], i);
85+
checkHoles(int32, 4, [1]);
86+
87+
let double = fillDouble(i);
88+
shouldBe(double[1], i + 0.5);
89+
checkHoles(double, 4, [1]);
90+
91+
let contiguous = fillContiguous(i);
92+
shouldBe(contiguous[1], String(i));
93+
checkHoles(contiguous, 4, [1]);
94+
95+
let value = mixed[i % mixed.length];
96+
let array = fillMixed(value);
97+
shouldBe(array[1], value);
98+
checkHoles(array, 4, [1]);
99+
100+
shouldBe(ignored(), 3);
101+
102+
let grown = grow(i);
103+
shouldBe(grown[2], i);
104+
shouldBe(grown[5], i);
105+
checkHoles(grown, 6, [2, 5]);
106+
107+
let lead = leading(i);
108+
shouldBe(lead[2], i);
109+
checkHoles(lead, 3, [2]);
110+
111+
let trail = trailing(i);
112+
shouldBe(trail[0], i);
113+
checkHoles(trail, 3, [0]);
114+
115+
shouldBe(two() === two(), false);
116+
}
117+
118+
for (let length of [1, 8, 1000, 99999, 100000, 100001, 200000]) {
119+
let array = (0, eval)("[" + ",".repeat(length) + "]");
120+
shouldBe(array.length, length);
121+
shouldBe(0 in array, false);
122+
shouldBe(length - 1 in array, false);
123+
shouldBe(Object.keys(array).length, 0);
124+
array[length - 1] = length;
125+
shouldBe(array[length - 1], length);
126+
shouldBe(array.length, length);
127+
}
128+
129+
Array.prototype[1] = "prototype";
130+
for (let i = 0; i < testLoopCount; ++i) {
131+
let array = two();
132+
shouldBe(array[0], undefined);
133+
shouldBe(array[1], "prototype");
134+
shouldBe(array.hasOwnProperty(1), false);
135+
}
136+
delete Array.prototype[1];
137+
138+
let originalArray = Array;
139+
globalThis.Array = function () { throw new Error("should not be called"); };
140+
for (let i = 0; i < testLoopCount; ++i) {
141+
let array = two();
142+
shouldBe(array.length, 2);
143+
shouldBe(array.constructor, originalArray);
144+
}
145+
globalThis.Array = originalArray;

‎Source/JavaScriptCore/bytecompiler/NodesCodegen.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,9 @@ WTF_ALLOW_UNSAFE_BUFFER_USAGE_END
516516
if (!firstPutElement && !m_elision)
517517
return newArray(generator.finalDestination(dst), m_element, length);
518518

519+
if (!m_element)
520+
return generator.emitNewArrayWithSize(generator.finalDestination(dst), generator.emitLoad(nullptr, jsNumber(m_elision)));
521+
519522
allDenseStrings = false;
520523

521524
if (firstPutElement && firstPutElement->value()->isSpreadExpression()) {

0 commit comments

Comments
 (0)