Skip to content

Commit d158e05

Browse files
committed
[JSC] Add fast path for spreading TypedArray
https://bugs.webkit.org/show_bug.cgi?id=327804 Reviewed by Yusuke Suzuki. `[...typedArray]` and `f(...typedArray)` run the iterator protocol for each element, because trySpreadFast only handles JSArray, String, arguments objects and Set. This patch makes trySpreadFast copy the elements directly when iterating the TypedArray is not observable and the TypedArray is neither detached nor out of bounds. slow_path_spread and operationSpreadGeneric both use trySpreadFast, so all tiers get this. Baseline Patched spread-typed-array 550.8211+-16.0416 ^ 45.8387+-0.8682 ^ definitely 12.0165x faster Tests: JSTests/microbenchmarks/spread-typed-array.js JSTests/stress/spread-typed-array-larger-than-4gb.js JSTests/stress/spread-typed-array.js * JSTests/microbenchmarks/spread-typed-array.js: Added. (test): * JSTests/stress/spread-typed-array-larger-than-4gb.js: Added. (shouldThrow): * JSTests/stress/spread-typed-array.js: Added. (shouldBe): (shouldThrow): (spreadWithOthers): (collect): (callSpread): (Collector): (constructSpread): (elements): (makeTypedArray): (shouldBeArray): (shouldThrow.spread.new.DataView.new.ArrayBuffer): (shouldBeArray.typedArray.Symbol.iterator): (shouldBeArray.Derived): (shouldBeArray.DerivedWithIterator.prototype.Symbol.iterator): (shouldBeArray.DerivedWithIterator): (testInRealm): (testInRealm.realm.realm.Uint8Array.prototype.Symbol.iterator): (testInRealm.realm.Object.getPrototypeOf.realm.Uint8Array.prototype.Symbol.iterator): (testInRealm.realm.arrayIteratorPrototype.next): (testInRealm.realm.Symbol.iterator): * Source/JavaScriptCore/runtime/CommonSlowPathsInlines.h: (JSC::CommonSlowPaths::trySpreadFast): * Source/JavaScriptCore/runtime/JSCellButterfly.cpp: (JSC::createFromTypedArrayImpl): (JSC::JSCellButterfly::createFromTypedArray): * Source/JavaScriptCore/runtime/JSCellButterfly.h: Canonical link: https://commits.webkit.org/323941@main
1 parent 290ba26 commit d158e05

6 files changed

