Title: [295011] trunk
Revision
295011
Author
[email protected]
Date
2022-05-29 01:14:12 -0700 (Sun, 29 May 2022)

Log Message

[JSC] Async / Await should not wrap awaited value with one tick
https://bugs.webkit.org/show_bug.cgi?id=241072

Reviewed by Saam Barati.

This patch integrates spec change[1], which removes one level tick count when resolving promise with await.
Previously, regardless of whether the value is promise or not, we are always using resolveWithoutPromise,
but it introduces one tick before the handlers are resolved. The spec change makes it that we can call
performPromiseThen directly if the input value is promise, so we can skip one tick which looks up "then"
and register handlers.

This is beneficial for await performance and it also fixes a bug tested via test262 and attached test due
to the spec change.

We observed performance improvement in async + native promise tests.

    ToT
    Time(doxbee-async-es2017-native): 35.6 ms.
    Time(fibonacci-async-es2017-native): 292.3 ms.
    Time(parallel-async-es2017-native): 117.3 ms.

    Patched
    Time(doxbee-async-es2017-native): 24.2 ms.
    Time(fibonacci-async-es2017-native): 198.1 ms.
    Time(parallel-async-es2017-native): 109.5 ms.

[1]: https://github.com/tc39/ecma262/pull/1250

* JSTests/stress/async-await-basic.js:
* JSTests/stress/async-await-tick-count.js: Added.
(shouldBe):
(async returnDirectPrimitive):
(async returnAwaitPrimitive):
(async returnDirectPromisePrimitive):
(async returnAwaitPromisePrimitive):
(async test):
(async tests):
(globalThis.setUnhandledRejectionCallback.setUnhandledRejectionCallback):
* JSTests/test262/expectations.yaml:
* LayoutTests/inspector/canvas/recording-bitmaprenderer-frameCount-expected.txt:
* LayoutTests/inspector/canvas/recording-bitmaprenderer-full-expected.txt:
* LayoutTests/inspector/canvas/recording-bitmaprenderer-memoryLimit-expected.txt:
* LayoutTests/inspector/console/message-stack-trace-expected.txt:
* Source/_javascript_Core/builtins/AsyncFromSyncIteratorPrototype.js:
* Source/_javascript_Core/builtins/AsyncFunctionPrototype.js:
(globalPrivate.asyncFunctionResume):
* Source/_javascript_Core/builtins/AsyncGeneratorPrototype.js:
(globalPrivate.awaitValue):
(globalPrivate.asyncGeneratorResumeNext):
* Source/_javascript_Core/builtins/PromiseOperations.js:
(globalPrivate.newPromiseCapabilitySlow):
(globalPrivate.promiseResolve):
(globalPrivate.promiseResolveSlow):
(globalPrivate.promiseRejectSlow):
(globalPrivate.resolvePromiseWithFirstResolvingFunctionCallCheck):
(globalPrivate.fulfillPromiseWithFirstResolvingFunctionCallCheck):
(globalPrivate.rejectPromiseWithFirstResolvingFunctionCallCheck):
(globalPrivate.resolveWithoutPromiseForAsyncAwait):

Canonical link: https://commits.webkit.org/251106@main

Modified Paths

Added Paths

Diff

Modified: trunk/JSTests/stress/async-await-basic.js (295010 => 295011)


--- trunk/JSTests/stress/async-await-basic.js	2022-05-29 07:57:37 UTC (rev 295010)
+++ trunk/JSTests/stress/async-await-basic.js	2022-05-29 08:14:12 UTC (rev 295011)
@@ -332,4 +332,4 @@
 awaitedPromisesAreWrapped();
 Promise.resolve().then(() => log.push("Promise.resolve()"));
 drainMicrotasks();
-shouldBe("before|Promise.resolve()|after", log.join("|"));
\ No newline at end of file
+shouldBe("before|after|Promise.resolve()", log.join("|"));

