Skip to content

Add CALL and CALL_KW specializations for calling generator and coroutine functions - #163099

Draft
asvetlov wants to merge 4 commits into
python:mainfrom
asvetlov:claude/call-async-specializations-2453b3
Draft

asvetlov wants to merge 4 commits into
python:mainfrom
asvetlov:claude/call-async-specializations-2453b3

Conversation

@asvetlov

@asvetlov asvetlov commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Calling a generator, coroutine or async generator function today goes through CALL_PY_EXACT_ARGS/CALL_PY_GENERAL (or CALL_KW_PY): a frame is pushed, RETURN_GENERATOR allocates the generator object, copies the frame into it, pops the frame and returns to the caller.

This adds specializations that build the generator object directly at the call site:

  • CALL_PY_GEN_EXACT_ARGS
  • CALL_PY_GEN_GENERAL
  • CALL_BOUND_METHOD_GEN_EXACT_ARGS
  • CALL_BOUND_METHOD_GEN_GENERAL
  • CALL_KW_PY_GEN
  • CALL_KW_BOUND_METHOD_GEN

They apply to functions whose code starts with RETURN_GENERATOR, so functions with a MAKE_CELL/COPY_FREE_VARS prefix keep the current path. The function version guard covers later __code__ changes.

The new object's frame is initialized in place and left at the instruction after RETURN_GENERATOR, the same state RETURN_GENERATOR leaves it in, so the send, throw and FOR_ITER_GEN paths are unchanged. _Py_MakeCoro now delegates to _PyGen_NewFromFunction(), which takes the caller frame; coroutine origin tracking is computed from it.

Instructions and cycles per iteration, PGO+LTO build, x86-64, perf stat, user space:

instructions cycles
await noop() 1024 → 990 (−3.3%) 211 → 198 (−6.1%)
chain of 10 nested awaits 11582 → 11086 (−4.3%) 2391 → 2213 (−7.4%)
c = noop(); await c 1057 → 1018 (−3.7%) 219 → 207 (−5.1%)
await o.meth(1) (with a default) 1257 → 1206 (−4.1%) 250 → 232 (−7.2%)
next(gen()) 1249 → 1201 (−3.8%) 243 → 235 (−3.1%)
for _ in gen(): pass 1049 → 1002 (−4.5%) 214 → 204 (−4.7%)
for _ in gen(1, k=2): pass 1307 → 1250 (−4.4%) 259 → 248 (−4.3%)
create an async generator 852 → 809 (−5.1%) 171 → 165 (−3.7%)
await a coroutine that suspends once 2804 → 2755 (−1.8%) 588 → 551 (−6.3%)

The pyperformance async benchmarks were within noise on this machine.

Add CALL_PY_ASYNC_EXACT_ARGS, CALL_PY_ASYNC_GENERAL,
CALL_BOUND_METHOD_ASYNC_EXACT_ARGS and CALL_BOUND_METHOD_ASYNC_GENERAL.
They apply to coroutine functions whose code starts with
RETURN_GENERATOR; POP_TOP, and create the coroutine object directly
instead of pushing a frame and executing RETURN_GENERATOR.  The new
coroutine's frame is left at the POP_TOP, as RETURN_GENERATOR leaves it.
Add CALL_KW_PY_ASYNC and CALL_KW_BOUND_METHOD_ASYNC, which create the
coroutine directly for keyword calls of the same simple coroutine
functions as the CALL_*_ASYNC specializations.
@asvetlov asvetlov changed the title Add CALL specializations for calling Python async functions Add CALL and CALL_KW specializations for calling Python async functions Dec 3, 2026
@StanFromIreland

Copy link
Copy Markdown
Member

pyperformance async benchmarks were within noise on this machine.

IMO, I don't think adding bytecodes is worth it for little gains, that aren't actually going to be beneficial in practice.

Specialize calls of generator and async generator functions as well as
coroutine functions, and rename the opcodes from *_ASYNC to *_GEN:
CALL_PY_GEN_EXACT_ARGS, CALL_PY_GEN_GENERAL,
CALL_BOUND_METHOD_GEN_EXACT_ARGS, CALL_BOUND_METHOD_GEN_GENERAL,
CALL_KW_PY_GEN and CALL_KW_BOUND_METHOD_GEN.

Only require the code to start with RETURN_GENERATOR: the new frame is
left at the following instruction, as RETURN_GENERATOR leaves it, so
that instruction does not need to be checked.

_Py_MakeCoro now delegates to _PyGen_NewFromFunction(), which takes the
caller frame used to compute cr_origin.
@asvetlov asvetlov changed the title Add CALL and CALL_KW specializations for calling Python async functions Add CALL and CALL_KW specializations for calling generator and coroutine functions Dec 3, 2026
@asvetlov

asvetlov commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

@StanFromIreland in general, I agree with you.
The opcodes budget is very tight.
This draft is a part of my investigations for #161170 -- speed up async function calls, especially ones that return fast without the suspension.

On the other hand, if we implement #162920 proposal, new specializations couldn't require new opcodes; we can specialize broader than we do it now.

Anyway, consider it as my work-in-progress experiment, which is definitely not ready for landing yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants