Repository navigation
Skip the retired-table drain when nothing is retired - #1546
Merged
asvetlov merged 3 commits intoSep 24, 2026
Merged
Conversation
On the free-threaded build every lock-free reader that brings num_active_readers back to zero calls _md_drain_retired(), which without a second thread is every lookup. The first thing it does is a sequentially consistent exchange on md->retired, and that list is empty almost every time. Check the list with a relaxed load first and move the rest of the drain behind a call, so a lookup that has nothing to free pays one load rather than an out-of-line call and a read-modify-write. Nothing is stranded by the narrower window: a table retired after the load reads the same as one retired just after the exchange, and _md_retire() drains again once its push is visible. Instruction counts on CPython 3.14, free-threaded: `key in md` -10.6%, `md[key]` -7.9%, a missing `md.get(key)` -5.0%, and -8.1%, -6.2% and -3.3% for the CIMultiDict equivalents. No other benchmarked operation moves beyond the measurement's noise floor. The GIL build emits byte-identical code, confirmed with tools/codegen_diff.py.
The numbers on this page were collected before the retired-table drain change, and before aio-libs#1533 through aio-libs#1538 landed, so the refresh moves more cells than that change alone touches; the free-threaded read rows are the ones it accounts for.
4 of 6 tasks
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1546 +/- ##
==========================================
Coverage 100.00% 100.00%
==========================================
Files 40 41 +1
Lines 6852 6974 +122
Branches 596 606 +10
==========================================
+ Hits 6852 6974 +122
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
asvetlov
marked this pull request as ready for review
September 24, 2026 06:30
Contributor
|
Review catch. A relaxed load there can miss a push: a writer that retires a table and then reads num_active_readers as nonzero leaves the table for whoever brings that count to zero, and the reader doing so must observe the push. Both the push and that read are seq_cst, so the push precedes the writer's read, which precedes the reader's decrement, which precedes the gate's load in the single total order; a relaxed load carries no such guarantee and could strand the table until md's next operation. A sequentially consistent load closes that and costs nothing measurable: on x86-64 it is the same plain mov, and the three read benchmarks are bit-identical to the relaxed version (getitem 292, contains 253, get miss 474). The expensive part this skips was always the exchange, not the load. atomic_load_ptr() is already seq_cst, so the relaxed pointer helper the first commit added is no longer needed and goes away again.
asvetlov
added a commit
to asvetlov/multidict
that referenced
this pull request
Sep 24, 2026
Takes master's copies of hashtable.h, atomic_helpers.h and the 1546 fragment: aio-libs#1546 landed there, so the copies this branch carried are redundant and master's also has aio-libs#1539, aio-libs#1549 and aio-libs#1550 on top.
asvetlov
added a commit
that referenced
this pull request
Sep 24, 2026
<!-- Thank you for your contribution! --> ## What do these changes do? The `istr` section already tells people to build their keys once and reuse them, but not what that buys. This adds the instruction counts, for both builds, next to the plain `str` equivalents, and points out that a key which is not already lower case saves more still, since it has to be copied into a lower-cased string whose hash is not cached. Numbers come from `benchmarks/callgrind_driver.py --include-multidict-only`, the same harness and machine as the tables in `docs/benchmark.rst`. #1546 has landed, so this is down to its own two files. The figures were re-measured against master afterwards, since #1539, #1549 and #1550 also moved them; `d.add()` shifted most, 876 to 765 on the GIL build. ## Are there changes in behavior for the user? No, documentation only. ## Is it a substantial burden for the maintainers to support this? No, though the table joins `docs/benchmark.rst` in needing a refresh when the figures move. ## Related issue number None. ## Checklist - [x] I think the code is well written — N/A, documentation only - [ ] Unit tests for the changes exist — N/A - [x] Documentation reflects the changes - [ ] If you provide code modification, please add yourself to `CONTRIBUTORS.txt` — N/A - [x] Add a new news fragment into the `CHANGES/` folder - [x] `make doc-spelling` passes and any new technical words are added to `docs/spelling_wordlist.txt` Drafted with Claude Opus 5 in Claude Code; reviewed by @asvetlov. --------- Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
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.
What do these changes do?
On the free-threaded build every lock-free reader that brings
num_active_readersback to zero calls_md_drain_retired(), whichwithout a second thread is every single lookup. The first thing that
function does is a sequentially consistent exchange on
md->retired,and that list is empty almost every time.
The drain now checks the list with a load first and the rest of it sits
behind a call, so a lookup with nothing to free pays one load instead of
an out-of-line call and a read-modify-write. The load stays sequentially
consistent: a writer that pushes and then reads
num_active_readersasnonzero leaves the table for whoever brings that count to zero, so the
reader doing so must not be able to miss the push, and only a seq_cst
load is ordered against the push in the single total order. On x86-64
that is the same plain
mov, so it is free; the cost being skipped isthe exchange.
Instructions per operation, CPython 3.14.7 free-threaded:
key in mdmd[key]md.get(key), missCIMultiDictmoves -8.1%, -6.2% and -3.3% on the same three. No otherbenchmarked operation changes beyond the measurement's noise floor.
Wall clock improves more than the instruction counts do, because
Callgrind cannot see the removed
lock-prefixed exchange. Measured backto back against a build of the merge base, two passes each:
key in md35.5/35.3 ns before against 29.0/28.7 after,
md[key]37.1/36.9 against30.9/30.6, so about -18%.
CIMultiDictmoves within the noise of thismachine on wall clock and I would not claim a figure for it.
This is free-threaded only.
atomic_helpers.his entirely inside#ifdef Py_GIL_DISABLED, andtools/codegen_diff.pyreports0 of 190 functions differfor the GIL build. It is also C-extension only: thepure-Python backend has no table retirement to skip.
The second commit regenerates
docs/benchmark.rst. Those numbers werelast collected on the branch of #1532, so the refresh also absorbs
#1533 through #1538; the free-threaded read rows are the part this
change accounts for.
Are there changes in behavior for the user?
No, other than free-threaded reads being faster.
Is it a substantial burden for the maintainers to support this?
No. The retirement protocol is unchanged; one branch is added in front
of it, and the reasoning for why the window is safe is in the comment
above the gate.
Related issue number
None; found while comparing
multidictagainstdicton theoperations both have.
Checklist
CONTRIBUTORS.txt— N/ACHANGES/foldermake doc-spellingpasses and any new technical words are added todocs/spelling_wordlist.txtOn unit tests: no new test is added because the existing
test_drain_retired_defers_busy_table_thread_safety()andtest_setitem_update_thread_safety()already drive this path from bothsides, forcing a retirement on nearly every insert while lock-free
readers run; they exercise the gate taken and not taken.
Drafted with Claude Opus 5 in Claude Code; reviewed by @asvetlov.
Agent run details (optional, for reviewers)
Three variants were measured before this one was picked. Marking the
slow path
COLDrather thanNOINLINEbought the same read win butcost
d.clear()12% andd.copy()4%:coldimplies optimize-for-sizeand
_md_free_retired()'s per-entry decref loop lives down there.Adding the gate without moving the body out of line recovered only 1-2%,
which says the out-of-line call, not the exchange, was most of the cost.
Builds: one venv and one tree copy per leg, rebased on 9f28f61.
Benchmarks are
benchmarks/callgrind_driver.py, bracketed,drift_ir 0,valgrind 3.26.0, Intel Core Ultra 7 155H.