Lines changed: 349 additions & 0 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function test(typedArray) {
2+
return [...typedArray];
3+
}
4+
noInline(test);
5+
6+
const typedArray = new Uint8Array(32);
7+
for (let i = 0; i < typedArray.length; i++)
8+
typedArray[i] = i;
9+
10+
for (let i = 0; i < 1e6; i++)
11+
test(typedArray);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//@ memoryHog!
2+
//@ skip if $addressBits <= 32
3+
//@ runDefault
4+
5+
function shouldThrow(func, errorType) {
6+
let error;
7+
try {
8+
func();
9+
} catch (e) {
10+
error = e;
11+
}
12+
if (!(error instanceof errorType))
13+
throw new Error(`bad error: ${String(error)}`);
14+
}
15+
16+
function spread(iterable) {
17+
return [...iterable];
18+
}
19+
20+
shouldThrow(() => spread(new Uint8Array(2 ** 32 + 3)), RangeError);
Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
function shouldBe(actual, expected) {
2+
if (!Object.is(actual, expected))
3+
throw new Error(`bad value: expected ${String(expected)} but got ${String(actual)}`);
4+
}
5+
6+
function shouldBeArray(actual, expected) {
7+
shouldBe(Array.isArray(actual), true);
8+
shouldBe(actual.length, expected.length);
9+
for (let i = 0; i < expected.length; ++i)
10+
shouldBe(actual[i], expected[i]);
11+
}
12+
13+
function shouldThrow(func, errorType) {
14+
let error;
15+
try {
16+
func();
17+
} catch (e) {
18+
error = e;
19+
}
20+
if (!(error instanceof errorType))
21+
throw new Error(`bad error: ${String(error)}`);
22+
}
23+
24+
function spread(iterable) {
25+
return [...iterable];
26+
}
27+
noInline(spread);
28+
29+
function spreadWithOthers(iterable) {
30+
return [1, ...iterable, 2, ...iterable];
31+
}
32+
noInline(spreadWithOthers);
33+
34+
function collect() {
35+
return Array.prototype.slice.call(arguments);
36+
}
37+
noInline(collect);
38+
39+
function callSpread(iterable) {
40+
return collect(...iterable);
41+
}
42+
noInline(callSpread);
43+
44+
class Collector {
45+
constructor(...args) {
46+
this.args = args;
47+
}
48+
}
49+
50+
function constructSpread(iterable) {
51+
return new Collector(...iterable).args;
52+
}
53+
noInline(constructSpread);
54+
55+
function elements(typedArray) {
56+
let result = [];
57+
for (let i = 0; i < typedArray.length; ++i)
58+
result.push(typedArray[i]);
59+
return result;
60+
}
61+
62+
function makeTypedArray(constructor, values) {
63+
let typedArray = new constructor(19);
64+
for (let i = 0; i < typedArray.length; ++i)
65+
typedArray[i] = values[i % values.length];
66+
return typedArray;
67+
}
68+
69+
const typedArrays = [
70+
makeTypedArray(Int8Array, [-128, -1, 0, 1, 127]),
71+
makeTypedArray(Uint8Array, [0, 1, 128, 255]),
72+
makeTypedArray(Uint8ClampedArray, [0, 1, 128, 255]),
73+
makeTypedArray(Int16Array, [-32768, -1, 0, 32767]),
74+
makeTypedArray(Uint16Array, [0, 1, 32768, 65535]),
75+
makeTypedArray(Int32Array, [-2147483648, -1, 0, 2147483647]),
76+
makeTypedArray(Uint32Array, [0, 1, 2147483647, 2147483648, 4294967295]),
77+
makeTypedArray(Float16Array, [0, -0, 1.5, NaN, Infinity, -Infinity, 65504]),
78+
makeTypedArray(Float32Array, [0, -0, 1.5, NaN, Infinity, -Infinity, 16777216, 0.1]),
79+
makeTypedArray(Float64Array, [0, -0, 1.5, NaN, Infinity, -Infinity, 9007199254740992, 0.1, 4294967296]),
80+
makeTypedArray(BigInt64Array, [-9223372036854775808n, -1n, 0n, 1n, 9223372036854775807n]),
81+
makeTypedArray(BigUint64Array, [0n, 1n, 9223372036854775808n, 18446744073709551615n]),
82+
new Uint8Array(0),
83+
new Uint16Array(new Uint16Array([1, 2, 3, 4, 5, 6]).buffer, 4, 3),
84+
];
85+
const expectedElements = typedArrays.map(elements);
86+
const expectedElementsWithOthers = expectedElements.map(expected => [1, ...expected, 2, ...expected]);
87+
88+
for (let i = 0; i < testLoopCount; ++i) {
89+
let index = i % typedArrays.length;
90+
let typedArray = typedArrays[index];
91+
let expected = expectedElements[index];
92+
shouldBeArray(spread(typedArray), expected);
93+
shouldBeArray(callSpread(typedArray), expected);
94+
shouldBeArray(constructSpread(typedArray), expected);
95+
shouldBeArray(spreadWithOthers(typedArray), expectedElementsWithOthers[index]);
96+
}
97+
98+
{
99+
let typedArray = new Uint8Array([1, 2, 3]);
100+
let first = spread(typedArray);
101+
let second = spread(typedArray);
102+
shouldBe(first === second, false);
103+
first[0] = 42;
104+
first.push(4);
105+
shouldBeArray(first, [42, 2, 3, 4]);
106+
shouldBeArray(second, [1, 2, 3]);
107+
shouldBeArray(elements(typedArray), [1, 2, 3]);
108+
typedArray[1] = 9;
109+
shouldBeArray(second, [1, 2, 3]);
110+
shouldBeArray(spread(typedArray), [1, 9, 3]);
111+
}
112+
113+
{
114+
let typedArray = new Uint8Array([1, 2, 3]);
115+
typedArray.buffer.transfer();
116+
shouldThrow(() => spread(typedArray), TypeError);
117+
}
118+
119+
shouldThrow(() => spread(new DataView(new ArrayBuffer(8))), TypeError);
120+
121+
{
122+
let buffer = new ArrayBuffer(8, { maxByteLength: 16 });
123+
let whole = new Uint8Array(buffer);
124+
for (let i = 0; i < 8; ++i)
125+
whole[i] = i + 1;
126+
let fixed = new Uint8Array(buffer, 4, 4);
127+
let tracking = new Uint8Array(buffer, 4);
128+
let trackingUint16 = new Uint16Array(buffer, 2);
129+
shouldBeArray(spread(whole), [1, 2, 3, 4, 5, 6, 7, 8]);
130+
shouldBeArray(spread(fixed), [5, 6, 7, 8]);
131+
shouldBeArray(spread(tracking), [5, 6, 7, 8]);
132+
shouldBeArray(spread(trackingUint16), elements(trackingUint16));
133+
134+
buffer.resize(7);
135+
shouldBeArray(spread(whole), [1, 2, 3, 4, 5, 6, 7]);
136+
shouldThrow(() => spread(fixed), TypeError);
137+
shouldBeArray(spread(tracking), [5, 6, 7]);
138+
shouldBeArray(spread(trackingUint16), elements(trackingUint16));
139+
140+
buffer.resize(3);
141+
shouldBeArray(spread(whole), [1, 2, 3]);
142+
shouldThrow(() => spread(fixed), TypeError);
143+
shouldThrow(() => spread(tracking), TypeError);
144+
shouldBeArray(spread(trackingUint16), []);
145+
146+
buffer.resize(1);
147+
shouldThrow(() => spread(trackingUint16), TypeError);
148+
149+
buffer.resize(16);
150+
shouldBeArray(spread(whole), [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
151+
shouldBeArray(spread(fixed), [0, 0, 0, 0]);
152+
shouldBeArray(spread(tracking), [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
153+
shouldBeArray(spread(trackingUint16), [0, 0, 0, 0, 0, 0, 0]);
154+
}
155+
156+
{
157+
let buffer = new SharedArrayBuffer(4, { maxByteLength: 8 });
158+
let tracking = new Uint8Array(buffer);
159+
let fixed = new Uint8Array(buffer, 1, 2);
160+
tracking.set([1, 2, 3, 4]);
161+
shouldBeArray(spread(tracking), [1, 2, 3, 4]);
162+
shouldBeArray(spread(fixed), [2, 3]);
163+
buffer.grow(8);
164+
shouldBeArray(spread(tracking), [1, 2, 3, 4, 0, 0, 0, 0]);
165+
shouldBeArray(spread(fixed), [2, 3]);
166+
}
167+
168+
{
169+
let typedArray = new Uint8Array([1, 2, 3]);
170+
typedArray[Symbol.iterator] = function* () {
171+
yield 42;
172+
};
173+
shouldBeArray(spread(typedArray), [42]);
174+
}
175+
176+
{
177+
class Derived extends Uint8Array { }
178+
shouldBeArray(spread(new Derived([1, 2, 3])), [1, 2, 3]);
179+
180+
class DerivedWithIterator extends Uint8Array {
181+
*[Symbol.iterator]() {
182+
yield 42;
183+
}
184+
}
185+
shouldBeArray(spread(new DerivedWithIterator([1, 2, 3])), [42]);
186+
}
187+
188+
{
189+
let typedArray = new Uint8Array([1, 2, 3]);
190+
Object.setPrototypeOf(typedArray, {
191+
__proto__: Uint8Array.prototype,
192+
*[Symbol.iterator]() {
193+
yield 42;
194+
}
195+
});
196+
shouldBeArray(spread(typedArray), [42]);
197+
}
198+
199+
function testInRealm(modify, check) {
200+
let realm = createGlobalObject();
201+
let typedArrays = [
202+
new realm.Uint8Array([1, 2, 3]),
203+
new realm.Float64Array([1.5, 2.5]),
204+
new realm.BigInt64Array([1n, 2n]),
205+
];
206+
shouldBeArray(spread(typedArrays[0]), [1, 2, 3]);
207+
shouldBeArray(spread(typedArrays[1]), [1.5, 2.5]);
208+
shouldBeArray(spread(typedArrays[2]), [1n, 2n]);
209+
modify(realm);
210+
check(typedArrays);
211+
shouldBeArray(spread(new Uint8Array([1, 2, 3])), [1, 2, 3]);
212+
shouldBeArray(spread(new Float64Array([1.5, 2.5])), [1.5, 2.5]);
213+
}
214+
215+
testInRealm(realm => {
216+
realm.Uint8Array.prototype[Symbol.iterator] = function* () {
217+
yield 42;
218+
};
219+
}, typedArrays => {
220+
shouldBeArray(spread(typedArrays[0]), [42]);
221+
shouldBeArray(spread(typedArrays[1]), [1.5, 2.5]);
222+
shouldBeArray(spread(typedArrays[2]), [1n, 2n]);
223+
});
224+
225+
testInRealm(realm => {
226+
Object.getPrototypeOf(realm.Uint8Array.prototype)[Symbol.iterator] = function* () {
227+
yield this.length;
228+
};
229+
}, typedArrays => {
230+
shouldBeArray(spread(typedArrays[0]), [3]);
231+
shouldBeArray(spread(typedArrays[1]), [2]);
232+
shouldBeArray(spread(typedArrays[2]), [2]);
233+
});
234+
235+
testInRealm(realm => {
236+
let arrayIteratorPrototype = Object.getPrototypeOf(new realm.Array()[Symbol.iterator]());
237+
let count = 0;
238+
arrayIteratorPrototype.next = function () {
239+
return { done: count++ & 1, value: 42 };
240+
};
241+
}, typedArrays => {
242+
shouldBeArray(spread(typedArrays[0]), [42]);
243+
shouldBeArray(spread(typedArrays[1]), [42]);
244+
shouldBeArray(spread(typedArrays[2]), [42]);
245+
});
246+
247+
testInRealm(realm => {
248+
Object.setPrototypeOf(realm.Float64Array.prototype, {
249+
__proto__: Object.getPrototypeOf(realm.Float64Array.prototype),
250+
*[Symbol.iterator]() {
251+
yield 42;
252+
}
253+
});
254+
}, typedArrays => {
255+
shouldBeArray(spread(typedArrays[0]), [1, 2, 3]);
256+
shouldBeArray(spread(typedArrays[1]), [42]);
257+
shouldBeArray(spread(typedArrays[2]), [1n, 2n]);
258+
});

‎Source/JavaScriptCore/runtime/CommonSlowPathsInlines.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "ClonedArguments.h"
3030
#include "CommonSlowPaths.h"
3131
#include "DirectArguments.h"
32+
#include "JSArrayBufferViewInlines.h"
3233
#include "JSGlobalLexicalEnvironment.h"
3334
#include "JSSetInlines.h"
3435
#include "ScopedArguments.h"
@@ -209,6 +210,15 @@ ALWAYS_INLINE JSCellButterfly* trySpreadFast(JSGlobalObject* globalObject, JSCel
209210
return JSCellButterfly::createFromSet(globalObject, set);
210211
return nullptr;
211212
}
213+
#define CASE_TYPED_ARRAY_TYPE(name) case name##ArrayType:
214+
FOR_EACH_TYPED_ARRAY_TYPE_EXCLUDING_DATA_VIEW(CASE_TYPED_ARRAY_TYPE)
215+
#undef CASE_TYPED_ARRAY_TYPE
216+
{
217+
auto* view = uncheckedDowncast<JSArrayBufferView>(iterable);
218+
if (!view->isOutOfBounds() && view->isIteratorProtocolFastAndNonObservable()) [[likely]]
219+
return JSCellButterfly::createFromTypedArray(globalObject, view);
220+
return nullptr;
221+
}
212222
default:
213223
return nullptr;
214224
}

‎Source/JavaScriptCore/runtime/JSCellButterfly.cpp‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "ButterflyInlines.h"
3030
#include "ClonedArguments.h"
3131
#include "DirectArguments.h"
32+
#include "JSGenericTypedArrayViewInlines.h"
3233
#include "JSObjectInlines.h"
3334
#include "JSSetInlines.h"
3435
#include "ScopedArguments.h"
@@ -271,6 +272,53 @@ JSCellButterfly* JSCellButterfly::createFromSet(JSGlobalObject* globalObject, JS
271272
return result;
272273
}
273274

275+
template<typename ViewClass>
276+
static ALWAYS_INLINE JSCellButterfly* createFromTypedArrayImpl(JSGlobalObject* globalObject, ViewClass* typedArray)
277+
{
278+
VM& vm = globalObject->vm();
279+
auto scope = DECLARE_THROW_SCOPE(vm);
280+
281+
auto elements = typedArray->typedSpan();
282+
if (elements.size() > IndexingHeader::maximumLength) [[unlikely]] {
283+
throwOutOfMemoryError(globalObject, scope);
284+
return nullptr;
285+
}
286+
287+
JSCellButterfly* result = JSCellButterfly::tryCreate(vm, vm.cellButterflyStructure(CopyOnWriteArrayWithContiguous), elements.size());
288+
if (!result) [[unlikely]] {
289+
throwOutOfMemoryError(globalObject, scope);
290+
return nullptr;
291+
}
292+
293+
auto contiguous = result->toButterfly()->contiguous();
294+
for (size_t i = 0; i < elements.size(); ++i) {
295+
JSValue value = ViewClass::Adaptor::toJSValue(globalObject, elements[i]);
296+
if constexpr (ViewClass::Adaptor::canConvertToJSQuickly)
297+
contiguous.atUnsafe(i).setWithoutWriteBarrier(value);
298+
else {
299+
RETURN_IF_EXCEPTION(scope, nullptr);
300+
result->setIndex(vm, i, value);
301+
}
302+
}
303+
304+
ensureStillAliveHere(typedArray);
305+
return result;
306+
}
307+
308+
JSCellButterfly* JSCellButterfly::createFromTypedArray(JSGlobalObject* globalObject, JSArrayBufferView* view)
309+
{
310+
switch (view->type()) {
311+
#define CASE_TYPED_ARRAY_TYPE(name) \
312+
case name##ArrayType: \
313+
return createFromTypedArrayImpl(globalObject, uncheckedDowncast<JS##name##Array>(view));
314+
FOR_EACH_TYPED_ARRAY_TYPE_EXCLUDING_DATA_VIEW(CASE_TYPED_ARRAY_TYPE)
315+
#undef CASE_TYPED_ARRAY_TYPE
316+
default:
317+
RELEASE_ASSERT_NOT_REACHED();
318+
return nullptr;
319+
}
320+
}
321+
274322
JSCellButterfly* JSCellButterfly::tryCreateFromArgList(VM& vm, ArgList argList)
275323
{
276324
JSCellButterfly* result = JSCellButterfly::tryCreate(vm, vm.cellButterflyStructure(CopyOnWriteArrayWithContiguous), argList.size());

‎Source/JavaScriptCore/runtime/JSCellButterfly.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ namespace JSC {
4040

4141
class ClonedArguments;
4242
class DirectArguments;
43+
class JSArrayBufferView;
4344
class JSSet;
4445
class ScopedArguments;
4546

@@ -150,6 +151,7 @@ class JSCellButterfly : public JSCell {
150151
static JSCellButterfly* createFromScopedArguments(JSGlobalObject*, ScopedArguments*);
151152
static JSCellButterfly* createFromSet(JSGlobalObject*, JSSet*);
152153
static JSCellButterfly* createFromString(JSGlobalObject*, JSString*);
154+
static JSCellButterfly* createFromTypedArray(JSGlobalObject*, JSArrayBufferView*);
153155
static JSCellButterfly* tryCreateFromArgList(VM&, ArgList);
154156

155157
unsigned publicLength() const { return m_header.publicLength(); }

0 commit comments

Comments
 (0)