[Python-checkins] Minor performance tweak for deque.index() with a start argument (GH-9440)
Raymond Hettinger
webhook-mailer at python.org
Fri Sep 21 04:46:45 EDT 2018
https://github.com/python/cpython/commit/b46ad5431d2643f61e929c1ffec48766b2fafd75
commit: b46ad5431d2643f61e929c1ffec48766b2fafd75
branch: master
author: Raymond Hettinger <rhettinger at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2018-09-21T01:46:41-07:00
summary:
Minor performance tweak for deque.index() with a start argument (GH-9440)
files:
M Lib/test/test_deque.py
M Modules/_collectionsmodule.c
diff --git a/Lib/test/test_deque.py b/Lib/test/test_deque.py
index 921136069d77..51b66b76aca9 100644
--- a/Lib/test/test_deque.py
+++ b/Lib/test/test_deque.py
@@ -288,6 +288,14 @@ def test_index(self):
else:
self.assertEqual(d.index(element, start, stop), target)
+ # Test large start argument
+ d = deque(range(0, 10000, 10))
+ for step in range(100):
+ i = d.index(8500, 700)
+ self.assertEqual(d[i], 8500)
+ # Repeat test with a different internal offset
+ d.rotate()
+
def test_index_bug_24913(self):
d = deque('A' * 3)
with self.assertRaises(ValueError):
diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c
index 935b4348a8ff..267cf07f1f72 100644
--- a/Modules/_collectionsmodule.c
+++ b/Modules/_collectionsmodule.c
@@ -1050,8 +1050,10 @@ deque_index(dequeobject *deque, PyObject *const *args, Py_ssize_t nargs)
start = stop;
assert(0 <= start && start <= stop && stop <= Py_SIZE(deque));
- /* XXX Replace this loop with faster code from deque_item() */
- for (i=0 ; i<start ; i++) {
+ for (i=0 ; i < start - BLOCKLEN ; i += BLOCKLEN) {
+ b = b->rightlink;
+ }
+ for ( ; i < start ; i++) {
index++;
if (index == BLOCKLEN) {
b = b->rightlink;
More information about the Python-checkins
mailing list