Skip to content

Commit 7e71ddf

Browse files
committed
macro(FOR_ITER_RANGE) = _ITER_CHECK_RANGE + _ITER_JUMP_RANGE + _ITER_NEXT_RANGE
1 parent d3a5b28 commit 7e71ddf

File tree

3 files changed

+190
-216
lines changed

3 files changed

+190
-216
lines changed

Python/bytecodes.c

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2437,42 +2437,32 @@ dummy_func(
24372437
DEOPT_IF(Py_TYPE(r) != &PyRangeIter_Type, FOR_ITER);
24382438
}
24392439

2440-
// NOTE: next may be NULL (TODO: Can we show this in the type?)
2441-
op(_ITER_NEXT_RANGE, (iter -- iter, next)) {
2440+
op(_ITER_JUMP_RANGE, (iter -- iter)) {
24422441
_PyRangeIterObject *r = (_PyRangeIterObject *)iter;
24432442
assert(Py_TYPE(r) == &PyRangeIter_Type);
24442443
STAT_INC(FOR_ITER, hit);
24452444
if (r->len <= 0) {
2446-
next = NULL;
2447-
}
2448-
else {
2449-
long value = r->start;
2450-
r->start = value + r->step;
2451-
r->len--;
2452-
next = PyLong_FromLong(value);
2453-
ERROR_IF(next == NULL, error);
2454-
}
2455-
}
2456-
2457-
op(_FOR_ITER_END, (iter, next -- iter, next)) {
2458-
if (next == NULL) {
2459-
// You might expect STACK_SHRINK(2) here.
2460-
// But we're taking a dangerous shortcut here:
2461-
// This op is always the second half of a
2462-
// (_ITER_NEXT_RANGE, _FOR_ITER_END) pair,
2463-
// and and the code generator hasn't increased
2464-
// the stack pointer to account for 'next' yet.
24652445
STACK_SHRINK(1);
2466-
Py_DECREF(iter);
24672446
SKIP_OVER(INLINE_CACHE_ENTRIES_FOR_ITER);
2468-
// Jump over END_FOR instruction.
2447+
/* Jump forward oparg, then skip following END_FOR instruction */
24692448
JUMPBY(oparg + 1);
24702449
DISPATCH();
24712450
}
24722451
}
24732452

2453+
op(_ITER_NEXT_RANGE, (iter -- iter, next)) {
2454+
_PyRangeIterObject *r = (_PyRangeIterObject *)iter;
2455+
assert(Py_TYPE(r) == &PyRangeIter_Type);
2456+
assert(r->len > 0);
2457+
long value = r->start;
2458+
r->start = value + r->step;
2459+
r->len--;
2460+
next = PyLong_FromLong(value);
2461+
ERROR_IF(next == NULL, error);
2462+
}
2463+
24742464
macro(FOR_ITER_RANGE) =
2475-
unused/1 + _ITER_CHECK_RANGE + _ITER_NEXT_RANGE + _FOR_ITER_END;
2465+
unused/1 + _ITER_CHECK_RANGE + _ITER_JUMP_RANGE + _ITER_NEXT_RANGE;
24762466

24772467
inst(FOR_ITER_GEN, (unused/1, iter -- iter, unused)) {
24782468
DEOPT_IF(tstate->interp->eval_frame, FOR_ITER);

Python/executor_cases.c.h

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

0 commit comments

Comments
 (0)