Skip to content

Commit d336a98

Browse files
committed
[JSC] Make %TypedArray%.prototype.join fast
https://bugs.webkit.org/show_bug.cgi?id=330279 rdar://178158917 Reviewed by Tadeu Zagallo. This patch makes %TypedArray%.prototype.join faster. 1. For non shared integer TypedArray, we have extreme fast path "joinIntegerTypedArray". We only apply this to non shared array as this scans entire array to know the length of generated string first, but shared array can change elements concurrently. 2. In the other cases, we add JSTypedArrayJoiner, which incrementally ensures the buffer size. ToT Patched typed-array-join-float64 29.5397+-0.9507 ^ 7.1845+-0.1148 ^ definitely 4.1116x faster typed-array-join-int32-separator 95.8649+-2.3456 ^ 17.7610+-1.2913 ^ definitely 5.3975x faster typed-array-join-uint8 66.8946+-1.7089 ^ 6.5944+-0.8064 ^ definitely 10.1441x faster typed-array-join-float64-integers 89.3071+-2.2896 ^ 10.5858+-0.3497 ^ definitely 8.4365x faster typed-array-join-float32 28.8638+-0.4050 ^ 7.5480+-0.2421 ^ definitely 3.8240x faster typed-array-join-shared-int32-separator 95.3301+-2.9318 ^ 18.7518+-0.6931 ^ definitely 5.0838x faster typed-array-join-shared-uint8 66.8511+-0.6475 ^ 9.1109+-0.2016 ^ definitely 7.3375x faster typed-array-join-bigint64 106.2910+-2.6682 ^ 9.9683+-0.2517 ^ definitely 10.6629x faster Tests: JSTests/stress/typedarray-join-floats-and-shared.js JSTests/stress/typedarray-join-integers.js Tools/TestWebKitAPI/Tests/WTF/StringConcatenate.cpp * JSTests/microbenchmarks/typed-array-join-bigint64.js: Added. (test): * JSTests/microbenchmarks/typed-array-join-float32.js: Added. (next): (test): * JSTests/microbenchmarks/typed-array-join-float64-integers.js: Added. (test): * JSTests/microbenchmarks/typed-array-join-float64.js: Added. (next): (test): * JSTests/microbenchmarks/typed-array-join-int32-separator.js: Added. (next): (test): * JSTests/microbenchmarks/typed-array-join-shared-int32-separator.js: Added. (next): (test): * JSTests/microbenchmarks/typed-array-join-shared-uint8.js: Added. (test): * JSTests/microbenchmarks/typed-array-join-uint8.js: Added. (test): * JSTests/stress/typedarray-join-floats-and-shared.js: Added. (shouldBe): (reference): (check): (next): (fillRandomBits): (fillRandomNumbers): * JSTests/stress/typedarray-join-integers.js: Added. (shouldBe): (shouldThrowTypeError): (reference): (check): * Source/JavaScriptCore/runtime/JSGenericTypedArrayViewPrototypeFunctions.h: (JSC::genericTypedArrayViewProtoFuncJoin): * Source/JavaScriptCore/runtime/JSStringJoiner.cpp: (JSC::ByteIntegerStrings::ByteIntegerStrings): (JSC::appendStringToData): (JSC::joinIntegerTypedArray): (JSC::JSTypedArrayJoiner::JSTypedArrayJoiner): (JSC::JSTypedArrayJoiner::ensureRoom): (JSC::JSTypedArrayJoiner::expandBuffer): (JSC::JSTypedArrayJoiner::separatorLength const): (JSC::JSTypedArrayJoiner::appendSeparator const): (JSC::CharacterType>::appendElements): (JSC::CharacterType>::append): (JSC::CharacterType>::appendSeparators): (JSC::CharacterType>::join): (JSC::joinTypedArray): * Source/JavaScriptCore/runtime/JSStringJoiner.h: * Source/WTF/wtf/text/IntegerToStringConversion.h: (WTF::lengthOfIntegerAsStringImpl): (WTF::lengthOfIntegerAsString): * Tools/TestWebKitAPI/Tests/WTF/StringConcatenate.cpp: (TestWebKitAPI::testLengthOfIntegerAsStringAtPowersOf10): (TestWebKitAPI::TEST(WTF, StringConcatenate_LengthOfIntegerAsString)): Canonical link: https://commits.webkit.org/325881@main
1 parent bb7fdb1 commit d336a98

