Skip to content

Micro-optimize list index range checks - #9784

Merged
rhettinger merged 2 commits into
python:masterfrom
rhettinger:list_index_checks
Oct 11, 2018
Merged

Micro-optimize list index range checks#9784
rhettinger merged 2 commits into
python:masterfrom
rhettinger:list_index_checks

Conversation

@rhettinger

Copy link
Copy Markdown
Contributor

Old code

_list_item:
testq %rsi, %rsi js L282
cmpq %rsi, 16(%rdi)
jg L283
...
L283:
movq 24(%rdi), %rax
movq (%rax,%rsi,8), %rax
addq $1, (%rax)
ret

New code

_list_item:
cmpq 16(%rdi), %rsi
jb L282
...
L282:
movq 24(%rdi), %rax
movq (%rax,%rsi,8), %rax
addq $1, (%rax)
ret

@serhiy-storchaka

serhiy-storchaka commented Oct 10, 2018

Copy link
Copy Markdown
Member

Is not this like bpo-28397?

@pablogsal

Copy link
Copy Markdown
Member

It would be interesting to run Linux perf on some representative examples to understand how the function call is affecting cache misses, references and branch predictions. (See #6493 as an example).

Comment threadObjects/listobject.c
optimization manual found at:
https://www.agner.org/optimize/optimizing_cpp.pdf
*/
return (size_t) i < (size_t) limit;

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'm not sure that the behaviour is well defined in C. I fear that it's Undefined Behaviour. @benjaminp@gpshead: What do you think ?

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.

If it's well defined, why should we hack such micro optimization? Why compilers would not implement the optimization themself?

@sir-sigurdsir-sigurdOct 10, 2018

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think because they don't know that Py_SIZE(op) is non-negative.

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 is well defined. It is used for example in the STL implementations.

But there was not found any difference in microbenchmark results on 64-bit platforms in previous discussion in bpo-28397.

@gpsheadgpshead left a comment

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.

Regardless of if this change is measurable, i like the way the code looks afterwards, getting rid of the repeated verbose i < 0 || i >= Py_SIZE(spam) everywhere. so +1 from me.

Comment threadObjects/listobject.c
}

static inline int
valid_index(Py_ssize_t i, Py_ssize_t limit)

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.

Why not just define this as taking two size_t parameters instead of doing the casting below. The casts then happen implicitly at all call sites.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performancePerformance or resource usageskip issueskip news

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants

@rhettinger@serhiy-storchaka@pablogsal@gpshead@vstinner@sir-sigurd@the-knights-who-say-ni@bedevere-bot