Skip to content

gh-155628: Add _Py_atomic_add_*_relaxed variants - #155630

Open
kddnewton wants to merge 5 commits into
python:mainfrom
kddnewton:relaxed-add
Open

gh-155628: Add _Py_atomic_add_*_relaxed variants#155630
kddnewton wants to merge 5 commits into
python:mainfrom
kddnewton:relaxed-add

Conversation

@kddnewton

@kddnewton kddnewton commented Aug 12, 2026

Copy link
Copy Markdown

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 think free-threaded builds should be able to do relaxed adds on cross-thread increfs instead of seq-cst, because you already hold a reference, and decref needs release (on decrement) and acquire (on zero). That should help ARM quite a bit on free-threading.
  • Thread-handle self->refcount
  • _code_object_generation

I can follow up with those separately. Fixes #155628.

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

bedevere-app Bot commented Aug 12, 2026

Copy link
Copy Markdown

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 skip news label instead.

@python-cla-bot

python-cla-bot Bot commented Aug 12, 2026

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app

bedevere-app Bot commented Aug 12, 2026

Copy link
Copy Markdown

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 skip news label instead.

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.

_Py_atomic_add_*_relaxed

1 participant