Skip to content

Use acquire loads for module globals - #162398

Draft
nascheme wants to merge 1 commit into
python:mainfrom
nascheme:ft-mo-globals-acquire
Draft

nascheme wants to merge 1 commit into
python:mainfrom
nascheme:ft-mo-globals-acquire

Conversation

@nascheme

@nascheme nascheme commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

LOAD_GLOBAL_MODULE, LOAD_GLOBAL_BUILTINS and LOAD_ATTR_MODULE load me_value with consume ordering, which is relaxed outside TSan. For deferred-refcount, immortal or thread-owned values the try-incref path does no further ordered load, so a reader could observe a new global without observing writes made before it was stored. Use acquire loads to pair with the release store in the dict.

This is the most significant divergence from the memory order documented by GH-162178. The performance impact of this change seems small. For strongly ordered architectures, like AMD64, there is no difference. For weak, like ARM, the me_value load goes from something like ldr x0, [x8, #0x28] to ldapur x0, [x8, #0x28] (an acquire load).

The results below are from a Macbook M3 Pro, using performance cores. The --scale 1000, median of 15 runs, averaged over 4 rounds that alternated between the two builds. Times are in ms; diff is branch vs. main, so positive means the branch is slower.

benchmark 1 thread, main 1 thread, branch diff 6 threads, main 6 threads, branch diff
load_global 19.62 19.62 +0.0% 26.64 24.33 −8.7%
load_builtin 29.66 29.68 +0.1% 37.19 35.38 −4.9%
load_attr_module 31.19 31.89 +2.3% 38.91 39.92 +2.6%
call_global_func 22.30 22.43 +0.6% 28.25 29.30 +3.7%
baseline_loop 13.84 14.07 +1.7% 17.84 18.54 +3.9%

bench_globals.py

Here is a test that shows memory order violations, when run without this patch. This run on a Macbook M3 Pro and fails about every 30 seconds of run time. Its a bit difficult to provoke the issue, the globals need to be on separate cache lines.
mo-litmus-globals.py

LOAD_GLOBAL_MODULE, LOAD_GLOBAL_BUILTINS and LOAD_ATTR_MODULE loaded
me_value with consume ordering, which is relaxed outside TSan.  For
deferred-refcount, immortal or thread-owned values the try-incref path
does no further ordered load, so a reader could observe a new global
without observing writes made before it was stored.  Use acquire loads
to pair with the release store in the dict.
@nascheme

Copy link
Copy Markdown
Member Author

CC @colesbury @mpage

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.

1 participant