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

Merged
merged 9 commits into from May 25, 2022

Conversation

oda-gitso
Copy link
Contributor

@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.

@oda-gitso
Copy link
Contributor Author

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

@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 for begins with for (; i > 0; i--).

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 = 3 is present. Here is a snippet with i = 3 versus without. The rest of the assembly, not shown, is identical across both versions.

.L2:                        .L2:
    cmpl    $2, -4(%rbp)        cmpl    $2, -4(%rbp)   
    jle .L3                     jle .L3
    movl    $10, %edi           movl    $10, %edi
    call    putchar@PLT         call    putchar@PLT
    movl    $3, -4(%rbp)        jmp .L4
    jmp .L4

I do not think a benchmark is necessary in this instance. It is very clear to me that removing i = wordshift can only make it more efficient. It definitely won't make it any less.

By the way, I do not think I am able to add the skip news and skip issue labels. Are you able to?

@oda-gitso
Copy link
Contributor Author

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

@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!

Copy link
Member

@vstinner vstinner left a comment

LGTM.

@mdickinson
Copy link
Member

@mdickinson mdickinson commented May 23, 2022

The policy stated in the developer's guide is:

Almost all changes made to the code base deserve an entry in Misc/NEWS.d. [...] Changes that affect documentation only generally do not require a NEWS entry.

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?

@bedevere-bot
Copy link

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

Most changes to Python require a NEWS entry.

Please add it using the blurb_it web app or the blurb command-line tool.

@bedevere-bot
Copy link

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

Most changes to Python require a NEWS entry.

Please add it using the blurb_it web app or the blurb command-line tool.

@oda-gitso
Copy link
Contributor Author

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

@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?

But honestly, I'd expect the change to make no observable difference with an optimised build.

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.

What compiler flags did you use when doing your assembly comparison?

gcc -S -o some_name.s some_name.c.

Thanks for your time!

@mdickinson
Copy link
Member

@mdickinson mdickinson commented May 23, 2022

Is there a go-to issue number for news entries that don't have an issue?

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.

@oda-gitso
Copy link
Contributor Author

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

@mdickinson As a workaround, I just used the pull request number as the issue number as well. I think this should be okay.

@oda-gitso
Copy link
Contributor Author

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

On balance, we could probably get away without a news entry here.

@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!

Copy link
Member

@vstinner vstinner left a comment

LGTM. I don't "believe" that this change would provide any significant performance enhancement, but the change is valid. The assignment was redundant.

@mdickinson mdickinson merged commit 854db1a into python:main May 25, 2022
12 checks passed
@mdickinson
Copy link
Member

@mdickinson mdickinson commented May 25, 2022

Merged. Thank you for the contribution, @oda-gitso!

@oda-gitso
Copy link
Contributor Author

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

Thanks a lot @mdickinson, @vstinner!

@oda-gitso oda-gitso deleted the fix-issue-anon branch May 26, 2022
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

5 participants