Skip to content

gh-161170: Lazily materialize coroutine objects via END_AWAITED_CALL - #162267

Draft
asvetlov wants to merge 1 commit into
python:mainfrom
asvetlov:claude/lazy-coroutine-materialization-5e5705
Draft

asvetlov wants to merge 1 commit into
python:mainfrom
asvetlov:claude/lazy-coroutine-materialization-5e5705

Conversation

@asvetlov

@asvetlov asvetlov commented Nov 19, 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.

The compiler emits a new END_AWAITED_CALL instruction after an awaited call. When it follows, the CALL family runs a coroutine body without creating the coroutine object, and END_AWAITED_CALL handles the outcome. 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 this PR change
await noop() 50 ns 26 ns −48%
chain of 10 nested awaits 582 ns 305 ns −48%
await that suspends once 134 ns 132 ns −1%
async with asyncio.Lock() 222 ns 145 ns −35%

🤖 Generated with Claude Code

@bedevere-app bedevere-app Bot added the type-feature A feature request or enhancement label Nov 19, 2026
@read-the-docs-community

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

Copy link
Copy Markdown

`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>
@asvetlov
asvetlov force-pushed the claude/lazy-coroutine-materialization-5e5705 branch from 81c138a to e7c3c9b Compare November 20, 2026 18:40
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