d8658177ba
In Promise.all we used to allocate a fresh closure plus a fresh context for each individual element, which is quite a lot of overhead, especially since this could be shared in a single context for all elements. The only bit of information that is needed for each resolve element closure is the index under which to store the resulting value. With this change we move this index to the "identity hash" field of the JSFunction, which doesn't care about the concrete value anyways, as long as it's not zero (the "no hash" sentinel), and share the rest of the fields in a single outer context for all resolve element closures. This limits the maximum number of elements for Promise.all to 2^21 for now, but that should be fine. Shall we ever see the need for more than this, we can add machinery to overflow to separate context for indices larger than 2^21. This significantly reduces the overhead due to Promise.all on the parallel-async-es2017-native test, with execution time dropping from around 148ms to 133ms, so overall a steady 10% improvement on this benchmark. Bug: v8:7253 Change-Id: I1092da771c4919f3db7129d2b0a244fc26a7b144 Reviewed-on: https://chromium-review.googlesource.com/973283 Reviewed-by: Yang Guo <yangguo@chromium.org> Reviewed-by: Benedikt Meurer <bmeurer@chromium.org> Commit-Queue: Benedikt Meurer <bmeurer@chromium.org> Cr-Commit-Position: refs/heads/master@{#52134}
85 lines
2.2 KiB
JavaScript
85 lines
2.2 KiB
JavaScript
// Copyright 2018 the V8 project authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
// Flags: --allow-natives-syntax
|
|
|
|
// We store the index in the hash code field of the Promise.all resolve
|
|
// element closures, so make sure we properly handle the cases where this
|
|
// magical field turns into a PropertyArray later.
|
|
(function() {
|
|
class MyPromise extends Promise {
|
|
then(resolve, reject) {
|
|
this.resolve = resolve;
|
|
}
|
|
};
|
|
const myPromise = new MyPromise(() => {});
|
|
MyPromise.all([myPromise]);
|
|
myPromise.resolve.x = 1;
|
|
myPromise.resolve(1);
|
|
})();
|
|
|
|
// Same test as above, but for PropertyDictionary.
|
|
(function() {
|
|
class MyPromise extends Promise {
|
|
then(resolve, reject) {
|
|
this.resolve = resolve;
|
|
}
|
|
};
|
|
const myPromise = new MyPromise(() => {});
|
|
MyPromise.all([myPromise]);
|
|
for (let i = 0; i < 1025; ++i) {
|
|
myPromise.resolve[`x${i}`] = i;
|
|
}
|
|
myPromise.resolve(1);
|
|
})();
|
|
|
|
// Test that we return a proper array even if (custom) "then" invokes the
|
|
// resolve callbacks right away.
|
|
(function() {
|
|
class MyPromise extends Promise {
|
|
constructor(executor, id) {
|
|
super(executor);
|
|
this.id = id;
|
|
}
|
|
|
|
then(resolve, reject) {
|
|
if (this.id) return resolve(this.id);
|
|
return super.then(resolve, reject)
|
|
}
|
|
};
|
|
const a = new MyPromise(() => {}, 'a');
|
|
const b = new MyPromise(() => {}, 'b');
|
|
testAsync(assert => {
|
|
assert.plan(1);
|
|
MyPromise.all([a, b]).then(
|
|
v => assert.equals(['a', 'b'], v),
|
|
assert.unexpectedRejection());
|
|
});
|
|
})();
|
|
|
|
// Test that we properly handle holes introduced into the resulting array
|
|
// by resolving some late elements immediately.
|
|
(function() {
|
|
class MyPromise extends Promise {
|
|
then(resolve, reject) {
|
|
if (this.immediately) {
|
|
resolve(42);
|
|
} else {
|
|
super.then(resolve, reject);
|
|
}
|
|
}
|
|
};
|
|
const a = new Array(1024);
|
|
a.fill(MyPromise.resolve(1));
|
|
const p = MyPromise.resolve(0);
|
|
p.immediately = true;
|
|
a.push(p);
|
|
testAsync(assert => {
|
|
assert.plan(1);
|
|
MyPromise.all(a).then(
|
|
b => assert.equals(42, b[1024]),
|
|
assert.unexpectedRejection());
|
|
});
|
|
})();
|