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

Conversation

rhettinger
Copy link
Contributor

Fix an old TODO to optimize the start argument for deque.index(). In the search for the starting block/index pair, make bounding jumps one block at a time rather than a single index at a time.

@@ -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.

Please make the new code conforming PEP 7:

for (i = 0; i < start - BLOCKLEN; i += BLOCKLEN) {
for (; i < start; i++) {

Copy link
Member

Choose a reason for hiding this comment

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

I think that these two loops can be replaced with a single loop:

index = deque->leftindex + start;
for (; index >= BLOCKLEN; index -= BLOCKLEN) {
    b = b->rightlink;
}
n = stop - start;

(not tested)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I find that harder to reason about.

Copy link
Member

Choose a reason for hiding this comment

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

It took me a time to understand the code in this PR, but it looks cryptic again the next day. The code proposed by me is simpler and more efficient. I have tested it, it works. What a problem do you have with it?

@@ -288,6 +288,11 @@ 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.

@@ -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.

@@ -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.

It took me a time to understand the code in this PR, but it looks cryptic again the next day. The code proposed by me is simpler and more efficient. I have tested it, it works. What a problem do you have with it?

@rhettinger rhettinger merged commit b46ad54 into python:master Sep 21, 2018
@serhiy-storchaka serhiy-storchaka removed their assignment Sep 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Performance or resource usage skip issue skip news
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants