]> granicus.if.org Git - python/commitdiff
Speed-up deque indexing by changing the deque block length to a power of two.
authorRaymond Hettinger <python@rcn.com>
Sat, 6 Jul 2013 04:05:29 +0000 (18:05 -1000)
committerRaymond Hettinger <python@rcn.com>
Sat, 6 Jul 2013 04:05:29 +0000 (18:05 -1000)
The division and modulo calculation in deque_item() can be compiled
to fast bitwise operations when the BLOCKLEN is a power of two.

Timing before:

 ~/cpython $ py -m timeit -r7 -s 'from collections import deque' -s 'd=deque(range(10))' 'd[5]'
10000000 loops, best of 7: 0.0627 usec per loop

Timing after:

~/cpython $ py -m timeit -r7 -s 'from collections import deque' -s 'd=deque(range(10))' 'd[5]'
10000000 loops, best of 7: 0.0581 usec per loop

Lib/test/test_deque.py
Modules/_collectionsmodule.c

index a8487d2d9559266aff17c45fd5b7a52c2624ee92..e782b99203e08e0515705807aabb462da886d74b 100644 (file)
@@ -536,7 +536,7 @@ class TestBasic(unittest.TestCase):
 
     @support.cpython_only
     def test_sizeof(self):
-        BLOCKLEN = 62
+        BLOCKLEN = 64
         basesize = support.calcobjsize('2P4nlP')
         blocksize = struct.calcsize('2P%dP' % BLOCKLEN)
         self.assertEqual(object.__sizeof__(deque()), basesize)
index b9224754db4742bc3a249aef0a8cab5b5d36ec87..781949d48f22d9182d97817553d300c8d23b53ab 100644 (file)
@@ -14,7 +14,7 @@
  * division/modulo computations during indexing.
  */
 
-#define BLOCKLEN 62
+#define BLOCKLEN 64
 #define CENTER ((BLOCKLEN - 1) / 2)
 
 /* A `dequeobject` is composed of a doubly-linked list of `block` nodes.