Skip to content

Keep slot-level operations out of line - #1600

Merged
asvetlov merged 3 commits into
aio-libs:masterfrom
asvetlov:inline-annotations
Sep 26, 2026
Merged

asvetlov merged 3 commits into
aio-libs:masterfrom
asvetlov:inline-annotations

Conversation

@asvetlov

@asvetlov asvetlov commented Sep 26, 2026 •

Copy link
Copy Markdown
Member

What do these changes do?

_multidict.c and every _multilib header form one translation unit, and GCC caps how much the whole unit may grow through inlining (inline-unit-growth, 40%). -fopt-info-inline shows 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 NOINLINE the 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 as mp_ass_subscript, where delitem then pays md_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 NOINLINE macro, 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.

operation MultiDict CIMultiDict
getitem_hit 246 -> 234 (-4.9%) 296 -> 286 (-3.4%)
get_miss 380 -> 371 (-2.4%) 419 -> 412 (-1.7%)
contains_hit 195 -> 195 250 -> 247 (-1.2%)
setitem_replace 511 -> 505 (-1.2%) 562 -> 556 (-1.1%)
pop 549 -> 538 (-2.0%) 600 -> 586 (-2.3%)
popitem 677 -> 665 (-1.8%) 1,728 -> 1,716 (-0.7%)
ctor_empty 893 -> 878 (-1.7%) 981 -> 966 (-1.5%)
ctor_items 70,308 -> 69,893 (-0.6%) 81,336 -> 80,321 (-1.2%)

Are 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

  • I think the code is well written
  • Unit tests for the changes exist (no behavior change; the existing suite covers these paths)
  • Documentation reflects the changes (N/A, internal only)
  • 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); pending review by @asvetlov before leaving draft.

Agent run details (optional, for reviewers)

Tests, merged with master 8708f36:

  • C extension, 3.14.7: 2790 passed, 170 skipped
  • C extension, 3.14.7t: 2777 passed, 183 skipped
  • C extension, 3.10.17: 2790 passed, 170 skipped
  • Pure Python, 3.14.7 (MULTIDICT_NO_EXTENSIONS=1 --no-c-extensions): 1320 passed, 17 skipped

Lint: pre-commit hooks pass. make doc-spelling passes.

Most of the free-threaded refusals are Py_DECREF() and Py_NewRef(), whose free-threaded bodies are large, so hot loops call a local out-of-line copy for each reference (about 40 instructions per del d[key]). A first revision forced them inline by redeclaring them always_inline, which gained another 3 to 10% there, but that does not work on Windows and macOS and was dropped. --param=inline-unit-growth=400 removes every refusal but is a GCC-only flag and grows the code by 36 to 54%. Adding NOINLINE to 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.

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.
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided There is a change note present in this PR label Sep 26, 2026
@codspeed

codspeed Bot commented Sep 26, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 310 untouched benchmarks


Comparing asvetlov:inline-annotations (acef3c8) with master (8708f36)

Open in CodSpeed

Redeclaring Py_DECREF() and _Py_NewRef() always_inline does not work on
Windows and macOS. The NOINLINE annotations stand on their own.
@asvetlov asvetlov changed the title Inline refcounting and keep slot-level operations out of line Keep slot-level operations out of line Sep 26, 2026
@asvetlov
asvetlov marked this pull request as ready for review September 26, 2026 16:03
@asvetlov
asvetlov requested a review from webknjaz as a code owner September 26, 2026 16:03
@greptile-apps

greptile-apps Bot commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 4/5

[Medium risk] Changes C extension inlining strategy for performance.

The outstanding changelog requirement must be satisfied before merging.

Reviews (2) · Last reviewed commit: "Merge remote-tracking branch 'upstream/m..."

Comment thread CHANGES/1600.feature.rst Resolved
# Conflicts:
#	multidict/_multilib/hashtable.h
@asvetlov
asvetlov merged commit d66879b into aio-libs:master Sep 26, 2026
62 checks passed
@asvetlov
asvetlov deleted the inline-annotations branch September 26, 2026 16:34
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>
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