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

gh-130599: precompute conversion constants for long() #130714

Merged
merged 9 commits into from
Mar 4, 2025

Conversation

nascheme
Copy link
Member

@nascheme nascheme commented Mar 1, 2025

This avoids a data race in free-threaded builds due to mutating the static conversion arrays at runtime. Instead, compute and initialize the constants at runtime initialization. We only need to do this once (not once per interpreter) and so I added _PyLong_InitRuntime().

This avoids a data race in free-threaded builds due to mutating the
statically arrays at runtime.  Instead, compute and initialize the constants
at runtime initialization.
@kumaraditya303
Copy link
Contributor

You need to add the new static globals to ignored.tsv.

@nascheme
Copy link
Member Author

nascheme commented Mar 1, 2025

Given that Sam expressed a preference to not compute at startup time, I investigated how that looks. It turns out that 15 and 30 are the only allowed values for PYLONG_BITS_IN_DIGIT. So statically defined constant tables are not too large. I wrote a little Python script to produce the C code, just in case we want to change any of the constants.

I updated ignored.tsv as noted by Kumar.

@kumaraditya303
Copy link
Contributor

Given that Sam expressed a preference to not compute at startup time, I investigated how that looks. It turns out that 15 and 30 are the only allowed values for PYLONG_BITS_IN_DIGIT. So statically defined constant tables are not too large.

Since the table is now statically defined, it can be marked as const as it is read only.

Copy link
Contributor

@colesbury colesbury left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I measured the difference for ./python -c "pass" between this and the earlier commit that computed the values at runtime initialization. It's very small, about 0.1%, so I if you prefer that approach, that seems fine to me too.

nascheme added 4 commits March 3, 2025 13:48
Use "const" and fix the swapped types of convmultmax_base and
convwidth_base.
This makes the pre-computed tables match exactly what was previously
computed at runtime, if all base conversions were used.  This makes
them slightly smaller too.
@nascheme nascheme merged commit 813bc56 into python:main Mar 4, 2025
41 checks passed
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.

3 participants