[Python-checkins] cpython: Move the freeblock() call outside the main loop to speed-up and simplify the

raymond.hettinger python-checkins at python.org
Sat Jul 13 11:34:19 CEST 2013


http://hg.python.org/cpython/rev/6a3a613f1335
changeset:   84618:6a3a613f1335
parent:      84616:f478eb9adfe0
user:        Raymond Hettinger <python at rcn.com>
date:        Sat Jul 13 02:34:08 2013 -0700
summary:
  Move the freeblock() call outside the main loop to speed-up and simplify the block re-use logic.

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


diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c
--- a/Modules/_collectionsmodule.c
+++ b/Modules/_collectionsmodule.c
@@ -454,12 +454,13 @@
 static int
 _deque_rotate(dequeobject *deque, Py_ssize_t n)
 {
+    block *b = NULL;
     block *leftblock = deque->leftblock;
     block *rightblock = deque->rightblock;
     Py_ssize_t leftindex = deque->leftindex;
     Py_ssize_t rightindex = deque->rightindex;
     Py_ssize_t len=Py_SIZE(deque), halflen=len>>1;
-    int rv = 0;
+    int rv = -1;
 
     if (len <= 1)
         return 0;
@@ -476,10 +477,10 @@
     deque->state++;
     while (n > 0) {
         if (leftindex == 0) {
-            block *b = newblock(len);
             if (b == NULL) {
-                rv = -1;
-                goto done;
+                b = newblock(len);
+                if (b == NULL)
+                    goto done;
             }
             b->rightlink = leftblock;
             CHECK_END(leftblock->leftlink);
@@ -487,6 +488,7 @@
             leftblock = b;
             MARK_END(b->leftlink);
             leftindex = BLOCKLEN;
+            b = NULL;
         }
         assert(leftindex > 0);
 
@@ -511,7 +513,7 @@
         if (rightindex == -1) {
             block *prevblock = rightblock->leftlink;
             assert(leftblock != rightblock);
-            freeblock(rightblock);
+            b = rightblock;
             CHECK_NOT_END(prevblock);
             MARK_END(prevblock->rightlink);
             rightblock = prevblock;
@@ -520,10 +522,10 @@
     }
     while (n < 0) {
         if (rightindex == BLOCKLEN - 1) {
-            block *b = newblock(len);
             if (b == NULL) {
-                rv = -1;
-                goto done;
+                b = newblock(len);
+                if (b == NULL)
+                    goto done;
             }
             b->leftlink = rightblock;
             CHECK_END(rightblock->rightlink);
@@ -531,6 +533,7 @@
             rightblock = b;
             MARK_END(b->rightlink);
             rightindex = -1;
+            b = NULL;
         }
         assert (rightindex < BLOCKLEN - 1);
 
@@ -555,14 +558,17 @@
         if (leftindex == BLOCKLEN) {
             block *nextblock = leftblock->rightlink;
             assert(leftblock != rightblock);
-            freeblock(leftblock);
+            b = leftblock;
             CHECK_NOT_END(nextblock);
             MARK_END(nextblock->leftlink);
             leftblock = nextblock;
             leftindex = 0;
         }
     }
+    rv = 0;
 done:
+    if (b != NULL)
+        freeblock(b);
     deque->leftblock = leftblock;
     deque->rightblock = rightblock;
     deque->leftindex = leftindex;

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


More information about the Python-checkins mailing list