Skip to content

Always inline the hash-table probe initializer - #1554

Merged
asvetlov merged 2 commits into
aio-libs:masterfrom
asvetlov:claude/htkeysiter-init-inline
Sep 24, 2026
Merged

asvetlov merged 2 commits into
aio-libs:masterfrom
asvetlov:claude/htkeysiter-init-inline

Conversation

@asvetlov

Copy link
Copy Markdown
Member

What do these changes do?

htkeysiter_init() sets five fields of the struct that walks a hash
chain, and every lookup, insertion and deletion in the extension starts
by calling it. GCC was emitting it out of line, so each of those paths
opened with a call. Marking it ALWAYS_INLINE removes the call.

Instructions per operation on a GIL build of CPython 3.13, measured with
benchmarks/callgrind_driver.py against 3336132:

operation MultiDict CIMultiDict
for k, v in d.items() -8.8% -8.7%
d.setdefault(key, v), new key -5.0% -2.5%
d.get(key), miss -4.5% -2.2%
d.pop(key) -4.1% -2.2%
d.update(other) -2.9% -2.3%
d[key] = v, existing key -2.7% -2.2%
d.getall(key) -1.7% -1.2%
d.popitem() -1.7% -0.6%

Nothing measured slower on either class. Free-threaded builds already
inlined it: tools/codegen_diff.py reports the emitted code byte for
byte identical there, so the change is a no-op on that build.

Are there changes in behavior for the user?

No, other than the speed. The function's body is untouched.

Is it a substantial burden for the maintainers to support this?

No. It is one attribute, next to the existing ALWAYS_INLINE on
md_calc_identity_hash() and for the same reason.

Related issue number

None.

Checklist

  • I think the code is well written
  • Unit tests for the changes exist — N/A, no behaviour changes; the existing suite covers every path this touches
  • Documentation reflects the changes — N/A
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt — N/A, already listed
  • 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
Agent run details (optional, for reviewers)

Tests, all against this branch:

  • GIL 3.13.2, C extension: 2587 passed, 100 skipped
  • Free-threaded 3.14.7t: 2575 passed, 112 skipped
  • CPython 3.10.17, C extension: 2501 passed, 21 skipped
  • MULTIDICT_NO_EXTENSIONS=1, 3.13.2: 1243 passed, 17 skipped

Codegen: tools/codegen_diff.py --ref upstream/master reports 109 of 200
functions differing on 3.13 with htkeysiter_init gone as a standalone
symbol, and 0 of 203 on 3.14.7t.

Measurements are deterministic instruction counts under Callgrind with
PYTHONHASHSEED=0; two runs of the same build agreed on all 20 cells.

Drafted with Claude Opus 5; reviewed by @asvetlov.

GCC emits it out of line otherwise, so every probe in the extension
opens with a call for five stores.
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided There is a change note present in this PR label Sep 24, 2026
@asvetlov
asvetlov force-pushed the claude/htkeysiter-init-inline branch from 1f0a982 to 442570f Compare September 24, 2026 06:58
@codspeed

codspeed Bot commented Sep 24, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 310 untouched benchmarks


Comparing asvetlov:claude/htkeysiter-init-inline (442570f) with master (3336132)

Open in CodSpeed

@asvetlov
asvetlov marked this pull request as ready for review September 24, 2026 07:44
@asvetlov
asvetlov requested a review from webknjaz as a code owner September 24, 2026 07:44
@asvetlov
asvetlov merged commit 130bd92 into aio-libs:master Sep 24, 2026
62 checks passed
@asvetlov
asvetlov deleted the claude/htkeysiter-init-inline branch September 24, 2026 07:46
@greptile-apps

greptile-apps Bot commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 4/5

The benchmark-table requirement must be satisfied before merging.

Reviews (1) · Last reviewed commit: "Add a CHANGES fragment"

static inline void
/* Always inlined: left to itself GCC emits it out of line, and then
every probe in the extension opens with a call for five stores. */
ALWAYS_INLINE static inline void

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Refresh the benchmark tables

Forcing the probe initializer inline changes a performance-sensitive path, but the published instruction-count tables in docs/benchmark.rst remain unchanged despite the reported improvement of up to 8.8%. This violates the repository directive to refresh those tables when an operation moves by more than a couple of percent. Update the affected rows before merging so readers are not given stale comparisons.

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!

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