Skip to content

GH-143732: SEND specialization#148963

Merged
markshannon merged 11 commits intopython:mainfrom
markshannon:send-specialization
May 5, 2026
Merged

GH-143732: SEND specialization#148963
markshannon merged 11 commits intopython:mainfrom
markshannon:send-specialization

Conversation

@markshannon
Copy link
Copy Markdown
Member

@markshannon markshannon commented Apr 24, 2026

Adds 2 new specialized instructions:

  • SEND_VIRTUAL for sends to virtual iterators e.g lists and tuples
  • SEND_ASYNC_GEN for sends to async generators

Also, tweaks FOR_ITER_VIRTUAL so that SEND_VIRTUAL and FOR_ITER_VIRTUAL use equivalent guards

This is aimed more at JIT coverage, especially ensuring that we can trace through SEND instructions, than interpreter performance.

Performance is broadly neutral, but with improvements of 4-6% on the async_generators benchmark.

* SEND_VIRTUAL: for sends to virtual iterators e.g lists and tuples
* SEND_ASYNC_GEN: for sends to async generators

Tweak FOR_ITER_VIRTUAL so that SEND_VIRTUAL and FOR_ITER_VIRTUAL use equivalent guards
Comment thread Tools/cases_generator/analyzer.py Outdated
"_PyStolenTuple_Free",
"PyObject_GC_UnTrack",
"_PyErr_ExceptionMatches",
"CALL_TP_ITERITEM",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is right?

Copy link
Copy Markdown
Member Author

@markshannon markshannon Apr 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is correct in the normal build, but I'll need to exclude lists in the free-threading build.

Copy link
Copy Markdown
Member Author

@markshannon markshannon Apr 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Special casing the free-threaded build is too messy, so I've removed this from the whitelist instead.

Copy link
Copy Markdown
Member

@Fidget-Spinner Fidget-Spinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just two questions, feel free to merge after addressing them.

Comment thread Objects/genobject.c
0, /* am_anext */
0, /* am_send */
.am_await = PyObject_SelfIter,
.am_send = _PyAsyncGenASend_Send,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise, the send needs to go through a call to the send method. I don't know why this wasn't implemented before.

Comment thread Python/ceval_macros.h Outdated
(PyLongObject *)PyStackRef_AsPyObjectBorrow(right)); \
}

#define CALL_TP_ITERITEM(ITER, INDEX) \
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is to make it non-escaping to the analyzer, but it would be clearer if you said CALL_TP_ITERITEM_NO_ESCAPE or something.

Copy link
Copy Markdown
Member Author

@markshannon markshannon May 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dropped the special meaning, to avoid problems with the free-threading build.
I'll just remove the macro, as it isn't doing anything useful any more.

I dropped it for the SAI. I'll update the name as you suggest for the JIT.

@read-the-docs-community
Copy link
Copy Markdown

read-the-docs-community Bot commented May 5, 2026

@markshannon markshannon merged commit 70bd1c2 into python:main May 5, 2026
84 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants