Repository navigation
Keep slot-level operations out of line - #1600
Merged
Merged
Conversation
The module's inlining budget runs out on free-threaded builds, and GCC then calls Py_DECREF() and Py_NewRef() out of line in hot loops. Force those inline, and mark the operations behind Python slots, the bulk update loops and the rare insert-time compaction NOINLINE so they stop competing for the budget.
Redeclaring Py_DECREF() and _Py_NewRef() always_inline does not work on Windows and macOS. The NOINLINE annotations stand on their own.
5 of 6 tasks
asvetlov
marked this pull request as ready for review
September 26, 2026 16:03
Contributor
|
# Conflicts: # multidict/_multilib/hashtable.h
5 of 6 tasks
asvetlov
added a commit
that referenced
this pull request
Sep 26, 2026
<!-- Thank you for your contribution! --> ## What do these changes do? `del d[key]`, `d[key] = value` and `pop()` tested `md->watch` for every event they might record. Stores and decrefs in between force a reload each time, and the extra code made the probe loop spill. On a multidict nobody watches, that was most of the regression #1551 left in these rows. Each operation now reads `md->watch` once under its critical section. When it is `NULL`, the operation runs a copy of its locked body with no watcher code in it; the watched copy is `COLD`. The not-found `md_add_with_hash()` call moved out of `_md_replace()` so the hot path keeps a single inlined copy of it. The second copies use up GCC's inline budget, which is already at `inline-unit-growth` for this translation unit. GCC reacted by outlining `_md_del_at()` from `popitem()` and `htkeys_set_index()` from the free-threaded insert path. Both are now `ALWAYS_INLINE`. Callgrind `ir_per_op`, `multidict_c`, against 2f84475 (rows that moved by more than 0.1%): | operation | GIL 3.14.7 | free-threaded 3.14.7t | | --- | --- | --- | | `del d[key]` | 250.8 -> 237.5 (-5.3%) | 408.6 -> 383.3 (-6.2%) | | `d[key] = v`, existing key | 329.6 -> 325.5 (-1.2%) | 505.1 -> 506.9 (+0.4%) | | `d[key] = v`, new key | 403.1 -> 398.4 (-1.2%) | 610.5 -> 609.0 (-0.2%) | | `d.pop(key)` | 390.8 -> 389.8 (-0.3%) | 537.8 -> 533.8 (-0.7%) | | `d.getall(key)` | flat | 856.1 -> 844.2 (-1.4%) | | `d[key] = v`, `istr` key | 876.0 -> 884.9 (+1.0%) | 1028.8 -> 1032.6 (+0.4%) | GIL `delitem` is back to 237.5, against 236.5 before #1551. The `istr` setitem row is the one that got worse: the watch tests fell from 13 to 3 Ir per call, but the case-insensitive compare in the probe loop got a worse register allocation. Every other row is unchanged. ## Are there changes in behavior for the user? No, except in a corner of the watchers C API. A watcher that a `__del__` attaches while `del d[key]` is still running now sees none of that delete. Before, it received the remaining `DELETED` events and a `BATCH_END` with no `BATCH_BEGIN`. A new test covers this. ## Is it a substantial burden for the maintainers to support this? No. The watched and unwatched paths share one body and differ only in a constant `bool`. The watchers API is C-extension only, so the pure-Python implementation is not affected. ## Related issue number Follows up on #1551 and #1600. ## 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, existing contributor) - [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 (Claude Opus 5.5); reviewed by @asvetlov. <details> <summary>Agent run details (optional, for reviewers)</summary> Measured with `benchmarks/callgrind_driver.py --impl multidict_c --include-multidict-only`, one git worktree and one venv per build, `.so` rebuilt from scratch and timestamp-checked before each run. Base and change were built in the same tree. The 6e6ac72 comparison comes from a separate re-measure that reproduced the previous published tables exactly. Per-function callgrind for GIL `delitem`: all +14.3 Ir/op against 6e6ac72 sat in `md_del`. 7 Ir were the `md->watch` tests (watch.h lines) and the rest were spills in the probe loop. `tools/codegen_diff.py --ref 2f84475` (3.14.7 and 3.14.7t): new symbols are the three cold copies plus out-of-line `htkeysiter_next` and `reflist_push`. `objdump` shows those two called only from the cold copies and existing `.cold` partitions, never from a hot path. On free-threaded builds the standalone `htkeys_set_index` is gone. Variants tried and dropped: one body with the flag read into a runtime local (GIL delitem only -4.2%); the same for `_md_replace` (setitem_replace +0.2%, setitem_istr +1.2%); passing `&flush` to the cold helper (turned on the stack protector in `md_pop_one`, GIL pop +1.8%). Tests: `pytest` passes on 3.14.7, 3.14.7t, 3.10.17 and pure Python (`MULTIDICT_NO_EXTENSIONS=1 --no-c-extensions`, 3.13.2), each in its own tree copy and venv. `make doc-spelling`: passes. </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?
_multidict.cand every_multilibheader form one translation unit, and GCC caps how much the whole unit may grow through inlining (inline-unit-growth, 40%).-fopt-info-inlineshows the cap refusing 98 call sites on the GIL build and 662 on the free-threaded one, so unrelated edits keep moving these decisions (#1601 fixed one such case on the insert path).This marks
NOINLINEthe operations that sit behind Python slots (md_replace(),md_del(),md_repr()) and the bulk update loops (md_update_from_dict/ht/seq(),md_extend_self()). Otherwise they compete with the hot paths for the budget, and freed budget goes to inlining them into a slot such asmp_ass_subscript, wheredelitemthen paysmd_replace()'s register saves. The bulk loops are refused today anyway, so for them the attribute only pins the current layout.It uses only the existing
NOINLINEmacro, which also works on MSVC, with no build flags. The pure-Python implementation has nothing equivalent.Instructions per operation on 3.14.7t against master 8708f36 (Callgrind,
benchmarks/callgrind_driver.py), rows that moved at least 1%. On 3.14.7 no row moved 1% or more.MultiDictCIMultiDictgetitem_hitget_misscontains_hitsetitem_replacepoppopitemctor_emptyctor_itemsAre there changes in behavior for the user?
No.
Is it a substantial burden for the maintainers to support this?
No, these are annotations.
Related issue number
N/A
Checklist
CONTRIBUTORS.txt(N/A)CHANGES/foldermake doc-spellingpasses and any new technical words are added todocs/spelling_wordlist.txtDrafted with Claude Code (Claude Opus 5.5); pending review by @asvetlov before leaving draft.
Agent run details (optional, for reviewers)
Tests, merged with master 8708f36:
MULTIDICT_NO_EXTENSIONS=1 --no-c-extensions): 1320 passed, 17 skippedLint: pre-commit hooks pass.
make doc-spellingpasses.Most of the free-threaded refusals are
Py_DECREF()andPy_NewRef(), whose free-threaded bodies are large, so hot loops call a local out-of-line copy for each reference (about 40 instructions perdel d[key]). A first revision forced them inline by redeclaring themalways_inline, which gained another 3 to 10% there, but that does not work on Windows and macOS and was dropped.--param=inline-unit-growth=400removes every refusal but is a GCC-only flag and grows the code by 36 to 54%. AddingNOINLINEto slot-only functions themselves does nothing: 83 of the 98 slot and method functions have no direct caller.Wall clock: not re-measured for this revision. The pyperf A/B run was for the first revision, which included the dropped redeclaration.
Not tested: musl, macOS, Windows and Android builds.