[Python-checkins] cpython: Hoist constant expression out of an inner loop.

raymond.hettinger python-checkins at python.org
Sat Sep 26 10:31:07 CEST 2015


https://hg.python.org/cpython/rev/96af5df1f665
changeset:   98282:96af5df1f665
parent:      98280:40667f456c6f
user:        Raymond Hettinger <python at rcn.com>
date:        Sat Sep 26 01:30:51 2015 -0700
summary:
  Hoist constant expression out of an inner loop.

files:
  Modules/_collectionsmodule.c |  24 ++++++++++++++++++------
  1 files changed, 18 insertions(+), 6 deletions(-)


diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c
--- a/Modules/_collectionsmodule.c
+++ b/Modules/_collectionsmodule.c
@@ -371,6 +371,7 @@
 deque_extend(dequeobject *deque, PyObject *iterable)
 {
     PyObject *it, *item;
+    PyObject *(*iternext)(PyObject *);
     int trim = (deque->maxlen != -1);
 
     /* Handle case where id(deque) == id(iterable) */
@@ -399,7 +400,8 @@
     if (deque->maxlen == 0)
         return consume_iterator(it);
 
-    while ((item = PyIter_Next(it)) != NULL) {
+    iternext = *Py_TYPE(it)->tp_iternext;
+    while ((item = iternext(it)) != NULL) {
         deque->state++;
         if (deque->rightindex == BLOCKLEN - 1) {
             block *b = newblock(Py_SIZE(deque));
@@ -422,8 +424,12 @@
             deque_trim_left(deque);
     }
     if (PyErr_Occurred()) {
-        Py_DECREF(it);
-        return NULL;
+        if (PyErr_ExceptionMatches(PyExc_StopIteration))
+            PyErr_Clear();
+        else {
+            Py_DECREF(it);
+            return NULL;
+        }
     }
     Py_DECREF(it);
     Py_RETURN_NONE;
@@ -436,6 +442,7 @@
 deque_extendleft(dequeobject *deque, PyObject *iterable)
 {
     PyObject *it, *item;
+    PyObject *(*iternext)(PyObject *);
     int trim = (deque->maxlen != -1);
 
     /* Handle case where id(deque) == id(iterable) */
@@ -464,7 +471,8 @@
     if (deque->maxlen == 0)
         return consume_iterator(it);
 
-    while ((item = PyIter_Next(it)) != NULL) {
+    iternext = *Py_TYPE(it)->tp_iternext;
+    while ((item = iternext(it)) != NULL) {
         deque->state++;
         if (deque->leftindex == 0) {
             block *b = newblock(Py_SIZE(deque));
@@ -487,8 +495,12 @@
             deque_trim_right(deque);
     }
     if (PyErr_Occurred()) {
-        Py_DECREF(it);
-        return NULL;
+        if (PyErr_ExceptionMatches(PyExc_StopIteration))
+            PyErr_Clear();
+        else {
+            Py_DECREF(it);
+            return NULL;
+        }
     }
     Py_DECREF(it);
     Py_RETURN_NONE;

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list