Added: trunk/JSTests/stress/async-await-tick-count.js (0 => 295011)


--- trunk/JSTests/stress/async-await-tick-count.js	                        (rev 0)
+++ trunk/JSTests/stress/async-await-tick-count.js	2022-05-29 08:14:12 UTC (rev 295011)
@@ -0,0 +1,60 @@
+if (globalThis.console)
+    globalThis.print = console.log.bind(console);
+
+function shouldBe(actual, expected) {
+    if (actual !== expected)
+        throw new Error('bad value: ' + actual);
+}
+
+async function returnDirectPrimitive() {
+    return 1;
+}
+
+async function returnAwaitPrimitive() {
+    return await 1;
+}
+
+async function returnDirectPromisePrimitive() {
+    return Promise.resolve(1);
+}
+
+async function returnAwaitPromisePrimitive() {
+    return await Promise.resolve(1);
+}
+
+const resolved = Promise.resolve();
+
+async function test(fn, expected) {
+    let done = false;
+    let count = 0;
+    fn().then(() => { done = true; });
+
+    function counter() {
+        if (done)
+            shouldBe(count, expected);
+        else {
+            resolved.then(() => {
+                count++;
+                counter();
+            });
+        }
+    }
+    counter();
+}
+
+async function tests() {
+    await resolved;
+    await test(returnDirectPrimitive, 1);
+    await test(returnAwaitPrimitive, 2);
+
+    await test(returnDirectPromisePrimitive, 3);
+    await test(returnAwaitPromisePrimitive, 2);
+}
+
+if (globalThis.setUnhandledRejectionCallback) {
+    setUnhandledRejectionCallback(function (promise) {
+        $vm.abort();
+    });
+}
+
+tests();

Modified: trunk/JSTests/test262/expectations.yaml (295010 => 295011)


--- trunk/JSTests/test262/expectations.yaml	2022-05-29 07:57:37 UTC (rev 295010)
+++ trunk/JSTests/test262/expectations.yaml	2022-05-29 08:14:12 UTC (rev 295011)
@@ -1815,18 +1815,6 @@
   strict mode: 'Test262Error: Expected SameValue(«[object AsyncGenerator]», «[object AsyncGenerator]») to be false'
 test/language/expressions/async-generator/named-eval-var-scope-syntax-err.js:
   default: 'Test262Error: Expected a SyntaxError to be thrown but no exception was thrown at all'
-test/language/expressions/await/async-await-interleaved.js:
-  default: 'Test262:AsyncTestFailure:Test262Error: Test262Error: Expected [Await: 1, Promise: 1, Promise: 2, Await: 2] and [Await: 1, Promise: 1, Await: 2, Promise: 2] to have the same contents. Async/await and promises should be interleaved'
-  strict mode: 'Test262:AsyncTestFailure:Test262Error: Test262Error: Expected [Await: 1, Promise: 1, Promise: 2, Await: 2] and [Await: 1, Promise: 1, Await: 2, Promise: 2] to have the same contents. Async/await and promises should be interleaved'
-test/language/expressions/await/async-generator-interleaved.js:
-  default: 'Test262:AsyncTestFailure:Test262Error: Test262Error: Expected [await, 1, 2, await] and [await, 1, await, 2] to have the same contents. Async/await and promises should be interleaved'
-  strict mode: 'Test262:AsyncTestFailure:Test262Error: Test262Error: Expected [await, 1, 2, await] and [await, 1, await, 2] to have the same contents. Async/await and promises should be interleaved'
-test/language/expressions/await/await-monkey-patched-promise.js:
-  default: 'Test262:AsyncTestFailure:Test262Error: Test262Error: Expected [Promise: 1, Promise: 2, Await: 42] and [Promise: 1, Await: 42, Promise: 2] to have the same contents. Async/await and promises should be interleaved'
-  strict mode: 'Test262:AsyncTestFailure:Test262Error: Test262Error: Expected [Promise: 1, Promise: 2, Await: 42] and [Promise: 1, Await: 42, Promise: 2] to have the same contents. Async/await and promises should be interleaved'
-test/language/expressions/await/for-await-of-interleaved.js:
-  default: 'Test262:AsyncTestFailure:Test262Error: Test262Error: Expected [Promise: 6, Promise: 5, Promise: 4, Await: 3, Promise: 3, Promise: 2, Promise: 1, Await: 2, Promise: 0] and [Promise: 6, Promise: 5, Await: 3, Promise: 4, Promise: 3, Await: 2, Promise: 2, Promise: 1, Await: 1, Promise: 0] to have the same contents. Async/await and promises should be interleaved'
-  strict mode: 'Test262:AsyncTestFailure:Test262Error: Test262Error: Expected [Promise: 6, Promise: 5, Promise: 4, Await: 3, Promise: 3, Promise: 2, Promise: 1, Await: 2, Promise: 0] and [Promise: 6, Promise: 5, Await: 3, Promise: 4, Promise: 3, Await: 2, Promise: 2, Promise: 1, Await: 1, Promise: 0] to have the same contents. Async/await and promises should be interleaved'
 test/language/expressions/call/eval-realm-indirect.js:
   default: 'Test262Error: Expected SameValue(«inside», «outside») to be true'
 test/language/expressions/call/eval-spread-empty-leading.js:
