1
0
mirror of https://github.com/danog/amp.git synced 2024-11-30 04:29:08 +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->returnValue = null;
$cs->currentPromise = null;
$cs->nestingLevel = 0;
__coroutineAdvance($cs);
@ -573,8 +574,14 @@ function __coroutineAdvance($cs) {
$cs->returnValue = $yielded;
__coroutineSend(null, null, $cs);
} elseif ($yielded instanceof Promise) {
if ($cs->nestingLevel < 3) {
$cs->nestingLevel++;
$yielded->when("Amp\__coroutineSend", $cs);
$cs->nestingLevel--;
} else {
$cs->currentPromise = $yielded;
$cs->reactor->immediately("Amp\__coroutineNextTick", ["cb_data" => $cs]);
}
} else {
$error = new \DomainException(makeGeneratorError($cs->generator, sprintf(
'Unexpected yield (Promise|null|"return" expected); %s yielded at key %s',