Skip to content

GH-132554: "Virtual" iterators #132555

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 26 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
7476442
FOR_ITER now pushes NULL as well as the iterator. Preparation for vir…
markshannon Apr 10, 2025
38a429f
use tagged ints for indexes. Work in progress
markshannon Apr 11, 2025
2caf56f
Remove debug code
markshannon Apr 15, 2025
588943a
Regenerate files
markshannon Apr 15, 2025
aa62e39
Tidy up list of non-escaping functions
markshannon Apr 16, 2025
88562ec
Fix up list iteration for FT build
markshannon Apr 16, 2025
025049d
Fix tier 2 FT build
markshannon Apr 16, 2025
35e389d
Remove debugging code
markshannon Apr 30, 2025
e8f4ce6
Merge branch 'main' into virtual-iterators
markshannon Apr 30, 2025
ed89950
Rename function
markshannon Apr 30, 2025
ec8d797
Restrict specialization in free-threaded build
markshannon Apr 30, 2025
cad1946
Merge branch 'main' into virtual-iterators
markshannon Apr 30, 2025
428735b
Add news
markshannon Apr 30, 2025
4c83848
handle tagged ints when doing type checks
markshannon Apr 30, 2025
f43ccc3
Fix long check
markshannon Apr 30, 2025
3fd46c3
Attempt to make _PyForIter_NextWithIndex thread safe.
markshannon Apr 30, 2025
433282f
Add review comment
markshannon Apr 30, 2025
e915a05
Simplify list indexing code
markshannon May 8, 2025
69a328d
Merge branch 'main' into virtual-iterators
markshannon May 8, 2025
6d1b93e
GET_ITER may leave the iterable on the stack
markshannon May 8, 2025
37ca285
Fix test_dis
markshannon May 8, 2025
f3b9074
Merge branch 'main' into virtual-iterators
markshannon May 19, 2025
0efab59
Merge branch 'main' into virtual-iterators
markshannon May 20, 2025
a0d814b
Merge branch 'main' into virtual-iterators
markshannon May 22, 2025
5cd55c4
Merge branch 'main' into virtual-iterators
markshannon May 22, 2025
867bd10
Merge branch 'main' into virtual-iterators
markshannon May 22, 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
2 changes: 2 additions & 0 deletions Include/internal/pycore_ceval.h
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,8 @@ PyAPI_FUNC(_PyStackRef) _PyFloat_FromDouble_ConsumeInputs(_PyStackRef left, _PyS
extern int _PyRunRemoteDebugger(PyThreadState *tstate);
#endif

_PyStackRef _PyForIter_NextWithIndex(PyObject *seq, _PyStackRef index);

#ifdef __cplusplus
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion Include/internal/pycore_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +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_ForIter(_PyStackRef iter, _Py_CODEUNIT *instr, int oparg);
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);
extern void _Py_Specialize_ContainsOp(_PyStackRef value, _Py_CODEUNIT *instr);
Expand Down
1 change: 1 addition & 0 deletions Include/internal/pycore_compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ typedef enum {
enum _PyCompile_FBlockType {
COMPILE_FBLOCK_WHILE_LOOP,
COMPILE_FBLOCK_FOR_LOOP,
COMPILE_FBLOCK_ASYNC_FOR_LOOP,
COMPILE_FBLOCK_TRY_EXCEPT,
COMPILE_FBLOCK_FINALLY_TRY,
COMPILE_FBLOCK_FINALLY_END,
Expand Down
4 changes: 3 additions & 1 deletion Include/internal/pycore_magic_number.h
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ Known values:
Python 3.14a7 3621 (Optimize LOAD_FAST opcodes into LOAD_FAST_BORROW)
Python 3.14a7 3622 (Store annotations in different class dict keys)
Python 3.14a7 3623 (Add BUILD_INTERPOLATION & BUILD_TEMPLATE opcodes)
Python 3.14a7 3624 (Virtual iterators)


Python 3.15 will start with 3650

Expand All @@ -288,7 +290,7 @@ PC/launcher.c must also be updated.

*/

#define PYC_MAGIC_NUMBER 3623
#define PYC_MAGIC_NUMBER 3624
/* 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
40 changes: 20 additions & 20 deletions Include/internal/pycore_opcode_metadata.h

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

37 changes: 36 additions & 1 deletion Include/internal/pycore_stackref.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ extern intptr_t PyStackRef_UntagInt(_PyStackRef ref);

extern _PyStackRef PyStackRef_TagInt(intptr_t i);

/* Increments a tagged int, but does not check for overflow */
extern _PyStackRef PyStackRef_IncrementTaggedIntNoOverflow(_PyStackRef ref);

extern bool
PyStackRef_IsNullOrInt(_PyStackRef ref);

Expand Down Expand Up @@ -262,6 +265,14 @@ PyStackRef_UntagInt(_PyStackRef i)
}


static inline _PyStackRef
PyStackRef_IncrementTaggedIntNoOverflow(_PyStackRef ref)
{
assert(ref.bits != (uintptr_t)-1); // Deosn't overflow
return (_PyStackRef){ .bits = ref.bits + 4 };
}


#ifdef Py_GIL_DISABLED

#define Py_TAG_DEFERRED (1)
Expand Down Expand Up @@ -686,7 +697,13 @@ PyStackRef_XCLOSE(_PyStackRef ref)

#endif // !defined(Py_GIL_DISABLED) && defined(Py_STACKREF_DEBUG)

#define PyStackRef_TYPE(stackref) Py_TYPE(PyStackRef_AsPyObjectBorrow(stackref))
static inline PyTypeObject *
PyStackRef_TYPE(_PyStackRef stackref) {
if (PyStackRef_IsTaggedInt(stackref)) {
return &PyLong_Type;
}
return Py_TYPE(PyStackRef_AsPyObjectBorrow(stackref));
}

// Converts a PyStackRef back to a PyObject *, converting the
// stackref to a new reference.
Expand All @@ -697,36 +714,54 @@ PyStackRef_XCLOSE(_PyStackRef ref)
static inline bool
PyStackRef_GenCheck(_PyStackRef stackref)
{
if (PyStackRef_IsTaggedInt(stackref)) {
return false;
}
return PyGen_Check(PyStackRef_AsPyObjectBorrow(stackref));
}

static inline bool
PyStackRef_BoolCheck(_PyStackRef stackref)
{
if (PyStackRef_IsTaggedInt(stackref)) {
return false;
}
return PyBool_Check(PyStackRef_AsPyObjectBorrow(stackref));
}

static inline bool
PyStackRef_LongCheck(_PyStackRef stackref)
{
if (PyStackRef_IsTaggedInt(stackref)) {
return true;
}
return PyLong_Check(PyStackRef_AsPyObjectBorrow(stackref));
}

static inline bool
PyStackRef_ExceptionInstanceCheck(_PyStackRef stackref)
{
if (PyStackRef_IsTaggedInt(stackref)) {
return false;
}
return PyExceptionInstance_Check(PyStackRef_AsPyObjectBorrow(stackref));
}

static inline bool
PyStackRef_CodeCheck(_PyStackRef stackref)
{
if (PyStackRef_IsTaggedInt(stackref)) {
return false;
}
return PyCode_Check(PyStackRef_AsPyObjectBorrow(stackref));
}

static inline bool
PyStackRef_FunctionCheck(_PyStackRef stackref)
{
if (PyStackRef_IsTaggedInt(stackref)) {
return false;
}
return PyFunction_Check(PyStackRef_AsPyObjectBorrow(stackref));
}

Expand Down
1 change: 1 addition & 0 deletions Include/internal/pycore_uop_ids.h

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

6 changes: 5 additions & 1 deletion Include/internal/pycore_uop_metadata.h

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

Loading
Loading