Open
Labels
3.15bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usage
Description
Currently, we specialize FOR_ITER for both tuple and list. We also handle tuple and lists specially in GET_ITER to support this specialization.
We should extend this specialization of iteration over sequences, using virtual iterators to bytes, str, bytesarray and others.
To do this, we should:
- Add a
tp_getiteritemslots toPyTypeObjectfor sequences that can be efficiently indexed and cannot raise except for out-of-bounds access. - Add a specialization of
FOR_ITERfor these classes. - Specialize
GET_ITERfor:- iterators
- indexable sequences (those classes with a
tp_getiteritemslot)
- Specialize
SENDfor indexable sequences which should also improve JIT coverage, as unspecialized_SENDis currently a major cause of jit optimization failures.
Combined with #145667 this will allow effective specialization of a wider range of iterables and allow that specialization not only for for loops, but also for yield from loops.
It will add 3 instructions (4 new, 1 removed) which a reasonable
Linked PRs
Metadata
Metadata
Assignees
Labels
3.15bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usage
Activity
MrEx3cut0r commentedon Mar 9, 2026
@markshannon hi! I`d like to work on it
pythongh-145668: Optimize FOR_ITER virtual iterators for bytes, bytea…
Merge branch 'main' into pythongh-145668
markshannon commentedon Mar 17, 2026
Here is a more detailed, and slightly changed design.
The
tp_getiteritemfunction pointer slotIt is probably too limiting to only allow contiguous sequences that cannot raise. It would prevents iteration over strings (can raise if cannot allocate new string) and others that might occasionally raise.
We additionally specialize for lists and tuples, so it makes sense to broaden virtual iteration as it won't negatively impact the performance of lists and tuples.
The signature is
PyObject *tp_getiteritem(PyObject *iterable, intptr_t *index). The VM will initialize the index to0at the start of iteration, thereafter the index is under the control oftp_getiteritem. The returned value is the next item in the iteration. IfNULLis returned, iteration is complete if*index >= 0and an error has occurred if*index < 0. If iteration is complete, no exception should be set.StopIterationshould never be raised.tp_getiteritemcannot handle dicts, or sets, as we won't be able to spot concurrent modification, but it will be able to handle frozendicts and frozensets.Specialization of
GET_ITERGET_ITERshould gain specializations for:Py_TYPE(obj)->tp_iter == PyObject_SelfIterPy_TYPE(obj)->tp_iteritem != NULLSpecialization of
SENDSENDshould gain a specializations for virtual iterators:Py_TYPE(obj)->tp_iteritem != NULLbrijkapadia commentedon Mar 24, 2026
@MrEx3cut0r Are you still working on this or could I try to take a stab at this.
MrEx3cut0r commentedon Mar 24, 2026
@bkap123 you can take it, i have no time for it
GH-145668: Add FOR_ITER specialization for virtual iterators. Special…
pythonGH-145668: Add FOR_ITER specialization for virtual iterators. S…