Skip to content

Commit 213ae54

Browse files
alexmarkovCommit Bot
authored and
Commit Bot
committed
[vm] Always complete Future synchronously when returning non-Future value from async functions
From Dart language specification (17.15 Function Invocation): If execution of the body returns an object, o is completed with that object. If it completes normally or returns without an object, o is completed with the null object (17.4), and if it throws an exception e and stack trace t, o is completed with the error e and stack trace t. If execution of the body throws before the body suspends the first time, completion of o happens at some future time after the invocation has returned. The caller needs time to set up error handling for the returned future, so the future is not completed with an error before it has been returned. So far, implementation of async functions in Dart VM postponed completion of the Future both with the value and with an error if execution has not reached the first await. This change modifies the behavior to compile future with value immediately even if the first await was not reached. Completion with an error is still postponed. This change in behavior will let us improve performance of async/await in certain cases. The new behavior still conforms to the Dart language specification. TEST=ci Issue: #48378 Change-Id: I0e80a1d0dd0c3cd6acbc640681e7b06bbf81c19c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/243101 Reviewed-by: Lasse Nielsen <[email protected]> Commit-Queue: Alexander Markov <[email protected]>
1 parent de43a7c commit 213ae54

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

sdk/lib/_internal/vm/lib/async_patch.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -392,14 +392,16 @@ class _SuspendState {
392392
'returnValue=$returnValue)');
393393
}
394394
_Future future;
395-
bool isSync = true;
396395
if (suspendState is _SuspendState) {
397396
future = unsafeCast<_Future>(suspendState._functionData);
398397
} else {
399398
future = unsafeCast<_Future>(suspendState);
400-
isSync = false;
401399
}
402-
_completeOnAsyncReturn(future, returnValue, isSync);
400+
if (returnValue is Future) {
401+
future._asyncCompleteUnchecked(returnValue);
402+
} else {
403+
future._completeWithValue(returnValue);
404+
}
403405
return future;
404406
}
405407

@@ -412,14 +414,12 @@ class _SuspendState {
412414
'returnValue=$returnValue)');
413415
}
414416
_Future future;
415-
bool isSync = true;
416417
if (suspendState is _SuspendState) {
417418
future = unsafeCast<_Future>(suspendState._functionData);
418419
} else {
419420
future = unsafeCast<_Future>(suspendState);
420-
isSync = false;
421421
}
422-
_completeWithNoFutureOnAsyncReturn(future, returnValue, isSync);
422+
future._completeWithValue(returnValue);
423423
return future;
424424
}
425425

0 commit comments

Comments
 (0)