Skip to content

gh-161170: Lazily materialize coroutine objects for awaited calls - #162476

Draft
asvetlov wants to merge 10 commits into
python:mainfrom
asvetlov:claude/merge-awaited-call-awaitable-065c85
Draft

asvetlov wants to merge 10 commits into
python:mainfrom
asvetlov:claude/merge-awaited-call-awaitable-065c85

Conversation

@asvetlov

@asvetlov asvetlov commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

A coroutine that is awaited as soon as it is called (await f()), and an async def __aenter__() or __aexit__() of async with, no longer creates a coroutine object unless it suspends. Awaits that complete immediately become about twice as fast.

After an awaited call the compiler emits GET_AWAITABLE with the new GET_AWAITABLE_AWAITED_CALL=4 flag in its oparg; the low two bits keep the where value. When the flag is set, the CALL family may run a coroutine body without creating the coroutine object, and GET_AWAITABLE handles the outcome:

  1. go on as usual if coro is returned and the lazy optimization is not applicable
  2. skip the SEND loop if inlined call returns a value
  3. materialize the coroutine and jump to the loop's YIELD_VALUE if inlined call suspends
  4. go to the exception handling block on error

. The SEND loop always starts right after GET_AWAITABLE, so no offset is stored and GET_AWAITABLE stays a non-jump instruction. New *_AWAITED specializations of CALL and CALL_KW cover awaited calls of Python functions.

Until such a coroutine first suspends, frame.f_generator is None.

Calls from C, profiling and monitoring of calls, PEP 523, coroutine origin tracking and the JIT keep the current behavior.

Release build, x86-64 main #162267 this PR
await noop() 53 ns 28 ns (−47%) 28 ns (−47%)
chain of 10 nested awaits 605 ns 328 ns (−46%) 319 ns (−47%)
await that suspends once 141 ns 139 ns (−1%) 142 ns (+1%)
async with asyncio.Lock() 225 ns 150 ns (−33%) 150 ns (−33%)

(Median of three runs; all three builds measured side by side. Percentages are relative to main.)

A plain Python call that is not awaited executes 2–4 more machine instructions (under 1%): returning from a function now checks whether its frame is a lazily run coroutine body.

Supersedes #162267.

🤖 Generated with Claude Code

asvetlov and others added 5 commits November 20, 2026 19:33
`await f()` on an async def, and async def __aenter__() and __aexit__()
of async with, no longer create a coroutine object unless the body
suspends. The compiler pushes a NULL lazy_outcome below the callable and
follows the call with the new END_AWAITED_CALL instruction, which
continues with GET_AWAITABLE, skips the SEND loop if the body returned,
or creates the coroutine and jumps to YIELD_VALUE if it suspended.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Pop the yielded value from the caller's stack, fire PY_THROW, and allow
CLEANUP_THROW in a lazily run body. Make the MemoryError tests target
the coroutine allocation and cover a body that catches the error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
GET_AWAITABLE after an awaited call now has the
GET_AWAITABLE_AWAITED_CALL flag (4) in its oparg, next to 'where' in
the low two bits, and handles the lazy outcome itself.  The SEND loop
always starts right after it, so the target of the suspended case
needs no offset in the oparg and GET_AWAITABLE is not a jump.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@read-the-docs-community

read-the-docs-community Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

asvetlov and others added 5 commits November 23, 2026 10:03
Indexing the slot through lazy_outcome[(oparg >> 2) & 1] made its load
wait for oparg, which slowed down awaits that suspend.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Don't let f_lineno jumps land on the awaited-call GET_AWAITABLE.
* Don't make YIELD_VALUE a line start; emit the await line's LINE event
  from GET_AWAITABLE after materializing, so a raising callback is not
  handled as a throw into the awaited coroutine.
* Never line-instrument the awaited-call GET_AWAITABLE as a jump target.
* Allow the lazy coroutine's exception state to start a new data stack
  chunk.
* Skip LLTRACE_RESUME_FRAME on a lazy suspension.
* Publish the materialized coroutine's frame state after re-pointing its
  frame object.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Keep the await's line for GET_AWAITABLE after an awaited call, so
  tracebacks don't depend on whether LINE monitoring was ever set up;
  the SEND after it starts the line.
* Allow INSTRUCTION events at that GET_AWAITABLE, making the call eager,
  and skip the event if instrumentation was enabled while a lazy body ran.
* Emit the await's LINE event before the lazy body runs, as the eager
  path does at SEND, and forbid jumps from it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* PyFrame_GetGenerator() returns None, not NULL, without a generator.
* The awaited-call flag of GET_AWAITABLE is the value 4, not bit 4.
* Don't warn that lazy_outcome is unused when asserts are disabled.
* Skip test_memory_error_before_body_runs without _testcapi.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A running lazy coroutine body now has owner FRAME_OWNED_BY_LAZY_CORO
instead of a lazy_coro state bit, so returning from an ordinary function
no longer checks for it: _PyEval_FrameClearAndPop dispatches on the owner
it already tests. The new value is 4, keeping the existing values that
outside tools rely on, so range checks against FRAME_OWNED_BY_INTERPRETER
become equality checks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@asvetlov
asvetlov requested a review from pablogsal as a code owner November 24, 2026 18:04
@asvetlov
asvetlov marked this pull request as draft November 25, 2026 03:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant