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 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
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)


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
35 changes: 27 additions & 8 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 @@ -303,10 +311,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 @@ -823,6 +839,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