Skip to content

Commit 37aa2b5

Browse files
committed
[JSC] Avoid using wrapper iterator in builtins
https://bugs.webkit.org/show_bug.cgi?id=328563 rdar://190769590 Reviewed by Sosuke Suzuki. Since for-of calls @@iterator unconditionally, we end up having iterator wrappers in builtins to align to the spec behavior. But this does not help at all in terms of the performance since for-of protocol is tailored for the normal use cases. This patch just uses @iteratorGenericNext, @ifAbruptCloseIterator, and @iteratorGenericClose instead. ToT Patched array-from-iterable-typed-array 38.3254+-0.3136 ^ 33.4306+-0.2911 ^ definitely 1.1464x faster array-from-iterable-arguments-with-mapfn 16.8144+-0.0560 ^ 12.2938+-0.1566 ^ definitely 1.3677x faster array-from-iterable-string 13.2912+-0.1097 ^ 10.2982+-0.1468 ^ definitely 1.2906x faster array-from-iterable-generator 22.6355+-0.2494 ^ 20.5139+-0.2091 ^ definitely 1.1034x faster array-from-iterable-generator-with-mapfn 21.7771+-0.4297 ^ 15.9977+-0.3165 ^ definitely 1.3613x faster array-from-iterable-custom-iterator-with-mapfn 17.9004+-0.1186 ^ 13.7685+-0.0657 ^ definitely 1.3001x faster array-from-iterable-map-with-mapfn 22.8844+-0.1926 ^ 17.0909+-0.0785 ^ definitely 1.3390x faster array-from-iterable-typed-array-with-mapfn 35.9297+-0.1972 ^ 32.1489+-0.2680 ^ definitely 1.1176x faster array-from-iterable-string-with-mapfn 11.7597+-0.1257 ^ 8.8638+-0.1340 ^ definitely 1.3267x faster array-from-iterable-map 22.9383+-0.1265 ^ 17.9592+-0.1083 ^ definitely 1.2772x faster array-from-iterable-set-with-mapfn 14.8254+-0.1530 ^ 9.4993+-0.0998 ^ definitely 1.5607x faster array-from-iterable-custom-iterator 18.6419+-0.1618 ^ 14.7639+-0.0369 ^ definitely 1.2627x faster array-from-iterable-array-with-mapfn 13.6173+-0.0928 ^ 8.4895+-0.1468 ^ definitely 1.6040x faster Tests: JSTests/microbenchmarks/array-from-iterable-arguments-with-mapfn.js JSTests/microbenchmarks/array-from-iterable-array-with-mapfn.js JSTests/microbenchmarks/array-from-iterable-custom-iterator-with-mapfn.js JSTests/microbenchmarks/array-from-iterable-custom-iterator.js JSTests/microbenchmarks/array-from-iterable-generator-with-mapfn.js JSTests/microbenchmarks/array-from-iterable-generator.js JSTests/microbenchmarks/array-from-iterable-map-with-mapfn.js JSTests/microbenchmarks/array-from-iterable-map.js JSTests/microbenchmarks/array-from-iterable-set-with-mapfn.js JSTests/microbenchmarks/array-from-iterable-string-with-mapfn.js JSTests/microbenchmarks/array-from-iterable-string.js JSTests/microbenchmarks/array-from-iterable-typed-array-with-mapfn.js JSTests/microbenchmarks/array-from-iterable-typed-array.js JSTests/stress/array-from-iterable-closes-iterator-and-reads-iterator-once.js JSTests/stress/builtin-iteration-closes-iterator-and-reads-iterator-once.js JSTests/stress/iterator-helpers-flatmap-close-iterator-on-throw.js JSTests/stress/iterator-helpers-flatmap-close-iterator.js JSTests/stress/iterator-helpers-some-every-find-close-iterator.js * JSTests/microbenchmarks/array-from-iterable-arguments-with-mapfn.js: Added. (makeArguments): (test): * JSTests/microbenchmarks/array-from-iterable-array-with-mapfn.js: Added. (test): * JSTests/microbenchmarks/array-from-iterable-custom-iterator-with-mapfn.js: Added. (CustomIterator): (CustomIterator.prototype.next): (items.Symbol.iterator): (test): * JSTests/microbenchmarks/array-from-iterable-custom-iterator.js: Added. (CustomIterator): (CustomIterator.prototype.next): (items.Symbol.iterator): (test): * JSTests/microbenchmarks/array-from-iterable-generator-with-mapfn.js: Added. (items): (test): * JSTests/microbenchmarks/array-from-iterable-generator.js: Added. (items): (test): * JSTests/microbenchmarks/array-from-iterable-map-with-mapfn.js: Added. (test): * JSTests/microbenchmarks/array-from-iterable-map.js: Added. (test): * JSTests/microbenchmarks/array-from-iterable-set-with-mapfn.js: Added. (test): * JSTests/microbenchmarks/array-from-iterable-string-with-mapfn.js: Added. (test): * JSTests/microbenchmarks/array-from-iterable-string.js: Added. (test): * JSTests/microbenchmarks/array-from-iterable-typed-array-with-mapfn.js: Added. (test): * JSTests/microbenchmarks/array-from-iterable-typed-array.js: Added. (test): * JSTests/stress/array-from-iterable-closes-iterator-and-reads-iterator-once.js: Added. (shouldBe): (makeIterable): (iterableWithGetter.get Symbol): (set var): (set new): (nonObjectIterator.Symbol.iterator): (catch): (nonObjectResult.Symbol.iterator): * JSTests/stress/builtin-iteration-closes-iterator-and-reads-iterator-once.js: Added. (shouldBe): (shouldThrow): (makeIterableWithGetter.return.get Symbol): (makeIterableWithGetter): (checkReadsIteratorOnce): (checkCloses): (i.x): (nonObjectIterable.Symbol.iterator): (nonObjectResult.Symbol.iterator): (Array.fromAsync.get Symbol): (x.x.2.then): (Array.fromAsync.Symbol.iterator): (then): * JSTests/stress/iterator-helpers-flatmap-close-iterator-on-throw.js: Added. (shouldBe): (shouldThrow): (makeIterator): (checkCloses): (i.3.flatMap): (i.3.flatMap.x.next.return.get done): * JSTests/stress/iterator-helpers-flatmap-close-iterator.js: Added. (shouldBe): (shouldThrow): (makeIterator): (checkCloses): (i.3.flatMap): (i.3.flatMap.x): * JSTests/stress/iterator-helpers-some-every-find-close-iterator.js: Added. (shouldBe): (shouldThrow): (makeIterator): (checkCloses): (i.3.some): (i.3.every): (i.3.find): * Source/JavaScriptCore/builtins/ArrayConstructor.js: (from): (linkTimeConstant.visibility.PrivateRecursive.async defaultAsyncFromAsyncIterator): (fromAsync): (from.wrapper.iterator): Deleted. (wrapper.asyncIterator): Deleted. (linkTimeConstant.visibility.PrivateRecursive.async defaultAsyncFromAsyncIteratorWithNext): Deleted. * Source/JavaScriptCore/builtins/JSIteratorPrototype.js: (flatMap.generator): (flatMap): (some): (every): (find): (flatMap.iteratedWrapper.iterator): Deleted. (flatMap.iteratedWrapper.next): Deleted. (flatMap.iteratedWrapper.return): Deleted. (flatMap.): Deleted. (some.wrapper.iterator): Deleted. (every.wrapper.iterator): Deleted. (find.wrapper.iterator): Deleted. * Source/JavaScriptCore/builtins/MapConstructor.js: (groupBy): (wrapper.iterator): Deleted. * Source/JavaScriptCore/builtins/ObjectConstructor.js: (groupBy): (wrapper.iterator): Deleted. * Source/JavaScriptCore/builtins/TypedArrayConstructor.js: (from): (from.wrapper.iterator): Deleted. Canonical link: https://commits.webkit.org/324531@main
1 parent 8ee560d commit 37aa2b5

