GH-145668: Add FOR_ITER specialization for virtual iterators. Specialize GET_ITER.#147967
GH-145668: Add FOR_ITER specialization for virtual iterators. Specialize GET_ITER.#147967markshannon wants to merge 18 commits intopython:mainfrom
Conversation
* Add FOR_ITER_VIRTUAL to specialize FOR_ITER for virtual iterators * Add GET_ITER_SELF to specialize GET_ITER for iterators (including generators) * Add GET_ITER_VIRTUAL to specialize GET_ITER for iterables as virtual iterators
|
Doesn't this change the ABI guarantees of |
|
|
…eption handling in genexpr
Doc/c-api/typeobj.rst
Outdated
| +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+ | ||
| | :c:member:`~PyTypeObject.tp_vectorcall` | :c:type:`vectorcallfunc` | | | | | | | ||
| +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+ | ||
| | [:c:member:`~PyTypeObject.tp_iteritem`] | opaque function pointer | | | | | | |
There was a problem hiding this comment.
Docs are warning that
c:member reference target not found: PyTypeObject.tp_iteritem [ref.member]
There was a problem hiding this comment.
I'm not sure what I can do about that. It is defined, but Sphinx won't acknowledge it, unless I define iteritemfunc which I don't want to define, as it is internal.
There was a problem hiding this comment.
I think you can remove the :c:memberand just make it a normalcode`` role then?
There was a problem hiding this comment.
Name it _tp_iteritemfunc with an underscore, and don't document it.
| _GUARD_ITER_VIRTUAL + | ||
| _PUSH_TAGGED_ZERO; | ||
|
|
||
| op(_GET_ITER_TRAD, (iterable -- iter, index_or_null)) { |
|
Do you have benchmark numbers for this? I'm wondering of 3 extra opcodes is worth it. I think I might have a solution for the opcode problem. |
In the noise. But this enables more optimizations, so neutral is good.
The By themselves, the two new
So the specializations follow naturally.
That's good, but not directly relevant to this PR I think. |
Include/object.h
Outdated
| PyObject *object; | ||
| Py_ssize_t index; | ||
| } _PyObjectIndexPair; | ||
| typedef _PyObjectIndexPair (*iteritemfunc) (PyObject *, Py_ssize_t index); |
There was a problem hiding this comment.
Please name this _Py_iteritemfunc so I don't need to rename it later. If anyone complains, point at me.
Doc/c-api/typeobj.rst
Outdated
| +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+ | ||
| | :c:member:`~PyTypeObject.tp_vectorcall` | :c:type:`vectorcallfunc` | | | | | | | ||
| +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+ | ||
| | [:c:member:`~PyTypeObject.tp_iteritem`] | opaque function pointer | | | | | | |
There was a problem hiding this comment.
Name it _tp_iteritemfunc with an underscore, and don't document it.
| typedef PyObject *(*richcmpfunc) (PyObject *, PyObject *, int); | ||
| typedef PyObject *(*getiterfunc) (PyObject *); | ||
| typedef PyObject *(*iternextfunc) (PyObject *); | ||
| typedef struct _py_object_index_pair { |
There was a problem hiding this comment.
AFAIK the tag is unused; add it later if that changes.
| typedef struct _py_object_index_pair { | |
| typedef struct { |
Add
FOR_ITERspecialization for virtual iterators. SpecializeGET_ITER.FOR_ITER_VIRTUALto specializeFOR_ITERfor virtual iteratorsGET_ITER_SELFto specializeGET_ITERfor iterators (including generators)GET_ITER_VIRTUALto specializeGET_ITERfor iterables as virtual iteratorstp_iteriteminternal slot toPyTypeObjectto support a wider range of classes as virtual iterators.This PR adds
strs as virtual iterators, with the potential to addbytes,bytearray,frozenset,frozendictand others in the future.FOR_ITERspecializations to cover other indexable sequences #145668📚 Documentation preview 📚: https://cpython-previews--147967.org.readthedocs.build/