Skip to content

Commit

Permalink
Normative: Reduce the number of ticks in async/await
Browse files Browse the repository at this point in the history
JavaScript programmers may expect that the following
two programs are largely similar in terms of how they perform with
respect to the ECMAScript job queue (if inside of an async function):

promise.then(f);                f(await promise);

However, if `promise` is a built-in promise, then these two code fragments
will differ in the number of iterations through the job queue are taken: because
`await` always wraps a Promise with another Promise, there are three job
queue items enqueued and dequeued before calling `f` in the `await` example,
whereas there is just a single item for the `then` usage.

In discussions with JavaScript programmers, the number of job queue items
in the current semantics turns out to be surprising. For example, the difference
has become more visible in conjunction with new V8 features for Promise
visibility and debugging, which are sometimes used in Node.js.

This patch changes the semantics of await to reduce the number of
job queue turns that are taken in the common `await` Promise case by replacing
the unconditional wrapping with a call to PromiseResolve. Analogous changes
are made in async iterators.

The patch preserves key design goals of async/await:
- Job queue processing remains deterministic, including both ordering and the number of jobs enqueued (which is observable by interspersing other jobs)
- Userland Promise libraries with "then" methods ("foreign thenables") are usable within `await`, and trigger a turn of the job queue
- Non-Promises can be awaited, and this takes a turn of the native job queue (as do all usages of `await`)

Reducing the number of job queue turns also improves performance
on multiple highly optimized async/await implementations. In a draft
implementation of this proposal in V8 behind a flag [1]:
- The doxbee async/await performance benchmark [2] improved with 48%
- The fibonacci async/await performance benchmark [3] improved with 23%
- The Hapi throughput benchmark [4] improved with 50% (when async hooks are enabled) and with 20% (when async hooks are disabled)

[1] https://chromium-review.googlesource.com/c/v8/v8/+/1106977
[2] https://github.com/bmeurer/promise-performance-tests/blob/master/lib/doxbee-async.js
[3] https://github.com/bmeurer/promise-performance-tests/blob/master/lib/fibonacci-async.js
[4] https://github.com/fastify/benchmarks
  • Loading branch information
MayaLekova committed Jun 28, 2018
1 parent 4a0b2ad commit 320b6c2
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions spec.html
Original file line number Diff line number Diff line change
Expand Up @@ -3045,15 +3045,14 @@ <h1>Await</h1>
<p>Algorithm steps that say</p>

<emu-alg>
1. Let _completion_ be Await(_promise_).
1. Let _completion_ be Await(_value_).
</emu-alg>

<p>mean the same thing as:</p>

<emu-alg>
1. Let _asyncContext_ be the running execution context.
1. Let _promiseCapability_ be ! NewPromiseCapability(%Promise%).
1. Perform ! Call(_promiseCapability_.[[Resolve]], *undefined*, &laquo; _promise_ &raquo;).
1. Let _promise_ be ? PromiseResolve(&laquo; _value_ &raquo;).
1. Let _stepsFulfilled_ be the algorithm steps defined in <emu-xref href="#await-fulfilled" title></emu-xref>.
1. Let _onFulfilled_ be CreateBuiltinFunction(_stepsFulfilled_, &laquo; [[AsyncContext]] &raquo;).
1. Set _onFulfilled_.[[AsyncContext]] to _asyncContext_.
Expand All @@ -3062,7 +3061,7 @@ <h1>Await</h1>
1. Set _onRejected_.[[AsyncContext]] to _asyncContext_.
1. Let _throwawayCapability_ be ! NewPromiseCapability(%Promise%).
1. Set _throwawayCapability_.[[Promise]].[[PromiseIsHandled]] to *true*.
1. Perform ! PerformPromiseThen(_promiseCapability_.[[Promise]], _onFulfilled_, _onRejected_, _throwawayCapability_).
1. Perform ! PerformPromiseThen(_promise_, _onFulfilled_, _onRejected_, _throwawayCapability_).
1. Remove _asyncContext_ from the execution context stack and restore the execution context that is at the top of the execution context stack as the running execution context.
1. Set the code evaluation state of _asyncContext_ such that when evaluation is resumed with a Completion _completion_, the following steps of the algorithm that invoked Await will be performed, with _completion_ available.
</emu-alg>
Expand All @@ -3073,14 +3072,14 @@ <h1>Await</h1>
<p>Await can be combined with the `?` and `!` prefixes, so that for example</p>

<emu-alg>
1. Let _value_ be ? Await(_promise_).
1. Let _result_ be ? Await(_value_).
</emu-alg>

<p>means the same thing as:</p>

<emu-alg>
1. Let _value_ be Await(_promise_).
1. ReturnIfAbrupt(_value_).
1. Let _result_ be Await(_value_).
1. ReturnIfAbrupt(_result_).
</emu-alg>
</emu-note>

