-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
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
Faster index range checks #72583
Comments
The expression for testing that some index is in half-open range from 0 to limit can be written as
or as
The latter form generates simpler machine code. This is idiomatic code, it is used in many C and C++ libraries (including C++ stdlib implementations). It already is used in CPython (in deque implementation). Proposed patch rewrites index range checks in more efficient way. The patch was generated automatically by coccinelle script, and then manually cleaned up. |
Don't change the code in the collections module. While semantically valid, the change obfuscates the code. The meaning of maxlen < 0 is that there is no maximum length. I don't want this test hidden; otherwise, I would have changed it long ago as was done elsewhere in the module where it made sense. Elsewhere, consider making a macro with clear name and comment (as was done with NEEDS_TRIM) in the collections module. Otherwise, you're just leaving behind constipated and tricky code with no indication of why a signed variable is being coerced to unsigned. |
Serhiy, could you please take out _decimal.c? I thought we had been through that before. :) :) :) |
The same applies to memoryview.c and _testbuffer.c -- I doubt that there's any measurable speed benefit and the clarity is reduced (I also don't want macros there). |
In the following program, with gcc-5.3 doit() is significantly faster than doit2() in 64-bit Linux: ================================================================ #include <stdint.h>
int
doit(int64_t index, int64_t nitems)
{
return index < 0 || index >= nitems;
}
int
doit2(int64_t index, int64_t nitems)
{
return (uint64_t)index >= (uint64_t)nitems;
}
int
main(void)
{
int count, i;
for (i = 0; i < 1000000000; i++) {
count += doit(832921, i);
}
return count;
} ================================================================ |
The difference in favor of doit() is even more pronounced with this ===================================== for (i = 0; i < 10000; i++) {
for (j = 0; j < 10000; j++) {
count += doit(i, j);
}
} ====================================== |
I have tested. performace differs in about of two times. 0.22 nanoseconds per comparison. Does it cost a time that we spent to discuss here ? |
Which version is faster in your tests? |
Much more conveniet way is to use unsigned variables in appropriate places. |
oh shi....doit() i.e. return index < 0 || index >= nitems; is faster! |
Without optimisation in compiler (-O0) speed is the same. |
-O2 -- the same speed too! |
$ gcc --version
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.2) 5.4.0 20160609 |
That matches my results as well: -O2 gives about the same speed, with -O3 doit() has a huge advantage. I'm not sure this is an optimization at all. |
In your example functions are inlined. If prohibit inlining, the second function is faster. $ gcc -O3 -o issue28397 issue28397-2.c
$ time ./issue28397 0 real 0m8.097s real 0m5.467s |
$ gcc -O3 -DDOIT=doit ./zzz.c -o zzz && time ./zzz real 0m1.675s $ gcc -O3 -DDOIT=doit2 ./zzz.c -o zzz && time ./zzz real 0m1.657s ==================================================== #include <stdint.h>
static int __attribute__((noinline)) doit(int64_t index, int64_t nitems)
{
return index < 0 || index >= nitems;
}
static int __attribute__((noinline)) doit2(int64_t index, int64_t nitems)
{
return (uint64_t)index >= (uint64_t)nitems;
}
int main(void)
{
int count=0, i;
for (i = 0; i < 1000000000; i++) {
count += DOIT(832921, i);
}
return count;
} |
On 64-bit Linux there's no difference: $ ./usr/bin/gcc -O3 -o issue28397-2 issue28397-2.c
$ time ./issue28397-2 0 real 0m2.486s real 0m2.433s Also, most of the time "index < 0 || index >= nitems" *is* inlined, I guess the general point is that such micro-optimizations are Note that the fast inlined version used SSE instructions. |
Serhiy: "The latter form generates simpler machine code." Since this issue is an optimization, can you please provide results of Python benchmarks? Maybe run the performance benchmark suite? I just released performance 0.3 with new benchmarks and less bugs! ;-) |
Unlikely this optimization have measurable affect on benchmarks. I opened this issue because this optimization already was applied to deque (bpo-23553), and it is easy to write a script for applying it to all code. But since there are doubts about this optimization, I withdraw my patch. |
Serhiy Storchaka: "I opened this issue because this optimization already was applied to deque (bpo-23553)" Ah, change 1e89094998b2 written by Raymond Hettinger last year, Raymond who wrote (msg278397): "Don't change the code in the collections module. While semantically valid, the change obfuscates the code." :-) To stay consistent, I suggest to revert the useless micro-optimization 1e89094998b2. Python uses Py_ssize_t instead of size_t to support "i >= 0" checks", it's a deliberate choice to catch bugs. |
Raymond extracted his optimization into separate function and commented it. |
See also PR 9784 where Raymond shown assempler code generated for two variants. There is the similar difference on 64-bit platform with GCC 7.3: $ gcc -O3 -o issue28397 issue28397-2.c
$ time ./issue28397-2 0 real 0m2,740s real 0m2,449s But with GCC 8.2 there is not a difference. $ time ./issue28397-2 0 real 0m2,498s real 0m2,500s Both versions generate the same code for tested functions, there are only minor differences in the main() function (some independent instructions are reordered). I don't know what is the cause of such difference. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: