diff Python/ceval.c @ 31646:83b0382e0d36 legacy-trunk

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')).
author Raymond Hettinger <python@rcn.com>
date Fri, 12 Mar 2004 08:41:36 +0000
parents b4e4dbd24fe7
children d10c778af167
line wrap: on
line diff
--- a/Python/ceval.c	Fri Mar 12 08:04:00 2004 +0000
+++ b/Python/ceval.c	Fri Mar 12 08:41:36 2004 +0000
@@ -2091,21 +2091,23 @@
 		case FOR_ITER:
 			/* before: [iter]; after: [iter, iter()] *or* [] */
 			v = TOP();
-			x = PyIter_Next(v);
+			x = (*v->ob_type->tp_iternext)(v);
 			if (x != NULL) {
 				PUSH(x);
 				PREDICT(STORE_FAST);
 				PREDICT(UNPACK_SEQUENCE);
 				continue;
 			}
-			if (!PyErr_Occurred()) {
-				/* iterator ended normally */
- 				x = v = POP();
-				Py_DECREF(v);
-				JUMPBY(oparg);
-				continue;
+			if (PyErr_Occurred()) {
+				if (!PyErr_ExceptionMatches(PyExc_StopIteration))
+					break;
+				PyErr_Clear();
 			}
-			break;
+			/* iterator ended normally */
+ 			x = v = POP();
+			Py_DECREF(v);
+			JUMPBY(oparg);
+			continue;
 
 		case SETUP_LOOP:
 		case SETUP_EXCEPT: