Repository navigation
Move the bulk update paths into bulk_update.h - #1533
Merged
asvetlov merged 3 commits intoSep 23, 2026
Merged
Conversation
`hashtable.h` was 2735 lines covering three separate things: the table itself, the single-key operations, and the multi-item `extend()`, `update()` and `merge()` engine behind the constructors and the bulk methods. The last of those was a contiguous block nothing else in the file called into, whose only consumer is `_multidict.c`. It moves verbatim into `multidict/_multilib/bulk_update.h`, along with the `UpdateOp` enum: `_md_update()`, `_md_merge()`, the `_md_post_update_*` family, `md_post_update()`, `md_update_from_ht()`, `md_extend_self()`, `md_update_from_dict()`, `md_update_from_kwnames()`, `md_update_from_seq()` and the sequence-parsing helpers beside it. The new header includes `hashtable.h`, so `views.h`, `iter.h` and `capsule.h` keep reaching only the hash table. `_list_getitem_ref()` and `_list_item_gone()` stay behind: `views.h` uses them too, and it includes `hashtable.h` but not the new header. They have no user left in `hashtable.h` itself, which is worth a later look, but giving `views.h` the whole bulk block for two list accessors would undo the split. No behaviour change on either backend; the pure Python implementation has no header structure to mirror. `tools/codegen_diff.py` from aio-libs#1529 reports 0 of 187 functions different on CPython 3.13.2 and 0 of 189 on 3.14.7t, so the compiler emits the same code as before.
asvetlov
marked this pull request as ready for review
September 23, 2026 11:29
Contributor
|
aio-libs#1530 folded the deferred decref accumulator into `reflist_t` and reworked the same region of `hashtable.h` that this branch moves out, so both conflicting hunks resolve the same way: the block leaves `hashtable.h`, and `bulk_update.h` is rebuilt from master's text rather than the branch's older copy. `deferred_decref.h` is gone from the new header's includes, replaced by `reflist.h`. `tools/codegen_diff.py` arrived on master with aio-libs#1529 and now runs from the tree: 0 of 191 functions differ on CPython 3.13.2 and 0 of 196 on 3.14.7t, against master.
5 of 6 tasks
asvetlov
added a commit
that referenced
this pull request
Sep 23, 2026
<!-- Thank you for your contribution! --> ## What do these changes do? `_list_getitem_ref()` and `_list_item_gone()` were left behind in `hashtable.h` by #1533 without being about the hash table, and with no caller there any more. They move into a new `multidict/_multilib/pair.h`, along with a new `unpack_pair()` that both of their users now share: `itemsview.__contains__()` in `views.h` and `_md_parse_item()` in `bulk_update.h` each kept their own copy of the exact-tuple and exact-list fast paths, including the free-threaded reference dance the list branch needs. Each caller keeps its own slow path, because the two disagree on whether a wrong length or a foreign type is an error or a plain "no"; `unpack_pair()` reports those back rather than acting on them. Folding the wrong-length case into the foreign-type one, which would drop a state from the result, is wrong: it lets an exact list reach the general path, which re-reads the length and then indexes without the atomic accessor. `test_items_contains_list_shrunk_by_another_thread` catches it. C extension only. There is no pure-Python counterpart to keep in sync, since the split is a C header layout question; `_multidict_py.py` is untouched. ## Are there changes in behavior for the user? No. ## Is it a substantial burden for the maintainers to support this? No, it removes a duplicated fast path and finishes the split #1533 started. ## Related issue number None; this is the follow-up #1533 pointed at when it left these two helpers behind in `hashtable.h`. ## Checklist - [x] I think the code is well written - [x] Unit tests for the changes exist — N/A, no new behaviour; the existing suite covers both call sites, including the free-threaded `test_items_contains_list_shrunk_by_another_thread` - [x] Documentation reflects the changes — `AGENTS.md` header table - [ ] If you provide code modification, please add yourself to `CONTRIBUTORS.txt` — N/A, no such file in this repo - [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` <details> <summary>Agent run details (optional, for reviewers)</summary> Everything below was re-run after merging `master` at #1533, which moved `_md_parse_item()` into `bulk_update.h`. Tests: - C extension, CPython 3.13.2: `pytest -q` -> 2337 passed, 107 skipped - C extension, CPython 3.14.7t: `pytest -q` -> 2326 passed, 118 skipped - Pure Python, CPython 3.13.2: `MULTIDICT_NO_EXTENSIONS=1 pytest -q --no-c-extensions` -> 1124 passed, 17 skipped Each leg ran in its own venv against its own copy of the tree; a shared venv silently swaps which `.so` an import resolves to, and a stale `.so` left in the tree makes the pure-Python leg fail to collect `tests/test_capi.py`. Lint: `pre-commit run --all-files` passes. `make doc-spelling` passes with no additions to `docs/spelling_wordlist.txt`. Codegen, `tools/codegen_diff.py --ref upstream/master`: - CPython 3.13.2: 21 of 191 functions differ - CPython 3.14.7t: 29 of 196 functions differ A control run of the same tool against an unmodified tree reports 0 of 191 and 0 of 196, so those lists are real rather than build noise. Most entries are two changed lines that turn out to be a `.LCnn` string literal label renumbering, which the tool's normalization does not cover; the rest is the inlining reshuffle its docstring warns about. `md_update_from_seq` grows 822 -> 883 instructions on the GIL build. Callgrind instruction counts over the affected operations, GIL build, `PYTHONHASHSEED=0`, each measured as the difference between an N and a 2N iteration run so that interpreter startup cancels out: | benchmark | base | new | delta | | --- | ---: | ---: | ---: | | `MultiDict(pairs)` | 2250000 | 2240100 | -0.44% | | `extend(pairs)` | 2314490 | 2304579 | -0.43% | | `update(pairs)` | 2818962 | 2809072 | -0.35% | | `merge(pairs)` | 1693390 | 1683562 | -0.58% | | `tuple in items()` | 13230115 | 13240175 | +0.08% | | `list in items()` | 13270143 | 13280003 | +0.07% | | `items() & items()` | 22174564 | 22288011 | +0.51% | | `items() - items()` | 19905148 | 19906440 | +0.01% | An earlier draft dropped the wrong-length state from `unpack_pair()`'s result, which produced a smaller codegen diff, and the free-threaded suite rejected it with the `IndexError` described above. </details> Drafted with Claude Code (Opus 5); to be reviewed by @asvetlov before it leaves draft.
5 of 6 tasks
asvetlov
added a commit
that referenced
this pull request
Sep 24, 2026
<!-- Thank you for your contribution! --> ## What do these changes do? 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 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_readers` as nonzero 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 is the exchange. Instructions per operation, CPython 3.14.7 free-threaded: | Operation | Before | After | | --- | ---: | ---: | | `key in md` | 283 | 253 (-10.6%) | | `md[key]` | 317 | 292 (-7.9%) | | `md.get(key)`, miss | 499 | 474 (-5.0%) | `CIMultiDict` moves -8.1%, -6.2% and -3.3% on the same three. No other benchmarked 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 back to back against a build of the merge base, two passes each: `key in md` 35.5/35.3 ns before against 29.0/28.7 after, `md[key]` 37.1/36.9 against 30.9/30.6, so about -18%. `CIMultiDict` moves within the noise of this machine on wall clock and I would not claim a figure for it. This is free-threaded only. `atomic_helpers.h` is entirely inside `#ifdef Py_GIL_DISABLED`, and `tools/codegen_diff.py` reports `0 of 190 functions differ` for the GIL build. It is also C-extension only: the pure-Python backend has no table retirement to skip. The second commit regenerates `docs/benchmark.rst`. Those numbers were last 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 `multidict` against `dict` on the operations both have. ## Checklist - [x] I think the code is well written - [x] Unit tests for the changes exist - [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` On unit tests: no new test is added because the existing `test_drain_retired_defers_busy_table_thread_safety()` and `test_setitem_update_thread_safety()` already drive this path from both sides, 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. <details> <summary>Agent run details (optional, for reviewers)</summary> Three variants were measured before this one was picked. Marking the slow path `COLD` rather than `NOINLINE` bought the same read win but cost `d.clear()` 12% and `d.copy()` 4%: `cold` implies optimize-for-size and `_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. ``` tools/codegen_diff.py --ref 9f28f61 python3.14 0 of 190 functions differ python3.14t 35 of 196 functions differ (_md_drain_retired -> _md_drain_retired_slow and its former inline sites) pytest, 3.14.7 GIL 2353 passed, 107 skipped pytest, 3.14.7t 2342 passed, 118 skipped pytest, pure Python 1138 passed, 11 skipped, 1141 deselected pytest, 3.10.17 (cibw marks) 2260 passed, 21 skipped, 179 deselected TSan, 3.14.7t-tsan 2331 passed, 0 ThreadSanitizer warnings, 8m30s ASan+UBSan, 3.14.7 debug 2342 passed, 107 skipped make doc-spelling build succeeded pre-commit (clang-format) passed ``` Benchmarks are `benchmarks/callgrind_driver.py`, bracketed, `drift_ir 0`, valgrind 3.26.0, Intel Core Ultra 7 155H. </details>
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?
hashtable.hwas 2735 lines covering three separate things: the tableitself, the single-key operations, and the multi-item
extend(),update()andmerge()engine behind the constructors and the bulkmethods. The last of those was a contiguous block nothing else in the
file called into, whose only consumer is
_multidict.c.It moves verbatim into
multidict/_multilib/bulk_update.h, along withthe
UpdateOpenum:_md_update(),_md_merge(), the_md_post_update_*family,md_post_update(),md_update_from_ht(),md_extend_self(),md_update_from_dict(),md_update_from_kwnames(),md_update_from_seq()and the sequence-parsing helpers beside it. Thenew header includes
hashtable.h, soviews.h,iter.handcapsule.hkeep reaching only the hash table. This continues the splitstarted by #1510, #1513 and #1522.
Two things worth flagging for the reviewer.
_list_getitem_ref()and_list_item_gone()stay behind.views.huses them too, and it includes
hashtable.hbut not the new header, somoving them would have left
views.hcompiling only by accident of theinclude order in
_multidict.c. They now have no user left inhashtable.hitself, which deserves a proper home shared with theopen-coded two-element unpacking in
views.h; that is its own change,not this one.
The order the compiler sees does change, since the block now expands
after the rest of
hashtable.hrather than in the middle of it.tools/codegen_diff.pyreports no difference at all, so nothing fell inor out of line.
Are there changes in behavior for the user?
No. The move is verbatim, and the pure Python implementation has no
header structure to mirror.
Is it a substantial burden for the maintainers to support this?
No, it shrinks
hashtable.hby around 710 lines and keeps the bulkpaths separate from the table and its single-key API.
Related issue number
N/A
Checklist
functions are already covered through the constructors,
extend(),update()andmerge()CONTRIBUTORS.txt: N/ACHANGES/foldermake doc-spellingpasses and any new technical words are added todocs/spelling_wordlist.txtAgent run details (optional, for reviewers)
Rebased onto master after #1529 and #1530 landed; #1530 reworked the
same region, so
bulk_update.hwas rebuilt from master's text ratherthan carried over from the earlier push, and its includes now name
reflist.hinstead of the removeddeferred_decref.h.Verbatim-move check: the moved text, with the retained
_list_getitem_refblock spliced back at its original offset,reconstructs master's
hashtable.hlines 1693-2416 exactly.Codegen (
tools/codegen_diff.py --ref upstream/master):Tests,
python -m pytest tests -q -k "not test_leak", each in its ownvenv:
MULTIDICT_DEBUG_BUILD=1, asserts on): 2326passed, 107 skipped
MULTIDICT_NO_EXTENSIONS=1,--no-c-extensions): 1124passed, 17 skipped
Lint:
pre-commit run --all-filespassed;make doc-spellingpassed.Drafted with Claude Code (Opus 5); reviewed by @asvetlov.