15 files changed

Lines changed: 810 additions & 28 deletions
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var array = new BigInt64Array(1 << 14);
2+
for (var i = 0; i < array.length; ++i)
3+
array[i] = BigInt(i) * -0x9e3779b97f4an;
4+
5+
function test(array) {
6+
return array.join();
7+
}
8+
noInline(test);
9+
10+
var expected = test(array).length;
11+
var total = 0;
12+
for (var i = 0; i < 100; ++i)
13+
total += test(array).length;
14+
if (total !== expected * 100)
15+
throw new Error("bad result: " + total);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var seed = 42;
2+
function next() {
3+
seed = (seed + 0x9e3779b9) | 0;
4+
var t = seed ^ (seed >>> 16);
5+
t = Math.imul(t, 0x21f0aaad);
6+
t = t ^ (t >>> 15);
7+
t = Math.imul(t, 0x735a2d97);
8+
return (t ^ (t >>> 15)) >>> 0;
9+
}
10+
11+
var array = new Float32Array(1 << 14);
12+
for (var i = 0; i < array.length; ++i)
13+
array[i] = next() / 0x100000000 * 2 - 1;
14+
15+
function test(array) {
16+
return array.join(" ");
17+
}
18+
noInline(test);
19+
20+
var expected = test(array).length;
21+
var total = 0;
22+
for (var i = 0; i < 50; ++i)
23+
total += test(array).length;
24+
if (total !== expected * 50)
25+
throw new Error("bad result: " + total);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var array = new Float64Array(1 << 16);
2+
for (var i = 0; i < array.length; ++i)
3+
array[i] = ((i * 7919) & 0xffff) - 0x8000;
4+
5+
function test(array) {
6+
return array.join();
7+
}
8+
noInline(test);
9+
10+
var expected = test(array).length;
11+
var total = 0;
12+
for (var i = 0; i < 50; ++i)
13+
total += test(array).length;
14+
if (total !== expected * 50)
15+
throw new Error("bad result: " + total);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var seed = 42;
2+
function next() {
3+
seed = (seed + 0x9e3779b9) | 0;
4+
var t = seed ^ (seed >>> 16);
5+
t = Math.imul(t, 0x21f0aaad);
6+
t = t ^ (t >>> 15);
7+
t = Math.imul(t, 0x735a2d97);
8+
return (t ^ (t >>> 15)) >>> 0;
9+
}
10+
11+
var array = new Float64Array(1 << 14);
12+
for (var i = 0; i < array.length; ++i)
13+
array[i] = next() / 0x100000000 * 1000 - 500;
14+
15+
function test(array) {
16+
return array.join();
17+
}
18+
noInline(test);
19+
20+
var expected = test(array).length;
21+
var total = 0;
22+
for (var i = 0; i < 50; ++i)
23+
total += test(array).length;
24+
if (total !== expected * 50)
25+
throw new Error("bad result: " + total);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var seed = 42;
2+
function next() {
3+
seed = (seed + 0x9e3779b9) | 0;
4+
var t = seed ^ (seed >>> 16);
5+
t = Math.imul(t, 0x21f0aaad);
6+
t = t ^ (t >>> 15);
7+
t = Math.imul(t, 0x735a2d97);
8+
return t ^ (t >>> 15);
9+
}
10+
11+
var array = new Int32Array(1 << 14);
12+
for (var i = 0; i < array.length; ++i)
13+
array[i] = next();
14+
15+
function test(array) {
16+
return array.join(", ");
17+
}
18+
noInline(test);
19+
20+
var expected = test(array).length;
21+
var total = 0;
22+
for (var i = 0; i < 200; ++i)
23+
total += test(array).length;
24+
if (total !== expected * 200)
25+
throw new Error("bad result: " + total);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var seed = 42;
2+
function next() {
3+
seed = (seed + 0x9e3779b9) | 0;
4+
var t = seed ^ (seed >>> 16);
5+
t = Math.imul(t, 0x21f0aaad);
6+
t = t ^ (t >>> 15);
7+
t = Math.imul(t, 0x735a2d97);
8+
return t ^ (t >>> 15);
9+
}
10+
11+
var array = new Int32Array(new SharedArrayBuffer(4 << 14));
12+
for (var i = 0; i < array.length; ++i)
13+
array[i] = next();
14+
15+
function test(array) {
16+
return array.join(", ");
17+
}
18+
noInline(test);
19+
20+
var expected = test(array).length;
21+
var total = 0;
22+
for (var i = 0; i < 200; ++i)
23+
total += test(array).length;
24+
if (total !== expected * 200)
25+
throw new Error("bad result: " + total);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var array = new Uint8Array(new SharedArrayBuffer(1 << 16));
2+
for (var i = 0; i < array.length; ++i)
3+
array[i] = (i * 7919) & 0xff;
4+
5+
function test(array) {
6+
return array.join();
7+
}
8+
noInline(test);
9+
10+
var expected = test(array).length;
11+
var total = 0;
12+
for (var i = 0; i < 100; ++i)
13+
total += test(array).length;
14+
if (total !== expected * 100)
15+
throw new Error("bad result: " + total);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var array = new Uint8Array(1 << 16);
2+
for (var i = 0; i < array.length; ++i)
3+
array[i] = (i * 7919) & 0xff;
4+
5+
function test(array) {
6+
return array.join();
7+
}
8+
noInline(test);
9+
10+
var expected = test(array).length;
11+
var total = 0;
12+
for (var i = 0; i < 100; ++i)
13+
total += test(array).length;
14+
if (total !== expected * 100)
15+
throw new Error("bad result: " + total);
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
function shouldBe(actual, expected) {
2+
if (actual !== expected)
3+
throw new Error(`expected ${expected} but got ${actual}`);
4+
}
5+
6+
const separators = [undefined, '', ',', '--', '\u3042', 'ab\u3042cd'];
7+
8+
function reference(array, separator) {
9+
return Array.prototype.map.call(array, String).join(separator);
10+
}
11+
12+
function check(array) {
13+
for (const separator of separators)
14+
shouldBe(array.join(separator), reference(array, separator));
15+
}
16+
17+
let seed = 42;
18+
function next() {
19+
seed = (seed + 0x9e3779b9) | 0;
20+
let t = seed ^ (seed >>> 16);
21+
t = Math.imul(t, 0x21f0aaad);
22+
t = t ^ (t >>> 15);
23+
t = Math.imul(t, 0x735a2d97);
24+
return (t ^ (t >>> 15)) >>> 0;
25+
}
26+
27+
const values = [
28+
0, -0, 1, -1, 0.5, 0.1, -0.1, 1 / 3, 1e21, 1e-7, 123456789.125, 1e20, 123e-20,
29+
2 ** 53 - 1, 2 ** 53, 2 ** 53 + 2, -(2 ** 53), 2 ** 60, 2 ** 64,
30+
Number.MAX_VALUE, Number.MIN_VALUE, NaN, Infinity, -Infinity, 65504, 6.103515625e-5,
31+
];
32+
33+
function fillRandomBits(array) {
34+
const words = new Uint32Array(array.buffer, array.byteOffset, array.byteLength / 4);
35+
for (let i = 0; i < words.length; ++i)
36+
words[i] = next();
37+
}
38+
39+
function fillRandomNumbers(array) {
40+
for (let i = 0; i < array.length; ++i)
41+
array[i] = (next() / 0x100000000 - 0.5) * 10 ** ((next() % 40) - 20);
42+
}
43+
44+
for (const constructor of [Float16Array, Float32Array, Float64Array]) {
45+
check(new constructor(0));
46+
check(new constructor([0.5]));
47+
check(new constructor(values));
48+
for (const value of values)
49+
check(new constructor([value]));
50+
51+
const bits = new constructor(1024);
52+
fillRandomBits(bits);
53+
check(bits);
54+
55+
const numbers = new constructor(1024);
56+
fillRandomNumbers(numbers);
57+
check(numbers);
58+
59+
const shared = new constructor(new SharedArrayBuffer(1024 * constructor.BYTES_PER_ELEMENT));
60+
fillRandomNumbers(shared);
61+
check(shared);
62+
63+
{
64+
const array = new constructor(4).fill(0.5);
65+
shouldBe(array.join({ toString() { transferArrayBuffer(array.buffer); return '-'; } }), '---');
66+
}
67+
68+
{
69+
const buffer = new ArrayBuffer(4 * constructor.BYTES_PER_ELEMENT, { maxByteLength: 8 * constructor.BYTES_PER_ELEMENT });
70+
const array = new constructor(buffer).fill(0.5);
71+
shouldBe(array.join({ toString() { buffer.resize(2 * constructor.BYTES_PER_ELEMENT); return '\u3042'; } }), '0.5\u30420.5\u3042\u3042');
72+
}
73+
74+
{
75+
const buffer = new SharedArrayBuffer(2 * constructor.BYTES_PER_ELEMENT, { maxByteLength: 8 * constructor.BYTES_PER_ELEMENT });
76+
const array = new constructor(buffer).fill(0.5);
77+
shouldBe(array.join({ toString() { buffer.grow(4 * constructor.BYTES_PER_ELEMENT); return ','; } }), '0.5,0.5');
78+
shouldBe(array.join(), '0.5,0.5,0,0');
79+
}
80+
}
81+
82+
for (const constructor of [Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array, Int32Array, Uint32Array, BigInt64Array, BigUint64Array]) {
83+
const shared = new constructor(new SharedArrayBuffer(1024 * constructor.BYTES_PER_ELEMENT));
84+
fillRandomBits(shared);
85+
check(shared);
86+
check(new constructor(new SharedArrayBuffer(0)));
87+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
function shouldBe(actual, expected) {
2+
if (actual !== expected)
3+
throw new Error(`expected ${expected} but got ${actual}`);
4+
}
5+
6+
function shouldThrowTypeError(func) {
7+
let error;
8+
try {
9+
func();
10+
} catch (e) {
11+
error = e;
12+
}
13+
if (!(error instanceof TypeError))
14+
throw new Error('Expected TypeError');
15+
}
16+
17+
const separators = [undefined, '', ',', '--', '\u3042', 'ab\u3042cd'];
18+
19+
function reference(array, separator) {
20+
return Array.prototype.map.call(array, String).join(separator);
21+
}
22+
23+
function check(array) {
24+
for (const separator of separators)
25+
shouldBe(array.join(separator), reference(array, separator));
26+
}
27+
28+
const numberTypes = [
29+
[Int8Array, -128, 127],
30+
[Uint8Array, 0, 255],
31+
[Uint8ClampedArray, 0, 255],
32+
[Int16Array, -32768, 32767],
33+
[Uint16Array, 0, 65535],
34+
[Int32Array, -2147483648, 2147483647],
35+
[Uint32Array, 0, 4294967295],
36+
];
37+
38+
const bigIntTypes = [
39+
[BigInt64Array, -(2n ** 63n), 2n ** 63n - 1n],
40+
[BigUint64Array, 0n, 2n ** 64n - 1n],
41+
];
42+
43+
for (const [constructor, min, max] of numberTypes) {
44+
check(new constructor(0));
45+
check(new constructor([0]));
46+
check(new constructor([max]));
47+
check(new constructor([min, max, 0, 1, 9, 10, 99, 100, 101, max - 1, min + 1]));
48+
const big = new constructor(1000);
49+
for (let i = 0; i < big.length; ++i)
50+
big[i] = (i * 2654435761) % (max - min + 1) + min;
51+
check(big);
52+
}
53+
54+
for (const [constructor, min, max] of bigIntTypes) {
55+
check(new constructor(0));
56+
check(new constructor([0n]));
57+
check(new constructor([max]));
58+
check(new constructor([min, max, 0n, 1n, 9n, 10n, 99n, 100n, max - 1n, min + 1n]));
59+
const big = new constructor(1000);
60+
for (let i = 0; i < big.length; ++i)
61+
big[i] = BigInt(i) * 0x9e3779b97f4a7c15n;
62+
check(big);
63+
}
64+
65+
for (const [constructor] of [...numberTypes, ...bigIntTypes]) {
66+
const one = constructor === BigInt64Array || constructor === BigUint64Array ? 1n : 1;
67+
68+
{
69+
const array = new constructor(4).fill(one);
70+
shouldBe(array.join({ toString() { transferArrayBuffer(array.buffer); return '-'; } }), '---');
71+
shouldThrowTypeError(() => array.join());
72+
}
73+
74+
{
75+
const buffer = new ArrayBuffer(4 * constructor.BYTES_PER_ELEMENT, { maxByteLength: 8 * constructor.BYTES_PER_ELEMENT });
76+
const array = new constructor(buffer, 0, 4).fill(one);
77+
shouldBe(array.join({ toString() { buffer.resize(2 * constructor.BYTES_PER_ELEMENT); return ','; } }), ',,,');
78+
shouldThrowTypeError(() => array.join());
79+
}
80+
81+
{
82+
const buffer = new ArrayBuffer(4 * constructor.BYTES_PER_ELEMENT, { maxByteLength: 8 * constructor.BYTES_PER_ELEMENT });
83+
const array = new constructor(buffer).fill(one);
84+
shouldBe(array.join({ toString() { buffer.resize(2 * constructor.BYTES_PER_ELEMENT); return '\u3042'; } }), '1\u30421\u3042\u3042');
85+
shouldBe(array.join(), '1,1');
86+
buffer.resize(8 * constructor.BYTES_PER_ELEMENT);
87+
shouldBe(array.join(''), '11000000');
88+
}
89+
90+
{
91+
const buffer = new ArrayBuffer(3 * constructor.BYTES_PER_ELEMENT, { maxByteLength: 3 * constructor.BYTES_PER_ELEMENT });
92+
const array = new constructor(buffer).fill(one);
93+
shouldBe(array.join({ toString() { buffer.resize(0); return ''; } }), '');
94+
}
95+
96+
if (typeof SharedArrayBuffer !== 'undefined') {
97+
const array = new constructor(new SharedArrayBuffer(3 * constructor.BYTES_PER_ELEMENT)).fill(one);
98+
shouldBe(array.join(), '1,1,1');
99+
check(array);
100+
}
101+
}
102+
103+
for (const [constructor, min, max] of numberTypes) {
104+
const boundaries = [];
105+
for (let power = 1; power <= max; power *= 10) {
106+
for (const value of [power - 1, power, -(power - 1), -power]) {
107+
if (value >= min && value <= max)
108+
boundaries.push(value);
109+
}
110+
}
111+
check(new constructor(boundaries));
112+
}
113+
114+
for (const [constructor, min, max] of bigIntTypes) {
115+
const boundaries = [];
116+
for (let power = 1n; power <= max; power *= 10n) {
117+
for (const value of [power - 1n, power, -(power - 1n), -power]) {
118+
if (value >= min && value <= max)
119+
boundaries.push(value);
120+
}
121+
}
122+
check(new constructor(boundaries));
123+
}

0 commit comments

Comments
 (0)