]> granicus.if.org Git - python/commitdiff
Add a spacing saving heuristic to deque's extend methods
authorRaymond Hettinger <python@rcn.com>
Tue, 9 Jul 2013 07:13:21 +0000 (00:13 -0700)
committerRaymond Hettinger <python@rcn.com>
Tue, 9 Jul 2013 07:13:21 +0000 (00:13 -0700)
Lib/test/test_deque.py
Modules/_collectionsmodule.c

index e782b99203e08e0515705807aabb462da886d74b..ae1de9ab46da42e8ec658436af130a1b6be7a28f 100644 (file)
@@ -543,8 +543,8 @@ class TestBasic(unittest.TestCase):
         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):
index 9924b63dff18b59b7b1620886456b6f36e0c5e4f..67ff55b215a3a04591db712be78d9879bbba3fb3 100644 (file)
@@ -332,6 +332,14 @@ deque_extend(dequeobject *deque, PyObject *iterable)
         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 @@ deque_extendleft(dequeobject *deque, PyObject *iterable)
         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;