gh-155628: Add _Py_atomic_add_*_relaxed variants - #155630
Open
kddnewton wants to merge 5 commits into
Open
Conversation
Add _Py_atomic_add_*_relaxed() for all arithmetic types supported by
_Py_atomic_add_*(). The existing add operations are sequentially
consistent, which is stronger (and on ARM, measurably more
expensive) than necessary for uses like statistics counters and unique
ID allocation, where the add must be atomic but does not need to order
surrounding memory accesses.
The GCC/Clang backend uses __atomic_fetch_add() with __ATOMIC_RELAXED,
and the standard C11/C++11 backend uses atomic_fetch_add_explicit()
with memory_order_relaxed. The MSVC backend uses the
_InterlockedExchangeAdd*_nf ("no fence") intrinsics on ARM64; on x86
and x64 those intrinsics do not exist, so it falls back to the plain
interlocked intrinsics, whose stronger ordering is a conforming
implementation of relaxed (x86 has no cheaper atomic
read-modify-write). As with the sequentially consistent version, 64-bit
adds on 32-bit x86 fall back to a compare-exchange loop.
The _testcapi smoke tests for atomic adds now exercise the relaxed
variants as well.
The add is the only access to LAST_MODULE_INDEX anywhere in the codebase; only the uniqueness of each returned index matters, which atomicity alone guarantees.
The free-threaded build buffers per-thread allocation counts and flushes them to gcstate->young.count in three places: when the local threshold is reached, when a thread state is cleared, and in gc.get_count(). The counter is a collection heuristic: its readers use relaxed loads (gc_should_collect()) or a compare-exchange loop, it is reset during a stop-the-world pause, and it publishes no other memory, so the flushes need atomicity but no ordering.
Add an FT_ATOMIC_ADD_SSIZE_RELAXED wrapper and use it for the lru_cache hits and misses counters, which are updated on every cached call in the free-threaded build. The counters are pure statistics: their only readers are cache_info() and cache_clear(), which already use relaxed loads, so the adds need atomicity but no ordering.
The counter only generates unique default Task names; the add is its sole access in the free-threaded build, so only the uniqueness of each returned value matters, which atomicity alone guarantees.
kddnewton
requested review from
1st1,
FFY00,
ZeroIntensity,
asvetlov,
brettcannon,
ericsnowcurrently,
kumaraditya303,
markshannon,
methane,
ncoghlan,
pablogsal,
rhettinger,
warsaw and
willingc
as code owners
August 12, 2026 15:16
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
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.
Add relaxed variants of the various atomic add functions, then use those functions in a couple of places around the codebase.
Purposefully avoids a couple of future optimizations because I think they will require a bit more investigation, but should be possible:
I can follow up with those separately. Fixes #155628.