Skip to content

Minor performance tweak for deque.index() with a start argument #9440

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Lib/test/test_deque.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is leftindex == 0 in this case? Would be nice to test also cases when leftindex != 0 and when (leftindex + start) % BLOCKLEN < leftindex.

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):
Expand Down
6 changes: 4 additions & 2 deletions Modules/_collectionsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PEP 7 requires spaces around = and no spaces before ;.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use your talents to focus on something relevant. Over-aggressive interpretations of PEP 7 are waste of time.

b = b->rightlink;
}
for ( ; i < start ; i++) {
index++;
if (index == BLOCKLEN) {
b = b->rightlink;
Expand Down