Repository navigation
Split deferred decref and compiler macros into their own headers - #1510
Merged
Merged
Conversation
The deferred decref accumulator is self-contained and does not depend on the hash table internals, so keep it in deferred_decref.h instead of growing hashtable.h further.
UNLIKELY, COLD and ALWAYS_INLINE are not specific to the hash table keys, so move them out of htkeys.h. Drop the HT_ prefix from them and the md_ prefix from the deferred decref helpers.
asvetlov
marked this pull request as ready for review
September 21, 2026 20:31
Contributor
|
Contributor
Comments Outside DiffThese findings sit on lines the diff does not cover, so they could not be posted inline. Each one leaves this list once its file changes.
|
Member
Author
|
@aiolibsbot rr |
5 of 6 tasks
asvetlov
added a commit
that referenced
this pull request
Sep 21, 2026
…y hashes (#1505) <!-- Thank you for your contribution! --> ## What do these changes do? Duplicate-key walks in the C extension used to dedupe by OR-ing a high bit into `entry->hash` and restoring it afterwards (`setitem`, `update()`, `merge()`, `to_dict()`), or by a linearly scanned visited list (`getall()` and the views). This replaces both with `bitmap_t`, an entry-index bitmap private to the call. It keeps a 4 KB inline buffer (32768 entries) and moves to the heap for bigger tables; nothing is zeroed or allocated until the first mark. Bitmaps of up to 64 words are zeroed outright, larger ones lazily through a small summary bitmap. Words are `size_t`, so 32-bit targets don't emulate 64-bit arithmetic. `getall()` and the views record their first matches in a short list and only start the bitmap past it: after 8 matches when the bitmap fits its inline buffer, after 32 on bigger tables, where it would need a heap allocation. `update()` and `merge()` keep two per-batch bitmaps (entries written, entries half-deleted). The batch's own resizes rebuild them for the new layout; a mutation from outside the batch (Python code run between items, another thread) is detected and falls back to the old full-table sweep. `md_post_update()` visits only the recorded deletions instead of every slot. With nothing marking the table, `MD_HASH_MARK` is gone, together with the masking it forced on lock-free readers, `htkeys_build_indices()` and the consistency check. The pure-Python backend is unchanged; it keeps its own hash marks and the observable behaviour is the same. CodSpeed against master: `getall()` on keys with many values +23 to +28%, `__setitem__` +10 to +18%, `to_dict()` +11 to +17%, insert +10 to +11%, the rest unchanged, except `update()` with many duplicate keys, which is 11% slower (`test_multidict_update_str_with_duplicates`, `test_cimultidict_update_istr_with_duplicates`). That is an accepted trade-off: the old code got its "already written by this batch" check for free from the hash comparison it made anyway, while the bitmap costs a few memory reads per candidate entry. Two attempts to shave it (a sweep-or-walk heuristic in `md_post_update()` and hoisting the bitmap state into locals) made it worse under cachegrind and were dropped. ## Are there changes in behavior for the user? No, apart from speed. One corner case is now unspecified on the C side only: if Python code run between the items of an `update()` (for example a generator) mutates the same multidict, repeated keys later in that batch may overwrite rather than append. The table stays consistent. ## Is it a substantial burden for the maintainers to support this? No. It removes the mark/unmark protocol that several recent free-threading fixes had to work around, and the new code lives in self-contained headers: `bitmap.h`, `update_marks.h`, and `identity.h` plus `finder.h` split out of `hashtable.h`. ## Related issue number N/A ## Checklist - [x] I think the code is well written - [x] Unit tests for the changes exist - [x] Documentation reflects the changes (N/A, internal) - [ ] 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 Code (Opus 5); to be reviewed by @asvetlov. <details> <summary>Agent run details (optional, for reviewers)</summary> All runs on be30b7d (rebased onto e4ad2d0, #1510), isolated venv. - C extension, CPython 3.13.2 release: `pytest tests` 2160 passed, 19 skipped. - Pure Python (`--no-c-extensions`): 1080 passed, 15 skipped. - `MULTIDICT_DEBUG_BUILD=1 MULTIDICT_ASAN_BUILD=1` with libasan preloaded and `PYTHONMALLOC=malloc`, `-k "not test_leak"`: 2149 passed, 19 skipped. - CPython 3.14.7t debug build, `-k "not test_leak"`: 2137 passed, 31 skipped. TSan not run. - gdb on a debug build confirmed `tests/test_update_marks.py` reaches the bitmap remap on resize, the compacting shrink mid-batch, the heap bitmap and the lost-marks fallback sweep; the finder spills after 8 matches on a 1024-entry table and after 32 on a 40040-entry one. - The 8/32 finder limits were picked with cachegrind (data references per `getall()`, 65536-entry table): a limit of 32 beats master by 8 to 13% for 12 to 48 values per key, while 8 lost to master between 9 and about 20 values and 16 or 24 lost just past their limit. - Every new header compiles on its own. `pre-commit` hooks and `make doc-spelling` pass. </details>
asvetlov
added a commit
to asvetlov/multidict
that referenced
this pull request
Sep 21, 2026
…ename master (aio-libs#1505, aio-libs#1510) replaced the MD_HASH_MARK-based keyed walk with a bitmap-tracked finder and dropped the md_ prefix from _multilib's private names, since the directory is already namespaced. Follow both: md_readonly_finder_t/_init/_find_next/_cleanup become the unified finder_t/finder_init/find_next/finder_cleanup (the bitmap approach needs no separate readonly variant, since it never marks the table), and md_deferred_decref_t/_init/_release become deferred_decref_t/_init/ _release. Also updates two comments left stale by the same upstream change: capsule.h's and docs/capi.rst's reentrancy notes described a keyed walk that could hide matches by reentering marked entries. The bitmap finder never marks the table; the actual guard against reentrant mutation is the version check both md_next and find_next already perform, raising "MultiDict is changed during iteration" instead.
4 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? `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. This continues the split started by #1510, #1513 and #1522. Two things worth flagging for the reviewer. `_list_getitem_ref()` and `_list_item_gone()` stay behind. `views.h` uses them too, and it includes `hashtable.h` but not the new header, so moving them would have left `views.h` compiling only by accident of the include order in `_multidict.c`. They now have no user left in `hashtable.h` itself, which deserves a proper home shared with the open-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.h` rather than in the middle of it. `tools/codegen_diff.py` reports no difference at all, so nothing fell in or 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.h` by around 710 lines and keeps the bulk paths separate from the table and its single-key API. ## Related issue number N/A ## Checklist - [x] I think the code is well written - [ ] Unit tests for the changes exist: N/A, code move only; the moved functions are already covered through the constructors, `extend()`, `update()` and `merge()` - [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` <details> <summary>Agent run details (optional, for reviewers)</summary> Rebased onto master after #1529 and #1530 landed; #1530 reworked the same region, so `bulk_update.h` was rebuilt from master's text rather than carried over from the earlier push, and its includes now name `reflist.h` instead of the removed `deferred_decref.h`. Verbatim-move check: the moved text, with the retained `_list_getitem_ref` block spliced back at its original offset, reconstructs master's `hashtable.h` lines 1693-2416 exactly. Codegen (`tools/codegen_diff.py --ref upstream/master`): - CPython 3.13.2: 0 of 191 functions differ - CPython 3.14.7t: 0 of 196 functions differ Tests, `python -m pytest tests -q -k "not test_leak"`, each in its own venv: - 3.13.2 release build: 2326 passed, 107 skipped - 3.13.2 debug build (`MULTIDICT_DEBUG_BUILD=1`, asserts on): 2326 passed, 107 skipped - 3.14.7t debug build: 2315 passed, 118 skipped - Pure Python (`MULTIDICT_NO_EXTENSIONS=1`, `--no-c-extensions`): 1124 passed, 17 skipped Lint: `pre-commit run --all-files` passed; `make doc-spelling` passed. </details> Drafted with Claude Code (Opus 5); reviewed by @asvetlov.
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?
Moves
md_deferred_decref_tand its helpers out ofhashtable.hinto a newmultidict/_multilib/deferred_decref.h, and theHT_UNLIKELY/HT_COLD/HT_ALWAYS_INLINEmacros out ofhtkeys.hinto a newcompiler.h. Themd_prefix is dropped from the deferred decref names (deferred_decref_t,deferred_decref_push(), ...) and theHT_prefix from the macros. Otherwise the code is moved verbatim. The pure Python backend is unaffected.Are there changes in behavior for the user?
No.
Is it a substantial burden for the maintainers to support this?
No, it shrinks
hashtable.handhtkeys.hand keeps self-contained helpers separate from the hash table logic.Related issue number
N/A
Checklist
CONTRIBUTORS.txtN/ACHANGES/foldermake doc-spellingpasses and any new technical words are added todocs/spelling_wordlist.txtDrafted with Claude Code (Claude Opus 5); reviewed by @asvetlov.
Agent run details (optional, for reviewers)
Tests (
pytest -q -k "not test_leak", CPython 3.14t):C extension release build: 2097 passed, 31 skipped
C extension
MULTIDICT_DEBUG_BUILD=1: 2097 passed, 31 skippedPure Python (
--no-c-extensions): 1056 passed, 9 skippedLint: pre-commit clang-format passed;
make doc-spellingpassed.