Expand Down Expand Up @@ -37908,12 +37907,11 @@ <h1>%AsyncFromSyncIteratorPrototype%.next ( _value_ )</h1>
1. IfAbruptRejectPromise(_nextDone_, _promiseCapability_).
1. Let _nextValue_ be IteratorValue(_nextResult_).
1. IfAbruptRejectPromise(_nextValue_, _promiseCapability_).
1. Let _valueWrapperCapability_ be ! NewPromiseCapability(%Promise%).
1. Perform ! Call(_valueWrapperCapability_.[[Resolve]], *undefined*, &laquo; _nextValue_ &raquo;).
1. Let _valueWrapper_ be ? PromiseResolve(&laquo; _nextValue_ &raquo;).
1. Let _steps_ be the algorithm steps defined in <emu-xref href="#sec-async-from-sync-iterator-value-unwrap-functions" title></emu-xref>.
1. Let _onFulfilled_ be CreateBuiltinFunction(_steps_, &laquo; [[Done]] &raquo;).
1. Set _onFulfilled_.[[Done]] to _nextDone_.
1. Perform ! PerformPromiseThen(_valueWrapperCapability_.[[Promise]], _onFulfilled_, *undefined*, _promiseCapability_).
1. Perform ! PerformPromiseThen(_valueWrapper_, _onFulfilled_, *undefined*, _promiseCapability_).
1. Return _promiseCapability_.[[Promise]].
</emu-alg>
</emu-clause>
Expand Down Expand Up @@ -37944,12 +37942,11 @@ <h1>%AsyncFromSyncIteratorPrototype%.return ( _value_ )</h1>
1. IfAbruptRejectPromise(_returnDone_, _promiseCapability_).
1. Let _returnValue_ be IteratorValue(_returnResult_).
1. IfAbruptRejectPromise(_returnValue_, _promiseCapability_).
1. Let _valueWrapperCapability_ be ! NewPromiseCapability(%Promise%).
1. Perform ! Call(_valueWrapperCapability_.[[Resolve]], *undefined*, &laquo; _returnValue_ &raquo;).
1. Let _valueWrapper_ be ? PromiseResolve(&laquo; _returnValue_ &raquo;).
1. Let _steps_ be the algorithm steps defined in <emu-xref href="#sec-async-from-sync-iterator-value-unwrap-functions" title></emu-xref>.
1. Let _onFulfilled_ be CreateBuiltinFunction(_steps_, &laquo; [[Done]] &raquo;).
1. Set _onFulfilled_.[[Done]] to _returnDone_.
1. Perform ! PerformPromiseThen(_valueWrapperCapability_.[[Promise]], _onFulfilled_, *undefined*, _promiseCapability_).
1. Perform ! PerformPromiseThen(_valueWrapper_, _onFulfilled_, *undefined*, _promiseCapability_).
1. Return _promiseCapability_.[[Promise]].
</emu-alg>
</emu-clause>
Expand Down Expand Up @@ -37979,12 +37976,11 @@ <h1>%AsyncFromSyncIteratorPrototype%.throw ( _value_ )</h1>
1. IfAbruptRejectPromise(_throwDone_, _promiseCapability_).
1. Let _throwValue_ be IteratorValue(_throwResult_).
1. IfAbruptRejectPromise(_throwValue_, _promiseCapability_).
1. Let _valueWrapperCapability_ be ! NewPromiseCapability(%Promise%).
1. Perform ! Call(_valueWrapperCapability_.[[Resolve]], *undefined*, &laquo; _throwValue_ &raquo;).
1. Let _valueWrapper_ be ? PromiseResolve(&laquo; _throwValue_ &raquo;).
1. Let _steps_ be the algorithm steps defined in <emu-xref href="#sec-async-from-sync-iterator-value-unwrap-functions" title></emu-xref>.
1. Let _onFulfilled_ be CreateBuiltinFunction(_steps_, &laquo; [[Done]] &raquo;).
1. Set _onFulfilled_.[[Done]] to _throwDone_.
1. Perform ! PerformPromiseThen(_valueWrapperCapability_.[[Promise]], _onFulfilled_, *undefined*, _promiseCapability_).
1. Perform ! PerformPromiseThen(_valueWrapper_, _onFulfilled_, *undefined*, _promiseCapability_).
1. Return _promiseCapability_.[[Promise]].
</emu-alg>
</emu-clause>
Expand Down Expand Up @@ -38687,8 +38683,7 @@ <h1>AsyncGeneratorResumeNext ( _generator_ )</h1>
1. If _state_ is `"completed"`, then
1. If _completion_.[[Type]] is ~return~, then
1. Set _generator_.[[AsyncGeneratorState]] to `"awaiting-return"`.
1. Let _promiseCapability_ be ! NewPromiseCapability(%Promise%).
1. Perform ! Call(_promiseCapability_.[[Resolve]], *undefined*, &laquo; _completion_.[[Value]] &raquo;).
1. Let _promise_ be ? PromiseResolve(&laquo; _completion_.[[Value]] &raquo;).
1. Let _stepsFulfilled_ be the algorithm steps defined in <emu-xref href="#async-generator-resume-next-return-processor-fulfilled" title></emu-xref>.
1. Let _onFulfilled_ be CreateBuiltinFunction(_stepsFulfilled_, &laquo; [[Generator]] &raquo;).
1. Set _onFulfilled_.[[Generator]] to _generator_.
Expand All @@ -38697,7 +38692,7 @@ <h1>AsyncGeneratorResumeNext ( _generator_ )</h1>
1. Set _onRejected_.[[Generator]] to _generator_.
1. Let _throwawayCapability_ be ! NewPromiseCapability(%Promise%).
1. Set _throwawayCapability_.[[Promise]].[[PromiseIsHandled]] to *true*.
1. Perform ! PerformPromiseThen(_promiseCapability_.[[Promise]], _onFulfilled_, _onRejected_, _throwawayCapability_).
1. Perform ! PerformPromiseThen(_promise_, _onFulfilled_, _onRejected_, _throwawayCapability_).
1. Return *undefined*.
1. Else,
1. Assert: _completion_.[[Type]] is ~throw~.
Expand Down

0 comments on commit 320b6c2

Please sign in to comment.