@@ -2061,18 +2049,9 @@
 test/language/statements/const/dstr/ary-ptrn-elem-id-iter-val-array-prototype.js:
   default: 'Test262Error: Expected SameValue(«3», «42») to be true'
   strict mode: 'Test262Error: Expected SameValue(«3», «42») to be true'
-test/language/statements/for-await-of/async-from-sync-iterator-continuation-abrupt-completion-get-constructor.js:
-  default: 'Test262:AsyncTestFailure:Test262Error: Test262Error: Expected [start, tick 1, tick 2] and [start, tick 1, tick 2, catch] to have the same contents. '
-  strict mode: 'Test262:AsyncTestFailure:Test262Error: Test262Error: Expected [start, tick 1, tick 2] and [start, tick 1, tick 2, catch] to have the same contents. '
 test/language/statements/for-await-of/head-lhs-async.js:
   default: "SyntaxError: Unexpected identifier 'of'"
   strict mode: "SyntaxError: Unexpected identifier 'of'"
-test/language/statements/for-await-of/ticks-with-async-iter-resolved-promise-and-constructor-lookup-two.js:
-  default: 'Test262:AsyncTestFailure:Test262Error: Test262Error: Expected [pre, tick 1, constructor, tick 2, loop] and [pre, constructor, tick 1, loop, constructor, tick 2, post] to have the same contents. Ticks and constructor lookups'
-  strict mode: 'Test262:AsyncTestFailure:Test262Error: Test262Error: Expected [pre, tick 1, constructor, tick 2, loop] and [pre, constructor, tick 1, loop, constructor, tick 2, post] to have the same contents. Ticks and constructor lookups'
-test/language/statements/for-await-of/ticks-with-sync-iter-resolved-promise-and-constructor-lookup.js:
-  default: 'Test262:AsyncTestFailure:Test262Error: Test262Error: Expected [pre, tick 1, constructor, constructor, tick 2, tick 3, loop, tick 4, constructor] and [pre, constructor, constructor, tick 1, tick 2, loop, constructor, tick 3, tick 4, post] to have the same contents. Ticks and constructor lookups'
-  strict mode: 'Test262:AsyncTestFailure:Test262Error: Test262Error: Expected [pre, tick 1, constructor, constructor, tick 2, tick 3, loop, tick 4, constructor] and [pre, constructor, constructor, tick 1, tick 2, loop, constructor, tick 3, tick 4, post] to have the same contents. Ticks and constructor lookups'
 test/language/statements/for-in/head-lhs-let.js:
   default: "SyntaxError: Cannot use the keyword 'in' as a lexical variable name."
 test/language/statements/for-in/identifier-let-allowed-as-lefthandside-_expression_-not-strict.js:

