Skip to content

Commit da85862

Browse files
committed
use tagged ints for indexes. Work in progress
1 parent d908cf7 commit da85862

15 files changed

+420
-375
lines changed

Include/internal/pycore_ceval.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,8 @@ PyAPI_FUNC(_PyStackRef) _PyFloat_FromDouble_ConsumeInputs(_PyStackRef left, _PyS
362362
#endif
363363
#endif
364364

365+
_PyStackRef _PyForIter_NextWithIndex(PyObject *seq, _PyStackRef index);
366+
365367
#ifdef __cplusplus
366368
}
367369
#endif

Include/internal/pycore_code.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ extern void _Py_Specialize_CompareOp(_PyStackRef lhs, _PyStackRef rhs,
311311
_Py_CODEUNIT *instr, int oparg);
312312
extern void _Py_Specialize_UnpackSequence(_PyStackRef seq, _Py_CODEUNIT *instr,
313313
int oparg);
314-
extern void _Py_Specialize_ForIter(_PyStackRef iter, _Py_CODEUNIT *instr, int oparg);
314+
extern void _Py_Specialize_ForIter(_PyStackRef iter, _PyStackRef null_or_index, _Py_CODEUNIT *instr, int oparg);
315315
extern void _Py_Specialize_Send(_PyStackRef receiver, _Py_CODEUNIT *instr);
316316
extern void _Py_Specialize_ToBool(_PyStackRef value, _Py_CODEUNIT *instr);
317317
extern void _Py_Specialize_ContainsOp(_PyStackRef value, _Py_CODEUNIT *instr);

Include/internal/pycore_opcode_metadata.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_stackref.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ extern intptr_t PyStackRef_UntagInt(_PyStackRef ref);
233233

234234
extern _PyStackRef PyStackRef_TagInt(intptr_t i);
235235

236+
extern _PyStackRef PyStackRef_IncrementTaggedInt(_PyStackRef ref);
237+
236238
extern bool
237239
PyStackRef_IsNullOrInt(_PyStackRef ref);
238240

@@ -262,6 +264,14 @@ PyStackRef_UntagInt(_PyStackRef i)
262264
}
263265

264266

267+
static inline _PyStackRef
268+
PyStackRef_IncrementTaggedInt(_PyStackRef ref)
269+
{
270+
assert(ref.bits != (uintptr_t)-1); // Overflow
271+
return (_PyStackRef){ .bits = ref.bits + 4 };
272+
}
273+
274+
265275
#ifdef Py_GIL_DISABLED
266276

267277
#define Py_TAG_DEFERRED (1)
@@ -686,7 +696,13 @@ PyStackRef_XCLOSE(_PyStackRef ref)
686696

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

689-
#define PyStackRef_TYPE(stackref) Py_TYPE(PyStackRef_AsPyObjectBorrow(stackref))
699+
static inline PyTypeObject *
700+
PyStackRef_TYPE(_PyStackRef stackref) {
701+
if (PyStackRef_IsTaggedInt(stackref)) {
702+
return &PyLong_Type;
703+
}
704+
return Py_TYPE(PyStackRef_AsPyObjectBorrow(stackref));
705+
}
690706

691707
// Converts a PyStackRef back to a PyObject *, converting the
692708
// stackref to a new reference.

Include/internal/pycore_uop_metadata.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/bytecodes.c

Lines changed: 123 additions & 132 deletions
Large diffs are not rendered by default.

Python/ceval.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ dump_item(_PyStackRef item)
155155
printf("<nil>");
156156
return;
157157
}
158+
if (PyList_CheckExact(obj)) {
159+
printf("len=%ld ", Py_SIZE(obj));
160+
}
158161
// Don't call __repr__(), it might recurse into the interpreter.
159162
printf("<%s at %p>", Py_TYPE(obj)->tp_name, (void *)obj);
160163
}
@@ -271,6 +274,7 @@ maybe_lltrace_resume_frame(_PyInterpreterFrame *frame, PyObject *globals)
271274
lltrace = *python_lltrace - '0'; // TODO: Parse an int and all that
272275
}
273276
}
277+
// lltrace = 5;
274278
if (lltrace >= 5) {
275279
lltrace_resume_frame(frame);
276280
}
@@ -3380,3 +3384,17 @@ _PyEval_LoadName(PyThreadState *tstate, _PyInterpreterFrame *frame, PyObject *na
33803384
}
33813385
return value;
33823386
}
3387+
3388+
_PyStackRef
3389+
_PyForIter_NextWithIndex(PyObject *seq, _PyStackRef index)
3390+
{
3391+
assert(PyStackRef_IsTaggedInt(index));
3392+
assert(Py_TYPE(seq) == &PyTuple_Type || Py_TYPE(seq) == &PyList_Type);
3393+
size_t size = Py_SIZE(seq);
3394+
intptr_t i = PyStackRef_UntagInt(index);
3395+
if ((size_t)i >= size) {
3396+
return PyStackRef_NULL;
3397+
}
3398+
PyObject *next_o = PySequence_Fast_GET_ITEM(seq, i);
3399+
return PyStackRef_FromPyObjectNew(next_o);
3400+
}

Python/codegen.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4463,9 +4463,9 @@ codegen_async_comprehension_generator(compiler *c, location loc,
44634463
else {
44644464
/* Sub-iter - calculate on the fly */
44654465
VISIT(c, expr, gen->iter);
4466-
ADDOP(c, LOC(gen->iter), GET_AITER);
44674466
}
44684467
}
4468+
ADDOP(c, LOC(gen->iter), GET_AITER);
44694469

44704470
USE_LABEL(c, start);
44714471
/* Runtime will push a block here, so we need to account for that */

Python/executor_cases.c.h

Lines changed: 75 additions & 79 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)