gh-82088: Improve performance of PyLong_As*() for multi-digit ints#135585
Merged
vstinner merged 16 commits intopython:mainfrom Jul 10, 2025
Merged
gh-82088: Improve performance of PyLong_As*() for multi-digit ints#135585vstinner merged 16 commits intopython:mainfrom
vstinner merged 16 commits intopython:mainfrom
Conversation
eendebakpt
commented
Jun 16, 2025
| assert(ULONG_MAX >= ((1UL << PyLong_SHIFT) - 1)); | ||
| x = digits[i]; | ||
|
|
||
| #if ((ULONG_MAX >> PyLong_SHIFT)) >= ((1UL << PyLong_SHIFT) - 1) |
Contributor
Author
There was a problem hiding this comment.
We can also unroll the second digit for the other methods. It gains a bit of performance at the cost of a bit of extra code. The unrolling can be factored out to a helper function (the loop itself is not so easy because there are small differences in how to handle the overflow)
vstinner
reviewed
Jun 17, 2025
Co-authored-by: Victor Stinner <vstinner@python.org>
vstinner
reviewed
Jun 24, 2025
Member
|
I rewrote the benchmark using pyperf: import pyperf
from struct import Struct
N = 1000
runner = pyperf.Runner()
values = tuple(range(2**30, 2**30 + N))
for key in 'nNlL':
pack = Struct(key*N).pack
runner.bench_func(f'pack {key}', pack, *values)Result:
|
vstinner
reviewed
Jun 24, 2025
eendebakpt
commented
Jun 24, 2025
Co-authored-by: Victor Stinner <vstinner@python.org>
vstinner
approved these changes
Jun 24, 2025
Member
There was a problem hiding this comment.
LGTM. I have two last minor suggestions.
@serhiy-storchaka: Would you mind to double check that the integer overflow checks are correct?
Replace:
prev = x;
x = (x << PyLong_SHIFT) | v->long_value.ob_digit[i];
if ((x >> PyLong_SHIFT) != prev) ...with:
if (x > (ULONG_MAX >> PyLong_SHIFT)) ...
x = (x << PyLong_SHIFT) | v->long_value.ob_digit[i];Co-authored-by: Victor Stinner <vstinner@python.org>
skirpichev
reviewed
Jun 25, 2025
Member
|
Thanks @eendebakpt, I merged your PR. Let's see how it goes :-) |
AndPuQing
pushed a commit
to AndPuQing/cpython
that referenced
this pull request
Jul 11, 2025
…nts (python#135585) Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> Co-authored-by: Victor Stinner <vstinner@python.org>
Pranjal095
pushed a commit
to Pranjal095/cpython
that referenced
this pull request
Jul 12, 2025
…nts (python#135585) Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> Co-authored-by: Victor Stinner <vstinner@python.org>
picnixz
pushed a commit
to picnixz/cpython
that referenced
this pull request
Jul 13, 2025
…nts (python#135585) Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> Co-authored-by: Victor Stinner <vstinner@python.org>
taegyunkim
pushed a commit
to taegyunkim/cpython
that referenced
this pull request
Aug 4, 2025
…nts (python#135585) Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> Co-authored-by: Victor Stinner <vstinner@python.org>
Agent-Hellboy
pushed a commit
to Agent-Hellboy/cpython
that referenced
this pull request
Aug 19, 2025
…nts (python#135585) Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> Co-authored-by: Victor Stinner <vstinner@python.org>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is a continuation of the work in #15363 by @sir-sigurd.
Performance is improved by
Performance: main
PR
Script:
(performance gain is similar to earlier reported improvements)