Commit 8760e494 authored by Raymond Hettinger's avatar Raymond Hettinger

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')).
parent 8e8e0ada
...@@ -2091,21 +2091,23 @@ eval_frame(PyFrameObject *f) ...@@ -2091,21 +2091,23 @@ eval_frame(PyFrameObject *f)
case FOR_ITER: case FOR_ITER:
/* before: [iter]; after: [iter, iter()] *or* [] */ /* before: [iter]; after: [iter, iter()] *or* [] */
v = TOP(); v = TOP();
x = PyIter_Next(v); x = (*v->ob_type->tp_iternext)(v);
if (x != NULL) { if (x != NULL) {
PUSH(x); PUSH(x);
PREDICT(STORE_FAST); PREDICT(STORE_FAST);
PREDICT(UNPACK_SEQUENCE); PREDICT(UNPACK_SEQUENCE);
continue; continue;
} }
if (!PyErr_Occurred()) { if (PyErr_Occurred()) {
/* iterator ended normally */ if (!PyErr_ExceptionMatches(PyExc_StopIteration))
x = v = POP(); break;
Py_DECREF(v); PyErr_Clear();
JUMPBY(oparg);
continue;
} }
break; /* iterator ended normally */
x = v = POP();
Py_DECREF(v);
JUMPBY(oparg);
continue;
case SETUP_LOOP: case SETUP_LOOP:
case SETUP_EXCEPT: case SETUP_EXCEPT:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment