Repository navigation
Conversation
… and CALL_FUNCTION_EX The oparg of CALL and CALL_KW is now the number of arguments shifted left by one, and the low bit of all three calls is reserved for marking a call whose result is awaited right away. The compiler doesn't set it yet, so there is no behavior change. _INIT_CALL_PY_EXACT_ARGS is replicated for the opargs 0..9 so that calls with up to 4 arguments, awaited or not, still get a constant argument count in tier 2. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ITABLE GET_AWAITABLE now also pushes the NULL and None that start the following SEND loop, so the compiler no longer emits them. `await <call>` sets the awaited flag of the call and pushes an extra NULL below its callable. GET_AWAITABLE with the low bit of its oparg set pops that slot as well. Nothing writes to it yet, so there is no behavior change; it will report how a coroutine body that ran without a coroutine object ended. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`await f()` on an `async def` no longer allocates a coroutine object unless the body actually suspends. Python-function CALL paths with the awaited flag mark the new frame of a coroutine function (_MAYBE_LAZY_CORO). RETURN_GENERATOR then keeps running the body in that frame, with its own exception state item on the data stack. A body that returns or raises without suspending never creates a coroutine object; YIELD_VALUE creates one when the body first suspends and moves the frame into it. The body reports how it ended through the slot that the compiler pushes below the awaited call: a tagged int when it returned, or the coroutine when it suspended. GET_AWAITABLE turns that into the SEND loop's input: a NULL receiver makes SEND jump to END_SEND, and None in place of the NULL makes it go on to YIELD_VALUE with the value that the body has already yielded. All forms of SEND check for this, since an await site may have been specialized for another awaitable. Exceptions leaving the body are raised from the caller's SEND, as before. Calls from C, instrumented calls, coroutine origin tracking, JIT trace recording and tier 2 keep creating the coroutine eagerly. frame.f_generator and PyFrame_GetGenerator() return None/NULL for such a coroutine until it first suspends; asyncio.graph classifies frames by co_flags instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
asvetlov
requested review from
1st1,
AA-Turner,
Fidget-Spinner,
ZeroIntensity,
ericsnowcurrently,
iritkatriel,
kumaraditya303,
markshannon,
tomasr8 and
willingc
as code owners
November 18, 2026 20:59
Documentation build overview
6 files changed ·
|
This was referenced Nov 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
await f()on anasync defno longer allocates a coroutine object unless the body actually suspends.This is an alternative to gh-161171. Instead of having
RETURN_GENERATORpeek at the caller's bytecode, the compiler marks awaited calls explicitly, and each instruction does a small, local part of the work:nargs << 1 | awaitedfor CALL and CALL_KW). The Python-function paths (CALL_PY_EXACT_ARGS,CALL_PY_GENERAL, the bound-method and KW variants,CALL_EX_PYand the generic inline paths) mark the new frame of a coroutine function as lazy (_MAYBE_LAZY_CORO). Non-Python callables and non-coroutines are untouched.await <call>, the compiler pushes an extraNULLslot below the callable. A lazy body reports how it ended there: a tagged int if it returned, or the coroutine object if it suspended. In the second case,YIELD_VALUEcreates the coroutine and moves the frame into it.NULL/Nonethat start theSENDloop. With the low bit of its oparg set, it also pops the extra slot and turns it into theSENDinput. It never jumps.NULLreceiver as "done,vis the result" and jumps toEND_SEND. It treatsNonein place of theNULLas "already yieldedv", resets the slot and falls through toYIELD_VALUE.An exception that leaves a lazy body is raised from the caller's
SEND, so tracebacks are the same as before.The coroutine is still created eagerly for calls from C, instrumented calls, PEP 523 hooks, coroutine origin tracking, JIT trace recording and in tier 2 (
_MAYBE_LAZY_CORObecomes a_NOPthere). A tier-2YIELD_VALUEexits to tier 1 for a lazy frame.Visible change:
frame.f_generatorandPyFrame_GetGenerator()areNone/NULLfor such a coroutine until it first suspends;asyncio.graphnow classifies frames byco_flags.The PR has three commits for review: the CALL oparg encoding, the GET_AWAITABLE/codegen change (no behavior change on their own), and the lazy execution.
Microbenchmark
Release build without PGO, x86-64, best of 6 runs:
await noop()await f(1, 2, c=3)await obj.meth(1)The suspending case copies the frame into the coroutine at the first yield instead of at creation.
🤖 Generated with Claude Code