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. GET_AWAITABLE also pops an extra slot. For `await <call>` the compiler sets the awaited flag of the call, pushes a NULL below its callable and sets GET_AWAITABLE_AFTER_CALL (4) in the oparg of GET_AWAITABLE; for other awaitables it pushes the NULL right after the awaitable. The low two bits of the oparg keep saying where the awaitable comes from. Nothing writes to the slot 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>
Documentation build overview
6 files changed ·
|
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 a third take on the issue, after gh-161171 and gh-162238. 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.YIELD_VALUEcreates the coroutine and moves the frame into it only when the body first suspends.NULLand value that start theSENDloop. The compiler pushes aNULLright after an ordinary awaitable. Forawait <call>, it pushes theNULLbelow the callable and setsGET_AWAITABLE_AFTER_CALL(0b100) in the oparg; the low two bits keep thewherevalue (0, 1, 2). A lazily run body reports how it ended through that slot: a tagged int if it returned, or the coroutine object if it suspended.GET_AWAITABLEturns this into theSENDinput and 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. This is a single unlikely branch on the hot path.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_NOP); 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.Commits, for review: the CALL oparg encoding; the GET_AWAITABLE/codegen change (neither changes behavior on its own); and the lazy execution.
Performance
Release build without PGO, x86-64 (Golden Cove core, pinned), best of 6 runs:
await noop()await Suspend()(not lazy)The remaining slowdown on suspending awaits is not extra work. A build of this PR with the lazy start and all its runtime checks compiled out executes the same number of instructions per await as main, and is just as slow. Top-down counters show that the difference is front-end bound: about 200 more uops per await come from the legacy decoders instead of the decoded uop cache, mostly in the
SEND_GENandGET_AWAITABLEhandlers. This is a code-layout effect of the interpreter loop, so it still needs to be measured with PGO+LTO builds.🤖 Generated with Claude Code