Skip to content

Commit 2d4cefa

Browse files
committed
[JSC] JSCellButterfly::createFromArray should share the butterfly of a CopyOnWriteArrayWithContiguous array
https://bugs.webkit.org/show_bug.cgi?id=329640 Reviewed by Yusuke Suzuki. createFromArray has a path which reuses the JSCellButterfly of the input when it is a CopyOnWriteArrayWithContiguous array. But it tested isCopyOnWrite() against a value masked with IndexingShapeMask, which drops the CopyOnWrite bit, so the path was never taken and we always allocated and copied. This patch tests indexingMode() instead, as DFG and FTL Spread already do. This affects LLInt, Baseline, and operationSpreadGeneric in DFG and FTL. Baseline Patched spread-copy-on-write-contiguous-array 96.4047+-1.7865 ^ 24.7634+-0.7149 ^ definitely 3.8930x faster Tests: JSTests/microbenchmarks/spread-copy-on-write-contiguous-array.js JSTests/stress/spread-copy-on-write-contiguous-array.js * JSTests/microbenchmarks/spread-copy-on-write-contiguous-array.js: Added. (test): * JSTests/stress/spread-copy-on-write-contiguous-array.js: Added. (shouldBe): (collect): (Collector): (spreadToArray): (spreadToArrayWithOthers): (spreadToCall): (spreadToCallWithOthers): (spreadToConstruct): (test): (keepAlive): * Source/JavaScriptCore/runtime/JSCellButterfly.h: (JSC::JSCellButterfly::createFromArray): Canonical link: https://commits.webkit.org/325417@main
1 parent eae6b25 commit 2d4cefa

3 files changed

Lines changed: 130 additions & 1 deletion

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function test(iterable) {
2+
return [...iterable];
3+
}
4+
noInline(test);
5+
6+
const object = {};
7+
for (let i = 0; i < 64; i++)
8+
object["key" + i] = i;
9+
Object.keys(object);
10+
const keys = Object.keys(object);
11+
12+
for (let i = 0; i < 1e6; i++) {
13+
test(keys);
14+
test("a");
15+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
function shouldBe(actual, expected) {
2+
if (actual !== expected)
3+
throw new Error(`bad value: ${String(actual)}, expected: ${String(expected)}`);
4+
}
5+
6+
function shouldBeArray(actual, expected) {
7+
shouldBe(actual.length, expected.length);
8+
for (let i = 0; i < expected.length; ++i)
9+
shouldBe(actual[i], expected[i]);
10+
}
11+
12+
function collect() { return Array.prototype.slice.call(arguments); }
13+
noInline(collect);
14+
15+
class Collector {
16+
constructor() { this.values = Array.prototype.slice.call(arguments); }
17+
}
18+
19+
function spreadToArray(array) { return [...array]; }
20+
noInline(spreadToArray);
21+
22+
function spreadToArrayWithOthers(array) { return [0, ...array, ...array]; }
23+
noInline(spreadToArrayWithOthers);
24+
25+
function spreadToCall(array) { return collect(...array); }
26+
noInline(spreadToCall);
27+
28+
function spreadToCallWithOthers(array) { return collect(0, ...array); }
29+
noInline(spreadToCallWithOthers);
30+
31+
function spreadToConstruct(array) { return new Collector(...array).values; }
32+
noInline(spreadToConstruct);
33+
34+
const object = { a: 1, b: 2, c: 3 };
35+
Object.keys(object);
36+
37+
const factories = [
38+
() => ["a", 1, "b", 2.5, null, undefined],
39+
() => ["a", "b", "c"],
40+
() => [object, "b"],
41+
() => [...[1, 2, 3]],
42+
() => [...new Set(["a", 1])],
43+
() => [..."abc"],
44+
() => [...[1, , 3]],
45+
() => Object.keys(object),
46+
() => "a,b,c".split(","),
47+
() => ["a", 1, "b"].slice(),
48+
() => ["a", 1, "b"].concat([]),
49+
];
50+
51+
const mutations = [
52+
array => { array[0] = "changed"; },
53+
array => { array.push("pushed"); },
54+
array => { array.pop(); },
55+
array => { array.length = 1; },
56+
array => { delete array[1]; },
57+
array => { array.reverse(); },
58+
array => { array.fill(42); },
59+
array => { array.shift(); },
60+
array => { array[10] = "far"; },
61+
];
62+
63+
function test(factory, mutate) {
64+
const source = factory();
65+
const isCopyOnWrite = $vm.indexingMode(source) === "CopyOnWriteArrayWithContiguous";
66+
const expected = [];
67+
for (let i = 0; i < source.length; ++i)
68+
expected.push(source[i]);
69+
70+
const first = spreadToArray(source);
71+
const second = spreadToArray(source);
72+
if (isCopyOnWrite) {
73+
shouldBe($vm.deltaBetweenButterflies(first, source), 0);
74+
shouldBe($vm.deltaBetweenButterflies(second, source), 0);
75+
}
76+
shouldBeArray(first, expected);
77+
shouldBeArray(spreadToArrayWithOthers(source), [0, ...expected, ...expected]);
78+
shouldBeArray(spreadToCall(source), expected);
79+
shouldBeArray(spreadToCallWithOthers(source), [0, ...expected]);
80+
shouldBeArray(spreadToConstruct(source), expected);
81+
82+
mutate(first);
83+
shouldBeArray(source, expected);
84+
shouldBeArray(second, expected);
85+
86+
mutate(source);
87+
shouldBeArray(second, expected);
88+
shouldBeArray(spreadToArray(second), expected);
89+
shouldBeArray(spreadToCall(second), expected);
90+
shouldBeArray(spreadToArray(source), first);
91+
}
92+
noInline(test);
93+
94+
for (let i = 0; i < testLoopCount; ++i)
95+
test(factories[i % factories.length], mutations[i % mutations.length]);
96+
97+
for (const factory of factories) {
98+
for (const mutate of mutations)
99+
test(factory, mutate);
100+
}
101+
102+
function keepAlive() { return spreadToArray(["a", 1, { value: 42 }]); }
103+
noInline(keepAlive);
104+
105+
const kept = [];
106+
for (let i = 0; i < 100; ++i)
107+
kept.push(keepAlive());
108+
gc();
109+
for (const array of kept) {
110+
shouldBe(array.length, 3);
111+
shouldBe(array[0], "a");
112+
shouldBe(array[1], 1);
113+
shouldBe(array[2].value, 42);
114+
}

‎Source/JavaScriptCore/runtime/JSCellButterfly.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class JSCellButterfly : public JSCell {
9393
unsigned length = array->length();
9494

9595
// FIXME: JSCellButterfly::createFromArray should support re-using non contiguous indexing types as well.
96-
if (isCopyOnWrite(indexingType)) {
96+
if (isCopyOnWrite(array->indexingMode())) {
9797
if (hasContiguous(indexingType))
9898
return JSCellButterfly::fromButterfly(array->butterfly());
9999
}

0 commit comments

Comments
 (0)