Commit 764548e2 authored by gsathya's avatar gsathya Committed by Commit bot

[async-await] Don't create resolving callbacks for throwaway promises

This patch also cleans up NewPromiseCapability.

This patch results in a 20% improvement over 4 runs with the following micro
benchmark -

var x = Promise.resolve();

async function bar() {
    return x;
}

async function foo() {
    await bar();
}

var start = performance.now();
var count = 0;
var max = 10000;
for(var i = 0; i <= max; i++) {
    foo().then(() => {
        count++;
        if(count === max) print( performance.now() - start );
    })
}

BUG=v8:5639

Review-Url: https://codereview.chromium.org/2512103002
Cr-Commit-Position: refs/heads/master@{#41116}
parent 8ab945f2
......@@ -15,7 +15,7 @@ var AsyncFunctionNext;
var AsyncFunctionThrow;
var GlobalPromise;
var IsPromise;
var NewPromiseCapability;
var CreateInternalPromiseCapability;
var PerformPromiseThen;
var PromiseCreate;
var PromiseNextMicrotaskID;
......@@ -27,7 +27,7 @@ utils.Import(function(from) {
AsyncFunctionThrow = from.AsyncFunctionThrow;
GlobalPromise = from.GlobalPromise;
IsPromise = from.IsPromise;
NewPromiseCapability = from.NewPromiseCapability;
CreateInternalPromiseCapability = from.CreateInternalPromiseCapability;
PerformPromiseThen = from.PerformPromiseThen;
PromiseCreate = from.PromiseCreate;
RejectPromise = from.RejectPromise;
......@@ -90,8 +90,8 @@ function AsyncFunctionAwait(generator, awaited, outerPromise) {
return;
}
// Just forwarding the exception, so no debugEvent for throwawayCapability
var throwawayCapability = NewPromiseCapability(GlobalPromise, false);
// Just forwarding the exception, so no debugEvent for throwawayCapability.
var throwawayCapability = CreateInternalPromiseCapability();
// The Promise will be thrown away and not handled, but it shouldn't trigger
// unhandled reject events as its work is done
......
......@@ -132,8 +132,8 @@ function PromiseHandle(value, handler, deferred) {
} %catch (exception) { // Natives syntax to mark this catch block.
try {
if (IS_UNDEFINED(deferred.reject)) {
// Pass false for debugEvent so .then chaining does not trigger
// redundant ExceptionEvents.
// Pass false for debugEvent so .then chaining or throwaway promises
// in async functions do not trigger redundant ExceptionEvents.
%PromiseReject(deferred.promise, exception, false);
PromiseSet(deferred.promise, kRejected, exception);
} else {
......@@ -291,6 +291,17 @@ function DoRejectPromise(promise, reason) {
PromiseSet(promise, kRejected, reason);
}
// The resultCapability.promise is only ever fulfilled internally,
// so we don't need the closures to protect against accidentally
// calling them multiple times.
function CreateInternalPromiseCapability() {
return {
promise: PromiseCreate(),
resolve: UNDEFINED,
reject: UNDEFINED
};
}
// ES#sec-newpromisecapability
// NewPromiseCapability ( C )
function NewPromiseCapability(C, debugEvent) {
......@@ -381,17 +392,8 @@ function PromiseThen(onResolve, onReject) {
var constructor = SpeciesConstructor(this, GlobalPromise);
var resultCapability;
// The resultCapability.promise is only ever fulfilled internally,
// so we don't need the closures to protect against accidentally
// calling them multiple times.
if (constructor === GlobalPromise) {
// TODO(gsathya): Combine this into NewPromiseCapability.
resultCapability = {
promise: PromiseCreate(),
resolve: UNDEFINED,
reject: UNDEFINED
};
resultCapability = CreateInternalPromiseCapability();
} else {
// Pass false for debugEvent so .then chaining does not trigger
// redundant ExceptionEvents.
......@@ -646,7 +648,7 @@ utils.Export(function(to) {
to.PromiseThen = PromiseThen;
to.GlobalPromise = GlobalPromise;
to.NewPromiseCapability = NewPromiseCapability;
to.CreateInternalPromiseCapability = CreateInternalPromiseCapability;
to.PerformPromiseThen = PerformPromiseThen;
to.ResolvePromise = ResolvePromise;
to.RejectPromise = RejectPromise;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment