Repository navigation
gh-158407: Restore the C fast path for anext() - #158964
Closed
leandrodamascena wants to merge 2 commits into
Closed
leandrodamascena wants to merge 2 commits into
leandrodamascena wants to merge 2 commits into
Conversation
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.
leandrodamascena
requested review from
FFY00,
ZeroIntensity and
ericsnowcurrently
as code owners
October 7, 2026 12:06
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) |
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.
This restores the C implementation of
anext()for the common one-argument case, calling theam_anextslot 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):
anext(async_gen)anext(class_iter)anext(async_gen, default)anext(class_iter, default)async for(control)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 theTypeErrornow comes from C.