Skip to content

gh-158407: Restore the C fast path for anext() - #158964

Closed
leandrodamascena wants to merge 2 commits into
python:mainfrom
leandrodamascena:gh-158407-anext
Closed

leandrodamascena wants to merge 2 commits into
python:mainfrom
leandrodamascena:gh-158407-anext

Conversation

@leandrodamascena

@leandrodamascena leandrodamascena commented Oct 7, 2026 •

Copy link
Copy Markdown

This restores the C implementation of anext() for the common one-argument case, calling the am_anext slot directly as before gh-157361. The two-argument form keeps using the Python coroutine from _pybuiltins, so the call graph improvement from gh-157361 still applies there.

This follows the approach I proposed in #158407. I'm happy to change direction if there's a better way, especially with aiter() in #157687 in mind.

Local results (release build, ns per item):

case main this PR
anext(async_gen) 211 140
anext(class_iter) 156 138
anext(async_gen, default) 297 239
anext(class_iter, default) 218 233
async for (control) 126 126

The one case that gets a bit slower is anext() with a default on a Python class iterator, since the coroutine is now called from C instead of from Python.

Two tests change as a result: inspect.signature(anext) is back to unsupported (as in 3.15), and the traceback test only checks the coroutine frame, since the TypeError now comes from C.

pythongh-157361 moved anext() to Python, which added a Python frame, a type()
call and an attribute lookup to every call, and made the call itself go
through the __anext__ slot wrapper for C types such as async generators.

Implement anext() in C again, calling the am_anext slot directly. The
two-argument form keeps using the Python coroutine from _pybuiltins, so
that introspection tools can still see through it into the awaitable.
@python-cla-bot

python-cla-bot Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@picnixz

picnixz commented Oct 7, 2026 •

Copy link
Copy Markdown
Member

Sorry but this still needs Kumar's output first. And I'm not really keen on having it back in Python (having it in Python helped the JIT)

@picnixz picnixz closed this Oct 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants