Repository navigation
Shrink the walk bitmap's inline buffer - #1543
Merged
Merged
Conversation
6 tasks done
asvetlov
force-pushed
the
claude/bitmap-inline-buffer
branch
from
September 23, 2026 18:50
f2ebc8e to
23e23cd
Compare
asvetlov
marked this pull request as ready for review
September 23, 2026 19:18
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1543 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 40 40
Lines 6820 6852 +32
Branches 596 596
=========================================
+ Hits 6820 6852 +32
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:
|
Contributor
|
bitmap_t carried a 4 KB inline word buffer. It sits in the caller's frame, twice over inside update_marks_t, and md_walk() is always inlined, so that frame lands in every getall(), popall(), __delitem__(), __contains__() and to_dict(). Past about 1 KB it costs those callers more in the inlining it crowds out than it saves in allocations. Nothing else changes: _bitmap_start() already falls back to the heap past the inline capacity, so only the crossover moves, and a table of up to 8192 entries still gets its bitmap for free. Instructions per operation on a GIL build, at 4096, 2048, 1024 and 512 bytes: a getall() that hits is 1055, 1054, 1026, 1026, and one over a 16384-entry table is 270685, 270429, 266973, 266973. Everything else is flat to within 0.1%, popall() over the same table exactly so. 1024 is where it stops paying, and it keeps twice the inline capacity of 512 for the same result. 512 is also the floor: below it the summary array rounds down to zero entries.
asvetlov
force-pushed
the
claude/bitmap-inline-buffer
branch
from
September 23, 2026 19:37
23e23cd to
836d77f
Compare
asvetlov
added a commit
that referenced
this pull request
Sep 24, 2026
<!-- Thank you for your contribution! --> ## What do these changes do? `htkeys_new()` and `htkeys_free()` carried a standing TODO to do what CPython does for `PyDictKeysObject`. They now check a bounded per-size-class pool held in the module state, which every multidict reaches through its cached `state` pointer. A miss allocates and a full pool frees, so the ordinary path is unchanged. Size classes `log2_size` 3 to 8, at depths 32, 32, 32, 16, 8 and 4: about 109 KB per interpreter with every pool full. The ladder stops where a 100-item constructor pre-sizes to, since past that the allocation is a small part of filling the table. Nothing is pooled on a free-threaded build, and that was the measurement that decided the design. A shared pool needs an atomic exchange to pop and another to push; on x86 an exchange is a locked operation whatever memory order it asks for, which came to a flat ~41 cycles per round trip against mimalloc's 30-52, so it lost below a 1456-byte block. mimalloc already gives every thread its own freelist. On a GIL build the same pool is ~10 cycles against pymalloc's 26-55 and libc `malloc`'s 117-158. Instruction counts said the opposite, 22 against 108-312, which is the blind spot: callgrind cannot see what a locked exchange costs. The pools live in the module state rather than at file scope because under a per-interpreter GIL a block has to be freed by the interpreter that allocated it. They hold no reference, so `module_traverse()` needs no change; `module_clear()` drains them. `MULTIDICT_NO_FREELIST=1` builds without them. A pooled block never reaches `free()`, so AddressSanitizer can neither poison it nor report a use-after-free on it, and an ASan run now wants a second pass with that set. AGENTS.md says so. ## Are there changes in behavior for the user? No. Faster construction and teardown, nothing observable otherwise. ## Is it a substantial burden for the maintainers to support this? No. The pool is one small header, and it degrades to the present behaviour: an empty pool allocates, a full one frees, and a pool that was never given a capacity misses every time. ## Related issue number None. This is the TODO that stood in `htkeys.h` at `htkeys_new()` and `htkeys_free()`. ## Checklist - [x] I think the code is well written - [x] Unit tests for the changes exist - [x] Documentation reflects the changes - [x] 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` --- First of five. #1540, #1541 and #1542 stack on this one and include its commits until it lands; #1543 is independent. GitHub cannot base a cross-repo pull request on a fork branch, so all five are opened against `master`. Drafted with Claude Opus 5 (Claude Code); reviewed by @asvetlov. <details> <summary>Agent run details (optional, for reviewers)</summary> Tests: `pytest -q` on 3.13.2 and 3.14.7t, release and `MULTIDICT_DEBUG_BUILD=1`, plus `MULTIDICT_NO_EXTENSIONS=1 pytest -q --no-c-extensions`, `pytest -q -m hypothesis` on both builds, and `pytest -q -m leaks`. All pass. ASan: `MULTIDICT_DEBUG_BUILD=1 MULTIDICT_ASAN_BUILD=1`, run twice, once as built and once with `MULTIDICT_NO_FREELIST=1`. No reports either way. Perf: callgrind with `PYTHONHASHSEED=0`, GC collected and disabled around the loop, a matching empty-body run subtracted. The cycle figures quoted above are `perf stat -e cycles:u` pinned to one P-core, min of 9 runs of 20M iterations, from a standalone probe module built with the extension's own flags. Cycles rather than instructions because that is the only way to see what the free-threaded pool's locked exchange costs. Not pooled, having been measured and rejected: reflist blocks (`_reflist_grow` is 0.20% inclusive of a workload built to hammer it, 2000 popalls of 200 values under one key), resume slots, and the bitmap heap block. </details>
6 tasks done
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?
bitmap_tcarried a 4 KB inline word buffer. It sits in the caller'sframe, twice over inside
update_marks_t, andmd_walk()is alwaysinlined, so that frame lands in every
getall(),popall(),__delitem__(),__contains__()andto_dict(). Past about 1 KB itcosts those callers more in the inlining it crowds out than it saves in
allocations.
Nothing else changes.
_bitmap_start()already falls back to the heappast the inline capacity, so only the crossover moves, and a table of up
to 8192 entries still gets its bitmap for free.
Instructions per operation on a GIL build, swept at 4096, 2048, 1024 and
512 bytes:
getall()that hitsgetall(), 16384-entry tableEverything else is flat to within 0.1%, and
popall()over the sametable exactly so. The cliff is between 2048 and 1024. 1024 keeps twice
the inline capacity of 512 for the same result, and 512 is the floor
anyway: below it
BITMAP_INLINE_SUMMARYrounds down to zero entries andthe struct does not compile.
Are there changes in behavior for the user?
No. A walk over a table between 8192 and 32768 entries now allocates a
bitmap where it did not before, but only once it has more matches than
the short list holds, and the numbers above cover that case.
Is it a substantial burden for the maintainers to support this?
No, it is one constant.
Related issue number
None.
Checklist
tests/test_update_marks.pyalready crosses the inline/heap boundary and passes at every value sweptCONTRIBUTORS.txt— N/A, no such file in this repoCHANGES/foldermake doc-spellingpasses and any new technical words are added todocs/spelling_wordlist.txtFifth of five and independent of the other four, which are #1539, #1540,
#1541 and #1542. It came out of the same investigation.
Drafted with Claude Opus 5 (Claude Code); reviewed by @asvetlov.
Agent run details (optional, for reviewers)
Tests:
pytest -qon 3.13.2 and 3.14.7t, release andMULTIDICT_DEBUG_BUILD=1, plusMULTIDICT_NO_EXTENSIONS=1 pytest -q --no-c-extensionsandpytest -q -m leaks. All pass.Perf: callgrind with
PYTHONHASHSEED=0, GC collected and disabled aroundthe loop, a matching empty-body run subtracted.