Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit db0de9e

Browse files
committed
Speedup for-loops by inlining PyIter_Next(). Saves duplicate tests
and a function call resulting in a 15% reduction of total loop overhead (as measured by timeit.Timer('pass')).
1 parent c1e4f9d commit db0de9e

1 file changed

Lines changed: 10 additions & 8 deletions

File tree

Python/ceval.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2091,21 +2091,23 @@ eval_frame(PyFrameObject *f)
20912091
case FOR_ITER:
20922092
/* before: [iter]; after: [iter, iter()] *or* [] */
20932093
v = TOP();
2094-
x = PyIter_Next(v);
2094+
x = (*v->ob_type->tp_iternext)(v);
20952095
if (x != NULL) {
20962096
PUSH(x);
20972097
PREDICT(STORE_FAST);
20982098
PREDICT(UNPACK_SEQUENCE);
20992099
continue;
21002100
}
2101-
if (!PyErr_Occurred()) {
2102-
/* iterator ended normally */
2103-
x = v = POP();
2104-
Py_DECREF(v);
2105-
JUMPBY(oparg);
2106-
continue;
2101+
if (PyErr_Occurred()) {
2102+
if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2103+
break;
2104+
PyErr_Clear();
21072105
}
2108-
break;
2106+
/* iterator ended normally */
2107+
x = v = POP();
2108+
Py_DECREF(v);
2109+
JUMPBY(oparg);
2110+
continue;
21092111

21102112
case SETUP_LOOP:
21112113
case SETUP_EXCEPT:

0 commit comments

Comments
 (0)