1
0
mirror of https://github.com/danog/amp.git synced 2024-11-27 04:24:42 +01:00

Check coroutine nesting level before incurring "next tick" overhead

This commit is contained in:
Daniel Lowrey 2015-06-16 12:08:24 -04:00
parent c7d0f3f86e
commit ad96a584d5

View File

@ -550,6 +550,7 @@ function resolve(\Generator $generator, Reactor $reactor = null) {
$cs->generator = $generator; $cs->generator = $generator;
$cs->returnValue = null; $cs->returnValue = null;
$cs->currentPromise = null; $cs->currentPromise = null;
$cs->nestingLevel = 0;
__coroutineAdvance($cs); __coroutineAdvance($cs);
@ -573,8 +574,14 @@ function __coroutineAdvance($cs) {
$cs->returnValue = $yielded; $cs->returnValue = $yielded;
__coroutineSend(null, null, $cs); __coroutineSend(null, null, $cs);
} elseif ($yielded instanceof Promise) { } elseif ($yielded instanceof Promise) {
$cs->currentPromise = $yielded; if ($cs->nestingLevel < 3) {
$cs->reactor->immediately("Amp\__coroutineNextTick", ["cb_data" => $cs]); $cs->nestingLevel++;
$yielded->when("Amp\__coroutineSend", $cs);
$cs->nestingLevel--;
} else {
$cs->currentPromise = $yielded;
$cs->reactor->immediately("Amp\__coroutineNextTick", ["cb_data" => $cs]);
}
} else { } else {
$error = new \DomainException(makeGeneratorError($cs->generator, sprintf( $error = new \DomainException(makeGeneratorError($cs->generator, sprintf(
'Unexpected yield (Promise|null|"return" expected); %s yielded at key %s', 'Unexpected yield (Promise|null|"return" expected); %s yielded at key %s',