Modified: trunk/LayoutTests/inspector/canvas/recording-bitmaprenderer-frameCount-expected.txt (295010 => 295011)


--- trunk/LayoutTests/inspector/canvas/recording-bitmaprenderer-frameCount-expected.txt	2022-05-29 07:57:37 UTC (rev 295010)
+++ trunk/LayoutTests/inspector/canvas/recording-bitmaprenderer-frameCount-expected.txt	2022-05-29 08:14:12 UTC (rev 295011)
@@ -22,5 +22,6 @@
         4: asyncFunctionResume
         5: (anonymous function)
         6: promiseReactionJobWithoutPromise
+        7: promiseReactionJob
       snapshot: <filtered>
 

Modified: trunk/LayoutTests/inspector/canvas/recording-bitmaprenderer-full-expected.txt (295010 => 295011)


--- trunk/LayoutTests/inspector/canvas/recording-bitmaprenderer-full-expected.txt	2022-05-29 07:57:37 UTC (rev 295010)
+++ trunk/LayoutTests/inspector/canvas/recording-bitmaprenderer-full-expected.txt	2022-05-29 08:14:12 UTC (rev 295011)
@@ -22,6 +22,7 @@
         4: asyncFunctionResume
         5: (anonymous function)
         6: promiseReactionJobWithoutPromise
+        7: promiseReactionJob
       snapshot: <filtered>
   1: (duration)
     0: width

Modified: trunk/LayoutTests/inspector/canvas/recording-bitmaprenderer-memoryLimit-expected.txt (295010 => 295011)


--- trunk/LayoutTests/inspector/canvas/recording-bitmaprenderer-memoryLimit-expected.txt	2022-05-29 07:57:37 UTC (rev 295010)
+++ trunk/LayoutTests/inspector/canvas/recording-bitmaprenderer-memoryLimit-expected.txt	2022-05-29 08:14:12 UTC (rev 295011)
@@ -22,5 +22,6 @@
         4: asyncFunctionResume
         5: (anonymous function)
         6: promiseReactionJobWithoutPromise
+        7: promiseReactionJob
       snapshot: <filtered>
 

Modified: trunk/LayoutTests/inspector/console/message-stack-trace-expected.txt (295010 => 295011)


--- trunk/LayoutTests/inspector/console/message-stack-trace-expected.txt	2022-05-29 07:57:37 UTC (rev 295010)
+++ trunk/LayoutTests/inspector/console/message-stack-trace-expected.txt	2022-05-29 08:14:12 UTC (rev 295011)
@@ -27,18 +27,16 @@
 CALL STACK:
 0: [N] (anonymous function)
 1: [N] rejectPromise
-2: [N] rejectPromiseWithFirstResolvingFunctionCallCheck
-3: [N] reject
-4: [F] triggerUnhandledRejectionPromiseReject
+2: [N] reject
+3: [F] triggerUnhandledRejectionPromiseReject
 
 -- Running test case: Console.StackTrace.UnhandledPromiseRejection.ExplicitReject
 CALL STACK:
 0: [N] (anonymous function)
 1: [N] rejectPromise
-2: [N] rejectPromiseWithFirstResolvingFunctionCallCheck
-3: [F] (anonymous function)
-4: [N] Promise
-5: [F] triggerUnhandledRejectionExplicit
+2: [F] (anonymous function)
+3: [N] Promise
+4: [F] triggerUnhandledRejectionExplicit
 
 -- Running test case: Console.StackTrace.UnhandledPromiseRejection.ImplicitReject
 CALL STACK:

Modified: trunk/Source/_javascript_Core/builtins/AsyncFromSyncIteratorPrototype.js (295010 => 295011)


