[Python-checkins] cpython: Add a spacing saving heuristic to deque's extend methods

raymond.hettinger python-checkins at python.org
Tue Jul 9 09:13:31 CEST 2013


http://hg.python.org/cpython/rev/904399fae081
changeset:   84524:904399fae081
user:        Raymond Hettinger <python at rcn.com>
date:        Tue Jul 09 00:13:21 2013 -0700
summary:
  Add a spacing saving heuristic to deque's extend methods

files:
  Lib/test/test_deque.py       |   4 ++--
  Modules/_collectionsmodule.c |  16 ++++++++++++++++
  2 files changed, 18 insertions(+), 2 deletions(-)


diff --git a/Lib/test/test_deque.py b/Lib/test/test_deque.py
--- a/Lib/test/test_deque.py
+++ b/Lib/test/test_deque.py
@@ -543,8 +543,8 @@
         check = self.check_sizeof
         check(deque(), basesize + blocksize)
         check(deque('a'), basesize + blocksize)
-        check(deque('a' * (BLOCKLEN // 2)), basesize + blocksize)
-        check(deque('a' * (BLOCKLEN // 2 + 1)), basesize + 2 * blocksize)
+        check(deque('a' * (BLOCKLEN - 1)), basesize + blocksize)
+        check(deque('a' * BLOCKLEN), basesize + 2 * blocksize)
         check(deque('a' * (42 * BLOCKLEN)), basesize + 43 * blocksize)
 
 class TestVariousIteratorArgs(unittest.TestCase):
diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c
--- a/Modules/_collectionsmodule.c
+++ b/Modules/_collectionsmodule.c
@@ -332,6 +332,14 @@
         return result;
     }
 
+    /* Space saving heuristic.  Start filling from the left */
+    if (Py_SIZE(deque) == 0) {
+        assert(deque->leftblock == deque->rightblock);
+        assert(deque->leftindex == deque->rightindex+1);
+        deque->leftindex = 1;
+        deque->rightindex = 0;
+    }
+
     it = PyObject_GetIter(iterable);
     if (it == NULL)
         return NULL;
@@ -385,6 +393,14 @@
         return result;
     }
 
+    /* Space saving heuristic.  Start filling from the right */
+    if (Py_SIZE(deque) == 0) {
+        assert(deque->leftblock == deque->rightblock);
+        assert(deque->leftindex == deque->rightindex+1);
+        deque->leftindex = BLOCKLEN - 1;
+        deque->rightindex = BLOCKLEN - 2;
+    }
+
     it = PyObject_GetIter(iterable);
     if (it == NULL)
         return NULL;

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


More information about the Python-checkins mailing list