Repository navigation
Add a CI check that hot paths keep their helpers inlined - #1612
Conversation
tools/check_inlining.py builds the extension with release flags, reads the disassembly and fails when a rule's helper is called out of line from a hot function. The first rules cover the inlining regressions seen in aio-libs#1527, aio-libs#1530, aio-libs#1541, aio-libs#1542, aio-libs#1554, aio-libs#1591 and aio-libs#1604.
|
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.
|
A rule now names slots, methods and C API functions, which GCC always keeps, and fails when the helper is reached through any chain of out-of-line calls. A listed function that GCC inlined away fails instead of passing silently.
<!-- Thank you for your contribution! --> ## What do these changes do? With GCC 13, which CI and CodSpeed build with, `md_calc_identity()` fell out of line in `getall()` and `md_contains()` on free-threaded builds, and `md_contains()` fell out of `keys().isdisjoint()` on regular ones (the two failures #1612 reports). This keeps code whose speed does not matter off GCC's inlining budget, and forces the one helper that does inline: - `md_contains()` loses its `pret` argument. The keys view `&` and `-` operators, the only users of the stored key, call a new `NOINLINE md_find_key()` instead. - Every view and iterator slot and method, and every function run only at module init (`multidict_views_init`, `multidict_iter_init`, `istr_init`, `htkeys_pools_init`, `pool_init`, `new_capsule`), is `NOINLINE`. - `md_calc_identity()` is `ALWAYS_INLINE`. The pure-Python implementation has no equivalent and is unchanged. ## Are there changes in behavior for the user? No, only speed. Measured with callgrind on GCC 13, instructions per operation against master: free-threaded `del d[key]` -7.9% (`MultiDict`) and -7.5% (`CIMultiDict`), `key in d` -3.8% and -6.1%, `CIMultiDict` `d[key]` -2.1%; regular `update()` -3.6% and -3.5%. The worst regressions are +0.9% on free-threaded `MultiDict.update()` and +0.5% to +0.8% on regular `key in d`. Geomean over 52 cells: -0.28% regular, -0.66% free-threaded. ## Is it a substantial burden for the maintainers to support this? No. ## Related issue number Related to #1612 ## Checklist - [x] I think the code is well written - [ ] Unit tests for the changes exist (N/A, no behavior change; covered by the existing suite) - [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 Code (Claude Opus 5.5); reviewed by @asvetlov. <details> <summary>Agent run details (optional, for reviewers)</summary> Measurement: `benchmarks/callgrind_driver.py --include-multidict-only`, bracketed, `PYTHONHASHSEED=0`, CPython 3.14.7 and 3.14.7t, extension built with GCC 13.5 (`gcc:13` image) and the interpreter's own CFLAGS plus the `setup.py` release flags. `ALWAYS_INLINE md_calc_identity` alone gives geomean -0.15% regular / -0.59% free-threaded; the `NOINLINE` changes on top add regular `update()` -3% and free-threaded `key in d` -2%. `tools/check_inlining.py` from #1612: all rules pass on both builds with GCC 13 and GCC 15 (master fails two with GCC 13). Tests: C extension on 3.14.7 and 3.10.17, 2803 passed each; pure Python (`MULTIDICT_NO_EXTENSIONS=1`, `--no-c-extensions`) on 3.14.7, 1324 passed. Free-threaded suite left to CI. Lint: `pre-commit run` clean; `make doc-spelling` clean. </details>
| Rule( | ||
| "md_next", | ||
| ITERNEXT_ENTRIES, | ||
| BOTH, | ||
| "#1601 prototype: items iteration +19% on FT", |
There was a problem hiding this comment.
The revised rules omit keys().isdisjoint() from the md_next check and remove its md_contains check. An out-of-line md_contains call in this method's per-key loop can therefore pass unnoticed, despite a previously measured 31% regression. The repository requires a measurement in the PR body to justify weakening an inlining rule, but none is provided for these removals. Restore the protection or provide the measurement; this requirement must be satisfied before merging.
Context Used: AGENTS.md (source)
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
Artifacts
Authored inlining verification script
- This exact script compiled temporary source variants, read their disassembly, and invoked each revision’s real check() — it makes the comparison reproducible.
Former checker detects both isdisjoint calls
- Running the HEAD^ checker on the controlled call graph reported both out-of-line calls — the former rules protected this entry point.
Revised checker passes despite both isdisjoint calls
- Running the current checker on the same controlled call graph found zero targeted failures despite both calls appearing in disassembly — the protection is absent.
Unmodified local build's isdisjoint call listing
- Running the checker’s --show command on unmodified source did not show either call on this Python 3.11 build — the demonstrated regression is a controlled possibility, not an observed regression in that build.
PR body describing inlining checks
- Fetching PR Add a CI check that hot paths keep their helpers inlined #1612’s body showed its inlining-check claims but no measurement justifying removal of these two protections — the AGENTS.md condition is not documented there.
What do these changes do?
Adds
tools/check_inlining.pyand anInliningCI job on 3.14 and 3.14t. The script compiles_multidict.cwith the interpreter's flags plus the release flags fromsetup.py, disassembles it, and fails when an entry point a rule names (a slot, method orMultiDict_*C API function) reaches the rule's helper through any chain of out-of-line calls; the report shows the chain. The first rules come from the regressions in #1527, #1530, #1541, #1542, #1554, #1591 and #1604, each of which slowed a benchmark on code the change never touched. AGENTS.md now asks agents to run it, to add a rule whenever they fix an inlining regression or pin a helper, and never to weaken a rule without a measurement.CI builds with GCC 13.3. The two rules it failed on master at first (
md_calc_identityout of line ingetall()andpopall()on free-threaded builds) were fixed by #1614; master now passes every rule with GCC 13 and GCC 15 on both builds.Are there changes in behavior for the user?
No.
Is it a substantial burden for the maintainers to support this?
No. A PR that moves an inlining decision on purpose edits the matching rule.
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); reviewed by @asvetlov.
Agent run details (optional, for reviewers)
Master passes on 3.14.7 and 3.14.7t with GCC 14 and GCC 15.
Each past regression reintroduced by hand was caught:
ALWAYS_INLINEfromhtkeysiter_init: out-of-line copy reported, plusmd_containsout ofisdisjointNOINLINEfrom the view, iterator and shell pool helpers (Pool the view and iterator shells #1541/Pool the multidict and proxy shells #1542):md_containsout ofisdisjointNOINLINEfrom_ci_str_to_identity(Compute the ASCII identity of a CIMultiDict key directly #1527): failures across getitem, getall, replace andisdisjointon both buildsGCC 13.5 (
gcc:13Docker image): the two failures listed above.Lint:
pre-commit runclean;make doc-spellingclean.