Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

oda-gitso
Copy link

@oda-gitso oda-gitso commented May 22, 2022

Here is a snippet from the definition of long_lshift1() in longobject.c:

for (i = 0; i < wordshift; i++)
    z->ob_digit[i] = 0;
accum = 0;
for (i = wordshift, j = 0; j < oldsize; i++, j++) {
    accum |= (twodigits)a->ob_digit[j] << remshift;
    z->ob_digit[i] = (digit)(accum & PyLong_MASK);
    accum >>= PyLong_SHIFT;
}

There is no need for i = wordshift in the for loop 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?

@bedevere-bot
Copy link

@bedevere-bot bedevere-bot commented May 22, 2022

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++) {
Copy link
Contributor

@eendebakpt eendebakpt May 22, 2022

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;
}

@eendebakpt
Copy link
Contributor

@eendebakpt eendebakpt commented May 22, 2022

@oda-gitso Did you benchmark whether this is faster? If so, can you show the results.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants