Skip to content

Merge the bodies of __init__(), extend(), update() and merge() - #1673

Merged
asvetlov merged 1 commit into
aio-libs:masterfrom
asvetlov:merge-bulk-entry-points
Oct 4, 2026
Merged

asvetlov merged 1 commit into
aio-libs:masterfrom
asvetlov:merge-bulk-entry-points

Conversation

@asvetlov

@asvetlov asvetlov commented Oct 4, 2026

Copy link
Copy Markdown
Member

What do these changes do?

__init__(), extend(), update() and merge() in _multidict.c were four copies of the same structure: parse the arguments, pick one of three locking arms by the type of the source, run the batch, flush the watchers. They now share _multidict_bulk() and _multidict_bulk_locked(), which removes about 140 lines from _multidict.c.

Both helpers are ALWAYS_INLINE and take the operation as a constant, so every entry point still compiles its own copy, as the check_inlining.py rule from #1591 requires; the rule now names _multidict_bulk.

md_post_update() moves out of line. Left to GCC it was inlined into one arm of update(), and on free-threaded builds that took Py_NewRef() out of the items iterator (+5 instructions per step) and list_getitem_ref() out of the constructor loop. There is no way to express a NOINLINE pin as a rule in check_inlining.py, so the comment on the function carries the reason.

No behaviour change. The pure-Python implementation is not affected.

Measured with callgrind, before and after, on 3.14.7 and 3.14.7t, both classes:

  • benchmarks/callgrind_driver.py --include-multidict-only: every row within 0.06%.
  • The four entry points themselves, with a list, a dict, a multidict of the same class, one of the other class, and keyword arguments only as the source: all 80 cells between -0.12% and +0.29%. merge() is the consistent mover, about +0.1% to +0.2% (roughly 20 to 30 instructions per call) for the call to md_post_update().

Are there changes in behavior for the user?

No.

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

No.

Related issue number

N/A

Checklist

  • I think the code is well written
  • Unit tests for the changes exist (the existing suite; no behaviour change)
  • Documentation reflects the changes (N/A)
  • 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 Fable 5.1); not yet reviewed by a human.

Agent run details (optional, for reviewers)

All numbers are from the branch rebased onto cfcf7f3.

  • tools/codegen_diff.py is not empty for this change (30 functions on 3.14.7 and 184 on 3.14.7t before the rebase), which is why it was measured instead.
  • Out-of-line call targets of multidict_items_iter_tp_iternext, _md_update_from_seq_extend_ci/cs, md_del_ci/cs on 3.14.7t are the same as on the base; without NOINLINE on md_post_update() they differ.
  • tools/check_inlining.py on 3.14.7 and 3.14.7t: ok
  • C extension: 3.14.7 4077 passed, 3.14.7t 4062 passed, 3.10.17 4081 passed
  • Pure Python, 3.14.7, clean tree, --no-c-extensions: 1604 passed
  • MULTIDICT_DEBUG_BUILD=1 MULTIDICT_ASAN_BUILD=1, 3.14.7, PYTHONMALLOC=malloc, -k "not test_leak": 4065 passed
  • make doc-spelling and pre-commit: pass

Not run: the MULTIDICT_NO_FREELIST=1 ASan pass, TSan, wall-clock benchmarks.

The four entry points were copies of the same three locking arms. One
ALWAYS_INLINE helper takes the operation as a constant, so each entry
point still compiles its own copy.

md_post_update() goes out of line: inlined into update(), it took
Py_NewRef() out of the free-threaded items iterator.
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided There is a change note present in this PR label Oct 4, 2026
@codspeed

codspeed Bot commented Oct 4, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 706 untouched benchmarks


Comparing asvetlov:merge-bulk-entry-points (cd82cca) with master (cfcf7f3)

Open in CodSpeed

@asvetlov
asvetlov marked this pull request as ready for review October 4, 2026 13:41
@asvetlov
asvetlov requested a review from webknjaz as a code owner October 4, 2026 13:41
@greptile-apps

greptile-apps Bot commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 4/5

[High risk] Refactors core multidict initialization and update logic in C extension.

The missing inlining rule violates a repository requirement and must be added before merging.

Reviews (1) · Last reviewed commit: "Merge the bodies of __init__(), extend()..."

Comment thread multidict/_multilib/bulk_update.h Resolved
@asvetlov
asvetlov merged commit af7206f into aio-libs:master Oct 4, 2026
54 checks passed
@asvetlov
asvetlov deleted the merge-bulk-entry-points branch October 4, 2026 13:45
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