gh-155628: Add _Py_atomic_add_*_relaxed variants - #155630
Conversation
|
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 |
dpdani
left a comment
There was a problem hiding this comment.
Mostly looks good, one usage of gcstate->young.count was left out and should be changed.
Though the change is purely internal, I think this needs a news entry.
There was a problem hiding this comment.
This should also have relaxed order.
There was a problem hiding this comment.
Ahh yeah I left this one because I didn't do compare_exchange, just add. I could do that too, but I figured maybe that's another PR?
There was a problem hiding this comment.
Mm, I don't see why it should be a different PR; I'd rather prefer consistency in the accesses of that object. Having some accesses be seq-cst kinda makes it more confusing.
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.
Add _Py_atomic_compare_exchange_*_relaxed() for all arithmetic types supported by _Py_atomic_compare_exchange_*().
e6f2ce7 to
90a0b21
Compare
|
Thanks @dpdani — I've updated to include the compare exchange variants and used it where you mentioned. I think this should be good to go. |
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.