--- trunk/Source/_javascript_Core/builtins/AsyncFromSyncIteratorPrototype.js	2022-05-29 07:57:37 UTC (rev 295010)
+++ trunk/Source/_javascript_Core/builtins/AsyncFromSyncIteratorPrototype.js	2022-05-29 08:14:12 UTC (rev 295011)
@@ -42,7 +42,7 @@
         var nextResult = @argumentCount() === 0 ? nextMethod.@call(syncIterator) : nextMethod.@call(syncIterator, value);
         var nextDone = !!nextResult.done;
         var nextValue = nextResult.value;
-        @resolveWithoutPromise(nextValue,
+        @resolveWithoutPromiseForAsyncAwait(nextValue,
             function (result) { @resolvePromiseWithFirstResolvingFunctionCallCheck(promise, { value: result, done: nextDone }); },
             function (error) { @rejectPromiseWithFirstResolvingFunctionCallCheck(promise, error); });
     } catch (e) {
@@ -89,7 +89,7 @@
 
         var resultDone = !!returnResult.done;
         var resultValue = returnResult.value;
-        @resolveWithoutPromise(resultValue,
+        @resolveWithoutPromiseForAsyncAwait(resultValue,
             function (result) { @resolvePromiseWithFirstResolvingFunctionCallCheck(promise, { value: result, done: resultDone }); },
             function (error) { @rejectPromiseWithFirstResolvingFunctionCallCheck(promise, error); });
     } catch (e) {
@@ -136,7 +136,7 @@
         
         var throwDone = !!throwResult.done;
         var throwValue = throwResult.value;
-        @resolveWithoutPromise(throwValue,
+        @resolveWithoutPromiseForAsyncAwait(throwValue,
             function (result) { @resolvePromiseWithFirstResolvingFunctionCallCheck(promise, { value: result, done: throwDone }); },
             function (error) { @rejectPromiseWithFirstResolvingFunctionCallCheck(promise, error); });
     } catch (e) {

Modified: trunk/Source/_javascript_Core/builtins/AsyncFunctionPrototype.js (295010 => 295011)


--- trunk/Source/_javascript_Core/builtins/AsyncFunctionPrototype.js	2022-05-29 07:57:37 UTC (rev 295010)
+++ trunk/Source/_javascript_Core/builtins/AsyncFunctionPrototype.js	2022-05-29 08:14:12 UTC (rev 295011)
@@ -48,7 +48,7 @@
 
     var capturedGenerator = generator;
     var capturedPromise = promise;
-    @resolveWithoutPromise(value,
+    @resolveWithoutPromiseForAsyncAwait(value,
         function(value) { @asyncFunctionResume(capturedGenerator, capturedPromise, value, @GeneratorResumeModeNormal); },
         function(error) { @asyncFunctionResume(capturedGenerator, capturedPromise, error, @GeneratorResumeModeThrow); });
 

Modified: trunk/Source/_javascript_Core/builtins/AsyncGeneratorPrototype.js (295010 => 295011)


--- trunk/Source/_javascript_Core/builtins/AsyncGeneratorPrototype.js	2022-05-29 07:57:37 UTC (rev 295010)
+++ trunk/Source/_javascript_Core/builtins/AsyncGeneratorPrototype.js	2022-05-29 08:14:12 UTC (rev 295011)
@@ -141,7 +141,7 @@
     "use strict";
 
     var _onRejected_ = function (result) { @doAsyncGeneratorBodyCall(generator, result, @GeneratorResumeModeThrow); };
-    @resolveWithoutPromise(value, onFulfilled, onRejected);
+    @resolveWithoutPromiseForAsyncAwait(value, onFulfilled, onRejected);
 }
 
 @globalPrivate
@@ -223,7 +223,7 @@
         if (state === @AsyncGeneratorStateCompleted) {
             if (next.resumeMode === @GeneratorResumeModeReturn) {
                 @putAsyncGeneratorInternalField(generator, @generatorFieldState, @AsyncGeneratorStateAwaitingReturn);
-                @resolveWithoutPromise(next.value,
+                @resolveWithoutPromiseForAsyncAwait(next.value,
                     function (result) {
                         @putAsyncGeneratorInternalField(generator, @generatorFieldState, @AsyncGeneratorStateCompleted);
                         @asyncGeneratorResolve(generator, result, true);

Modified: trunk/Source/_javascript_Core/builtins/PromiseOperations.js (295010 => 295011)


--- trunk/Source/_javascript_Core/builtins/PromiseOperations.js	2022-05-29 07:57:37 UTC (rev 295010)
+++ trunk/Source/_javascript_Core/builtins/PromiseOperations.js	2022-05-29 08:14:12 UTC (rev 295011)
@@ -52,6 +52,8 @@
 @globalPrivate
 function newPromiseCapabilitySlow(constructor)
 {
+    "use strict";
+
     var promiseCapability = {
         @resolve: @undefined,
         @reject: @undefined,
@@ -102,6 +104,8 @@
 @globalPrivate
 function promiseResolve(constructor, value)
 {
+    "use strict";
+
     if (@isPromise(value) && value.constructor === constructor)
         return value;
 
@@ -117,6 +121,8 @@
 @globalPrivate
 function promiseResolveSlow(constructor, value)
 {
+    "use strict";
+
     @assert(constructor !== @Promise);
     var promiseCapability = @newPromiseCapabilitySlow(constructor);
     promiseCapability.@resolve.@call(@undefined, value);
@@ -126,6 +132,8 @@
 @globalPrivate
 function promiseRejectSlow(constructor, reason)
 {
+    "use strict";
+
     @assert(constructor !== @Promise);
     var promiseCapability = @newPromiseCapabilitySlow(constructor);
     promiseCapability.@reject.@call(@undefined, reason);
@@ -232,6 +240,8 @@
 @globalPrivate
 function resolvePromiseWithFirstResolvingFunctionCallCheck(promise, value)
 {
+    "use strict";
+
     @assert(@isPromise(promise));
     var flags = @getPromiseInternalField(promise, @promiseFieldFlags);
     if (flags & @promiseFlagsIsFirstResolvingFunctionCalled)
@@ -243,6 +253,8 @@
 @globalPrivate
 function fulfillPromiseWithFirstResolvingFunctionCallCheck(promise, value)
 {
+    "use strict";
+
     @assert(@isPromise(promise));
     var flags = @getPromiseInternalField(promise, @promiseFieldFlags);
     if (flags & @promiseFlagsIsFirstResolvingFunctionCalled)
@@ -254,6 +266,8 @@
 @globalPrivate
 function rejectPromiseWithFirstResolvingFunctionCallCheck(promise, reason)
 {
+    "use strict";
+
     @assert(@isPromise(promise));
     var flags = @getPromiseInternalField(promise, @promiseFieldFlags);
     if (flags & @promiseFlagsIsFirstResolvingFunctionCalled)
@@ -352,7 +366,24 @@
     @enqueueJob(@promiseReactionJobWithoutPromise, onFulfilled, value);
 }
 
+// This function has strong guarantee that each handler function (onFulfilled and onRejected) will be called at most once.
+// This is special version of resolveWithoutPromise which skips resolution's then handling.
+// https://github.com/tc39/ecma262/pull/1250
 @globalPrivate
+function resolveWithoutPromiseForAsyncAwait(resolution, onFulfilled, onRejected)
+{
+    "use strict";
+
+    if (@isPromise(resolution)) {
+        var constructor = resolution.constructor;
+        if (constructor === @Promise || constructor === @InternalPromise)
+            return @performPromiseThen(resolution, onFulfilled, onRejected);
+    }
+
+    return @resolveWithoutPromise(resolution, onFulfilled, onRejected);
+}
+
+@globalPrivate
 function createResolvingFunctionsWithoutPromise(onFulfilled, onRejected)
 {
     "use strict";
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to