Skip to content

Keep view, iterator and init code off the inlining budget - #1614

Merged
asvetlov merged 4 commits into
aio-libs:masterfrom
asvetlov:md-contains-find-key
Sep 27, 2026
Merged

asvetlov merged 4 commits into
aio-libs:masterfrom
asvetlov:md-contains-find-key

Conversation

@asvetlov

Copy link
Copy Markdown
Member

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

  • I think the code is well written
  • Unit tests for the changes exist (N/A, no behavior change; covered by the existing suite)
  • Documentation reflects the changes
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt (N/A)
  • Add a new news fragment into the CHANGES/ folder
  • 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.

Agent run details (optional, for reviewers)

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.

md_contains() loses its pret argument; the two view operators that
need the stored key call a new out-of-line md_find_key(). Every view
and iterator slot and every module-init helper is NOINLINE, and
md_calc_identity() is ALWAYS_INLINE, which puts it back into
getall() and md_contains() on free-threaded builds with GCC 13.
@asvetlov
asvetlov marked this pull request as ready for review September 27, 2026 18:29
@asvetlov
asvetlov requested a review from webknjaz as a code owner September 27, 2026 18:29
@codspeed

codspeed Bot commented Sep 27, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 674 untouched benchmarks


Comparing asvetlov:md-contains-find-key (5cd6eab) with master (f61aa79)

Open in CodSpeed

@greptile-apps

greptile-apps Bot commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Medium risk] Compiler optimization hints for performance tuning.

No blocking issues were identified.

Reviews (2) · Last reviewed commit: "Merge branch 'master' into md-contains-f..."

@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided There is a change note present in this PR label Sep 27, 2026
@asvetlov
asvetlov merged commit fe0c53b into aio-libs:master Sep 27, 2026
65 checks passed
@asvetlov
asvetlov deleted the md-contains-find-key branch September 27, 2026 19:03
asvetlov added a commit that referenced this pull request Sep 28, 2026
<!-- Thank you for your contribution! -->

## What do these changes do?

Adds `tools/check_inlining.py` and an `Inlining` CI job on 3.14 and
3.14t. The script compiles `_multidict.c` with the interpreter's flags
plus the release flags from `setup.py`, disassembles it, and fails when
an entry point a rule names (a slot, method or `MultiDict_*` 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_identity` out of line in `getall()` and `popall()` 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

- [x] I think the code is well written
- [ ] Unit tests for the changes exist (N/A, a CI-only tool; verified by
reintroducing past regressions, see below)
- [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>

Master passes on 3.14.7 and 3.14.7t with GCC 14 and GCC 15.

Each past regression reintroduced by hand was caught:
- dropping `ALWAYS_INLINE` from `htkeysiter_init`: out-of-line copy
reported, plus `md_contains` out of `isdisjoint`
- dropping `NOINLINE` from the view, iterator and shell pool helpers
(#1541/#1542): `md_contains` out of `isdisjoint`
- dropping `NOINLINE` from `_ci_str_to_identity` (#1527): failures
across getitem, getall, replace and `isdisjoint` on both builds

GCC 13.5 (`gcc:13` Docker image): the two failures listed above.
Lint: `pre-commit run` clean; `make doc-spelling` clean.
</details>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided There is a change note present in this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant