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
Remove unnecessary for loop initializer in long_lshift1() #93071
Conversation
|
Most changes to Python require a NEWS entry. Please add it using the blurb_it web app or the blurb command-line tool. |
| @@ -4842,7 +4842,7 @@ long_lshift1(PyLongObject *a, Py_ssize_t wordshift, digit remshift) | |||
| for (i = 0; i < wordshift; i++) | |||
| z->ob_digit[i] = 0; | |||
| accum = 0; | |||
| for (i = wordshift, j = 0; j < oldsize; i++, j++) { | |||
| for (j = 0; j < oldsize; i++, j++) { | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One more optimization could be to eliminate the i variable. E.g. something like
pointer = z->ob_digit[wordshift]
for (j = 0; j < oldsize; j++) {
accum |= (twodigits)a->ob_digit[j] << remshift;
pointer[j] = (digit)(accum & PyLong_MASK);
accum >>= PyLong_SHIFT;
}
|
@oda-gitso Did you benchmark whether this is faster? If so, can you show the results. |
|
@eendebakpt I did not benchmark the differences. What I did was look at the assembly for a simple program such as #include <stdio.h>
int
main(void)
{
int i = 0;
for (i = 0; i < 3; i++)
printf("%d ", i);
printf("\n");
for (i = 3; i > 0; i--)
printf("%d ", i);
printf("\n");
return 0;
}versus one where the second On my system (Ubuntu 20.04 LTS, x86-64), the assembly of both programs is identical except for an additional instruction for the case that I do not think a benchmark is necessary in this instance. It is very clear to me that removing By the way, I do not think I am able to add the skip news and skip issue labels. Are you able to? |
|
@mdickinson @vstinner It says you both have touched this function in the past. I was wondering if you would be able to review this change? Thanks! |
|
The policy stated in the developer's guide is:
On that basis, this change probably should have a news entry (though I agree it doesn't need an issue). But honestly, I'd expect the change to make no observable difference with an optimised build. What compiler flags did you use when doing your assembly comparison? |
|
Most changes to Python require a NEWS entry. Please add it using the blurb_it web app or the blurb command-line tool. |
|
Most changes to Python require a NEWS entry. Please add it using the blurb_it web app or the blurb command-line tool. |
|
@mdickinson @vstinner Thanks to you both for getting back to me. I have just added a news entry. Could you also add the skip issue label?
I agree. There might be some behind-the-scenes optimization that I do not know about. However, my thinking is that (1) it will not make it slower (2) it may (depending on system, compiler, flags, etc.) make it faster.
Thanks for your time! |
Hmm, interesting; looks like it's hard to have news but no issue. (The same was true in the bad old Roundup days, but there it was clearer that everything needed an issue.) On balance, we could probably get away without a news entry here. |
|
@mdickinson As a workaround, I just used the pull request number as the issue number as well. I think this should be okay. |
Misc/NEWS.d/next/Core and Builtins/2022-05-23-17-38-04.gh-issue-93071.NHsmc8.rst
Outdated
Show resolved
Hide resolved
@mdickinson Just letting you know that I have opted to delete the news entry, as suggested by Victor. I hope this is okay with you too! Thanks! |
LGTM. I don't "believe" that this change would provide any significant performance enhancement, but the change is valid. The assignment was redundant.
|
Merged. Thank you for the contribution, @oda-gitso! |
|
Thanks a lot @mdickinson, @vstinner! |
Here is a snippet from the definition of
long_lshift1()inlongobject.c:There is no need for
i = wordshiftin theforloop initialization (it adds to the number of instructions) and I do not think it improves readability.I don't think this requires a news entry or an issue?