Skip to content

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

Merged
rhettinger merged 4 commits into
python:masterfrom
rhettinger:bounding-deque-index
Sep 21, 2018
Merged

Minor performance tweak for deque.index() with a start argument#9440
rhettinger merged 4 commits into
python:masterfrom
rhettinger:bounding-deque-index

Conversation

@rhettinger

Copy link
Copy Markdown
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.

Comment thread Modules/_collectionsmodule.c Outdated

/* 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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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?

Comment thread Lib/test/test_deque.py
self.assertEqual(d.index(element, start, stop), target)

# Test large start argument
d = deque(range(0, 10000, 10))

Copy link
Copy Markdown
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.


/* 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
Copy Markdown
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
Copy Markdown
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.

Comment thread Modules/_collectionsmodule.c Outdated

/* 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
Copy Markdown
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