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>
`await <call>` now pushes a NULL slot below the callable, sets the awaited flag of the call and follows it with END_AWAITED_CALL, which jumps to the YIELD_VALUE of the SEND loop that follows. The slot is meant to tell how a coroutine body that the call ran without creating the coroutine object ended. END_AWAITED_CALL pops a NULL slot and goes on to GET_AWAITABLE; for a tagged int (the body returned) it skips the SEND loop, finding the END_SEND through the SEND right before YIELD_VALUE. Nothing writes to the slot yet, so there is no behavior change. The jump to YIELD_VALUE has a stack effect of +1 (coroutine, NULL and value in place of slot and value), and lands inside the SETUP_FINALLY of the SEND loop, which the fallthrough from SEND sets up. 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. The body reports how it ended through the slot below the awaited call, and END_AWAITED_CALL acts on it: - the body returned: clear_thread_frame() stores a tagged int, and END_AWAITED_CALL skips the SEND loop; - the body suspended: YIELD_VALUE leaves the frame and its exception state on the data stack and stores the frame. END_AWAITED_CALL creates the coroutine, moves both into it and jumps to the YIELD_VALUE of the SEND loop with (coroutine, NULL, value), as SEND would have left them; - the body raised: the exception is raised from END_AWAITED_CALL, which has the location of the await. END_AWAITED_CALL is never instrumented, so nothing runs between the body's suspension and the creation of its coroutine. It reports the STOP_ITERATION event of the END_SEND it skips. 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
8 files changed ·
|
clear_thread_frame() runs on every return from a Python function. With the lazy coroutine handling inline it was no longer inlined into _PyEval_FrameClearAndPop() and kept a value across the frame pop, adding about 9 instructions per return. Move that handling into a separate non-inlined function. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Store the number of arguments of CALL and CALL_KW in the oparg as before and mark awaited calls with bit 0x80 instead of shifting the count. The compiler never emits these calls with that many arguments. Only the generic forms mask the bit out; awaited call sites are not specialized, so the specialized forms use the oparg as is and no longer check for an awaited call. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CALL_PY_EXACT_ARGS_AWAITED, CALL_BOUND_METHOD_EXACT_ARGS_AWAITED, CALL_PY_GENERAL_AWAITED, CALL_BOUND_METHOD_GENERAL_AWAITED, CALL_KW_PY_AWAITED and CALL_KW_BOUND_METHOD_AWAITED are the plain forms with CALL_AWAITED cleared from oparg first (_CLEAR_CALL_AWAITED), so they share all other uops, and with the coroutine body started lazily (_START_LAZY_CORO). Tier two clears CALL_AWAITED when translating and starts the coroutine eagerly. The recording uops of the CALL and CALL_KW families mask CALL_AWAITED too: they also run for awaited call sites. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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 suspends.This is an alternative to #162238 / #162242. It keeps the awaited flag in the call oparg and the slot below the callable, but puts all the handling into one new instruction after the call.
GET_AWAITABLE,SENDandEND_SENDstay as onmain.Bytecode
END_AWAITED_CALLhandles the slot as follows:GET_AWAITABLE.SENDloop is skipped. The target is past theEND_SENDthat theSENDbeforeYIELD_VALUEjumps to.YIELD_VALUEleft the frame and its exception state on the data stack.END_AWAITED_CALLcreates the coroutine, moves both into it, and jumps toYIELD_VALUEwith(coroutine, NULL, value), asSENDwould have left them.END_AWAITED_CALL, which has the await's location, so tracebacks are unchanged.The flowgraph gives the jump edge a stack effect of +1. The except-stack pass does not follow it, because the
SENDfallthrough sets up theSETUP_FINALLYaroundYIELD_VALUE.END_AWAITED_CALLis never instrumented (likeRESUME/END_FOR), so nothing can run between the body's suspension and the coroutine's creation. When theEND_SENDit skips is instrumented, it reports theSTOP_ITERATIONevent.Calls from C, instrumented calls, coroutine origin tracking, JIT trace recording and tier 2 keep creating the coroutine eagerly.
frame.f_generator/PyFrame_GetGenerator()areNone/NULLfor such a coroutine until it first suspends.Commits
CALL,CALL_KWandCALL_FUNCTION_EX(no behavior change).END_AWAITED_CALLafter awaited calls (no behavior change).Benchmark (release build, x86-64, best of 7)
await noop()🤖 Generated with Claude Code