23 files changed

Lines changed: 587 additions & 99 deletions
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function makeArguments() { return arguments; }
2+
var items = makeArguments(0, 1, 2, 3, 4, 5, 6, 7);
3+
var mapFn = (x) => x;
4+
5+
function test() {
6+
return Array.from(items, mapFn);
7+
}
8+
noInline(test);
9+
10+
var total = 0;
11+
for (var i = 0; i < 5e5; ++i)
12+
total += test().length;
13+
if (total !== 8 * 5e5)
14+
throw new Error("bad total: " + total);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var items = [0, 1, 2, 3, 4, 5, 6, 7];
2+
var mapFn = (x) => x;
3+
4+
function test() {
5+
return Array.from(items, mapFn);
6+
}
7+
noInline(test);
8+
9+
var total = 0;
10+
for (var i = 0; i < 5e5; ++i)
11+
total += test().length;
12+
if (total !== 8 * 5e5)
13+
throw new Error("bad total: " + total);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class CustomIterator {
2+
constructor(n) {
3+
this.i = 0;
4+
this.n = n;
5+
}
6+
next() { return this.i < this.n ? { value: this.i++, done: false } : { value: undefined, done: true }; }
7+
}
8+
var items = { [Symbol.iterator]() { return new CustomIterator(8); } };
9+
var mapFn = (x) => x;
10+
11+
function test() {
12+
return Array.from(items, mapFn);
13+
}
14+
noInline(test);
15+
16+
var total = 0;
17+
for (var i = 0; i < 5e5; ++i)
18+
total += test().length;
19+
if (total !== 8 * 5e5)
20+
throw new Error("bad total: " + total);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class CustomIterator {
2+
constructor(n) {
3+
this.i = 0;
4+
this.n = n;
5+
}
6+
next() { return this.i < this.n ? { value: this.i++, done: false } : { value: undefined, done: true }; }
7+
}
8+
var items = { [Symbol.iterator]() { return new CustomIterator(8); } };
9+
10+
function test() {
11+
return Array.from(items);
12+
}
13+
noInline(test);
14+
15+
var total = 0;
16+
for (var i = 0; i < 5e5; ++i)
17+
total += test().length;
18+
if (total !== 8 * 5e5)
19+
throw new Error("bad total: " + total);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function* items(n) {
2+
for (var i = 0; i < n; ++i)
3+
yield i;
4+
}
5+
var mapFn = (x) => x;
6+
7+
function test() {
8+
return Array.from(items(8), mapFn);
9+
}
10+
noInline(test);
11+
12+
var total = 0;
13+
for (var i = 0; i < 5e5; ++i)
14+
total += test().length;
15+
if (total !== 8 * 5e5)
16+
throw new Error("bad total: " + total);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function* items(n) {
2+
for (var i = 0; i < n; ++i)
3+
yield i;
4+
}
5+
6+
function test() {
7+
return Array.from(items(8));
8+
}
9+
noInline(test);
10+
11+
var total = 0;
12+
for (var i = 0; i < 5e5; ++i)
13+
total += test().length;
14+
if (total !== 8 * 5e5)
15+
throw new Error("bad total: " + total);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var items = new Map([[0, 0], [1, 1], [2, 2], [3, 3], [4, 4], [5, 5], [6, 6], [7, 7]]);
2+
var mapFn = (x) => x;
3+
4+
function test() {
5+
return Array.from(items, mapFn);
6+
}
7+
noInline(test);
8+
9+
var total = 0;
10+
for (var i = 0; i < 5e5; ++i)
11+
total += test().length;
12+
if (total !== 8 * 5e5)
13+
throw new Error("bad total: " + total);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var items = new Map([[0, 0], [1, 1], [2, 2], [3, 3], [4, 4], [5, 5], [6, 6], [7, 7]]);
2+
3+
function test() {
4+
return Array.from(items);
5+
}
6+
noInline(test);
7+
8+
var total = 0;
9+
for (var i = 0; i < 5e5; ++i)
10+
total += test().length;
11+
if (total !== 8 * 5e5)
12+
throw new Error("bad total: " + total);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var items = new Set([0, 1, 2, 3, 4, 5, 6, 7]);
2+
var mapFn = (x) => x;
3+
4+
function test() {
5+
return Array.from(items, mapFn);
6+
}
7+
noInline(test);
8+
9+
var total = 0;
10+
for (var i = 0; i < 5e5; ++i)
11+
total += test().length;
12+
if (total !== 8 * 5e5)
13+
throw new Error("bad total: " + total);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var items = "abcdefgh";
2+
var mapFn = (x) => x;
3+
4+
function test() {
5+
return Array.from(items, mapFn);
6+
}
7+
noInline(test);
8+
9+
var total = 0;
10+
for (var i = 0; i < 5e5; ++i)
11+
total += test().length;
12+
if (total !== 8 * 5e5)
13+
throw new Error("bad total: " + total);

0 commit comments

Comments
 (0)