Skip to content

GH-132554: Specialize GET_ITER and FOR_ITER for range #135063

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
4e34e27
Specialize GET_ITER
markshannon Apr 30, 2025
37757c6
Add news
markshannon Apr 30, 2025
658a422
Specialize FOR_ITER for ranges using tagged ints
markshannon Apr 30, 2025
8cf4e6e
Allow range starts other than zero
markshannon May 27, 2025
11117b4
Specialize GET_ITER for range
markshannon May 28, 2025
d6f033f
Fix assert
markshannon May 28, 2025
4679ccc
Fix GET_ITER stats
markshannon May 29, 2025
06c1680
Fix stats for FOR_ITER specialization
markshannon Jun 3, 2025
15cb250
Remove unused function
markshannon Jun 3, 2025
89d5d85
Correct comment
markshannon Jun 3, 2025
9f688e7
Fix merge glitch
markshannon Jun 3, 2025
7325b44
Fix tier 2 unspecialized iteration over ranges
markshannon Jun 3, 2025
e5f666d
Make functions inline for the JIT
markshannon Jun 3, 2025
8efce0a
Make PyStackRef_BoxInt inline for JIT
markshannon Jun 3, 2025
94ad6f9
Merge branch 'main' into specialize-for-iter-range
markshannon Jun 6, 2025
7ba5753
Fix PyStackRef_TaggedIntLessThan for Py_STACKREF_DEBUG
markshannon Jun 6, 2025
7553e10
Merge branch 'main' into specialize-for-iter-range
markshannon Jun 6, 2025
baf4722
Merge branch 'main' into specialize-for-iter-range
markshannon Jun 9, 2025
122a816
Cleanup _ITER_JUMP_TUPLE
markshannon Jun 10, 2025
9b65402
Merge branch 'main' into specialize-for-iter-range
markshannon Jun 13, 2025
4e44e85
Extend iteration by index to strings and bytes
markshannon Jun 16, 2025
c41d531
Streamline non-specialized FOR_ITER
markshannon Jun 16, 2025
47520ee
Convert INSTRUMENTED_FOR_ITER into macro.
markshannon Jun 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Include/cpython/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ struct _typeobject {
* Otherwise, limited to MAX_VERSIONS_PER_CLASS (defined elsewhere).
*/
uint16_t tp_versions_used;
/* Returns the object at the index given, or NULL if out-of-bounds
* Never raises an exception. */
iterindexfunc tp_iterindex;
};

#define _Py_ATTR_CACHE_UNUSED (30000) // (see tp_versions_used)
Expand Down
1 change: 1 addition & 0 deletions Include/internal/pycore_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ extern void _Py_Specialize_CompareOp(_PyStackRef lhs, _PyStackRef rhs,
_Py_CODEUNIT *instr, int oparg);
extern void _Py_Specialize_UnpackSequence(_PyStackRef seq, _Py_CODEUNIT *instr,
int oparg);
extern void _Py_Specialize_GetIter(_PyStackRef iter, _Py_CODEUNIT *instr);
extern void _Py_Specialize_ForIter(_PyStackRef iter, _PyStackRef null_or_index, _Py_CODEUNIT *instr, int oparg);
extern void _Py_Specialize_Send(_PyStackRef receiver, _Py_CODEUNIT *instr);
extern void _Py_Specialize_ToBool(_PyStackRef value, _Py_CODEUNIT *instr);
Expand Down
3 changes: 2 additions & 1 deletion Include/internal/pycore_magic_number.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ Known values:
Python 3.15a1 3651 (Simplify LOAD_CONST)
Python 3.15a1 3652 (Virtual iterators)
Python 3.15a1 3653 (Fix handling of opcodes that may leave operands on the stack when optimizing LOAD_FAST)
Python 3.15a1 3654 (Specialize GET_ITER. Add FOR_ITER_INDEX)


Python 3.16 will start with 3700
Expand All @@ -294,7 +295,7 @@ PC/launcher.c must also be updated.

*/

#define PYC_MAGIC_NUMBER 3653
#define PYC_MAGIC_NUMBER 3654
/* This is equivalent to converting PYC_MAGIC_NUMBER to 2 bytes
(little-endian) and then appending b'\r\n'. */
#define PYC_MAGIC_NUMBER_TOKEN \
Expand Down
45 changes: 35 additions & 10 deletions Include/internal/pycore_opcode_metadata.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions Include/internal/pycore_range.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,52 @@ extern "C" {
# error "this header requires Py_BUILD_CORE define"
#endif

typedef struct {
PyObject_HEAD
PyObject *start;
PyObject *stop;
PyObject *step;
PyObject *length;
} rangeobject;

typedef struct {
PyObject_HEAD
long start;
long step;
long len;
} _PyRangeIterObject;

// Does this range have step == 1 and both start and stop in compact int range?
static inline int
_PyRange_IsSimpleCompact(PyObject *range) {
assert(PyRange_Check(range));
rangeobject *r = (rangeobject*)range;
if (_PyLong_IsCompact((PyLongObject *)r->start) &&
_PyLong_IsCompact((PyLongObject *)r->stop) &&
r->step == _PyLong_GetOne()
) {
return 1;
}
return 0;
}

static inline Py_ssize_t
_PyRange_GetStartIfCompact(PyObject *range)
{
assert(PyRange_Check(range));
rangeobject *r = (rangeobject*)range;
assert(_PyLong_IsCompact((PyLongObject *)r->start));
return _PyLong_CompactValue((PyLongObject *)r->start);
}

static inline Py_ssize_t
_PyRange_GetStopIfCompact(PyObject *range) {
assert(PyRange_Check(range));
rangeobject *r = (rangeobject*)range;
assert(_PyLong_IsCompact((PyLongObject *)r->stop));
return _PyLong_CompactValue((PyLongObject *)r->stop);
}

#ifdef __cplusplus
}
#endif
Expand Down
32 changes: 31 additions & 1 deletion Include/internal/pycore_stackref.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ PyStackRef_IsTaggedInt(_PyStackRef ref)
return (ref.index & 1) == 1;
}

static inline bool
PyStackRef_TaggedIntLessThan(_PyStackRef a, _PyStackRef b)
{
assert(PyStackRef_IsTaggedInt(a));
assert(PyStackRef_IsTaggedInt(b));
return ((intptr_t)a.bits) < ((intptr_t)b.bits);
}

static inline PyObject *
_PyStackRef_AsPyObjectBorrow(_PyStackRef ref, const char *filename, int linenumber)
{
Expand Down Expand Up @@ -329,10 +337,18 @@ static inline _PyStackRef
PyStackRef_IncrementTaggedIntNoOverflow(_PyStackRef ref)
{
assert((ref.bits & Py_TAG_BITS) == Py_INT_TAG); // Is tagged int
assert((ref.bits & (~Py_TAG_BITS)) != (INT_MAX & (~Py_TAG_BITS))); // Isn't about to overflow
assert((ref.bits & (~Py_TAG_BITS)) != (INTPTR_MAX & (~Py_TAG_BITS))); // Isn't about to overflow
return (_PyStackRef){ .bits = ref.bits + 4 };
}

static inline bool
PyStackRef_TaggedIntLessThan(_PyStackRef a, _PyStackRef b)
{
assert(PyStackRef_IsTaggedInt(a));
assert(PyStackRef_IsTaggedInt(b));
return ((intptr_t)a.bits) < ((intptr_t)b.bits);
}

#define PyStackRef_IsDeferredOrTaggedInt(ref) (((ref).bits & Py_TAG_REFCNT) != 0)

#ifdef Py_GIL_DISABLED
Expand Down Expand Up @@ -849,6 +865,20 @@ _Py_TryXGetStackRef(PyObject **src, _PyStackRef *out)
} \
} while (0)


static inline _PyStackRef
PyStackRef_BoxInt(_PyStackRef i)
{
assert(PyStackRef_IsTaggedInt(i));
intptr_t val = PyStackRef_UntagInt(i);
PyObject *boxed = PyLong_FromSsize_t(val);
if (boxed == NULL) {
return PyStackRef_ERROR;
}
return PyStackRef_FromPyObjectSteal(boxed);
}


#ifdef __cplusplus
}
#endif
Expand Down
Loading
Loading