Skip to content

Give the list-pair helpers a header of their own - #1534

Merged
asvetlov merged 3 commits into
aio-libs:masterfrom
asvetlov:claude/pair-header
Sep 23, 2026
Merged

asvetlov merged 3 commits into
aio-libs:masterfrom
asvetlov:claude/pair-header

Conversation

@asvetlov

@asvetlov asvetlov commented Sep 23, 2026 •

Copy link
Copy Markdown
Member

What do these changes do?

_list_getitem_ref() and _list_item_gone() were left behind in
hashtable.h by #1533 without being about the hash table, and with no
caller there any more. They move into a new
multidict/_multilib/pair.h, along with a new unpack_pair() that both
of their users now share: itemsview.__contains__() in views.h and
_md_parse_item() in bulk_update.h each kept their own copy of the
exact-tuple and exact-list fast paths, including the free-threaded
reference dance the list branch needs.

Each caller keeps its own slow path, because the two disagree on whether
a wrong length or a foreign type is an error or a plain "no";
unpack_pair() reports those back rather than acting on them. Folding
the wrong-length case into the foreign-type one, which would drop a state
from the result, is wrong: it lets an exact list reach the general path,
which re-reads the length and then indexes without the atomic accessor.
test_items_contains_list_shrunk_by_another_thread catches it.

C extension only. There is no pure-Python counterpart to keep in sync,
since the split is a C header layout question; _multidict_py.py is
untouched.

Are there changes in behavior for the user?

No.

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

No, it removes a duplicated fast path and finishes the split #1533
started.

Related issue number

None; this is the follow-up #1533 pointed at when it left these two
helpers behind in hashtable.h.

Checklist

  • I think the code is well written
  • Unit tests for the changes exist — N/A, no new behaviour; the
    existing suite covers both call sites, including the free-threaded
    test_items_contains_list_shrunk_by_another_thread
  • Documentation reflects the changes — AGENTS.md header table
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt — N/A, no such file in this repo
  • 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)

Everything below was re-run after merging master at #1533, which moved
_md_parse_item() into bulk_update.h.

Tests:

  • C extension, CPython 3.13.2: pytest -q -> 2337 passed, 107 skipped
  • C extension, CPython 3.14.7t: pytest -q -> 2326 passed, 118 skipped
  • Pure Python, CPython 3.13.2: MULTIDICT_NO_EXTENSIONS=1 pytest -q --no-c-extensions -> 1124 passed, 17 skipped

Each leg ran in its own venv against its own copy of the tree; a shared
venv silently swaps which .so an import resolves to, and a stale .so
left in the tree makes the pure-Python leg fail to collect
tests/test_capi.py.

Lint: pre-commit run --all-files passes. make doc-spelling passes
with no additions to docs/spelling_wordlist.txt.

Codegen, tools/codegen_diff.py --ref upstream/master:

  • CPython 3.13.2: 21 of 191 functions differ
  • CPython 3.14.7t: 29 of 196 functions differ

A control run of the same tool against an unmodified tree reports 0 of
191 and 0 of 196, so those lists are real rather than build noise. Most
entries are two changed lines that turn out to be a .LCnn string
literal label renumbering, which the tool's normalization does not cover;
the rest is the inlining reshuffle its docstring warns about.
md_update_from_seq grows 822 -> 883 instructions on the GIL build.

Callgrind instruction counts over the affected operations, GIL build,
PYTHONHASHSEED=0, each measured as the difference between an N and a 2N
iteration run so that interpreter startup cancels out:

benchmark base new delta
MultiDict(pairs) 2250000 2240100 -0.44%
extend(pairs) 2314490 2304579 -0.43%
update(pairs) 2818962 2809072 -0.35%
merge(pairs) 1693390 1683562 -0.58%
tuple in items() 13230115 13240175 +0.08%
list in items() 13270143 13280003 +0.07%
items() & items() 22174564 22288011 +0.51%
items() - items() 19905148 19906440 +0.01%

An earlier draft dropped the wrong-length state from unpack_pair()'s
result, which produced a smaller codegen diff, and the free-threaded
suite rejected it with the IndexError described above.

Drafted with Claude Code (Opus 5); to be reviewed by @asvetlov before it leaves draft.

`_list_getitem_ref()` and `_list_item_gone()` sat in `hashtable.h`, which
is about the hash table, and neither has anything to do with one. They
move into a new `multidict/_multilib/pair.h`, next to the job they exist
for: reading a `(key, value)` pair out of an arbitrary object.

The two places that unpack such a pair, `itemsview.__contains__()` in
`views.h` and `_md_parse_item()` behind `extend()`, `update()` and
`merge()`, had their own copy of the exact-tuple and exact-list fast
paths, including the free-threaded reference dance the list branch needs.
Both now call one `unpack_pair()`. Their slow paths stay where they are;
the two disagree on whether a wrong length or a foreign type is an error
or a plain "no", so `unpack_pair()` reports those back instead of acting
on them. Folding the wrong-length case into the foreign-type one looks
tempting and is wrong: it lets an exact list reach the general path,
which re-reads the length and then indexes without the atomic accessor,
so `test_items_contains_list_shrunk_by_another_thread` sees an
`IndexError` where it expects a `RuntimeError`.

C extension only, no behaviour change. `tools/codegen_diff.py` reports 21
of 191 functions different on CPython 3.13.2 and 29 of 196 on 3.14.7t,
almost all of them a shifted string-literal label or the usual inlining
reshuffle; a base-to-base control run of the same tool reports 0, so the
list is real rather than compiler noise. Callgrind over the affected
operations puts every one of them within 0.6%, the largest being
`items() & items()` at +0.50%.
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided There is a change note present in this PR label Sep 23, 2026
@codspeed

codspeed Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

Merging this PR will improve performance by 13.48%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 1 improved benchmark
✅ 309 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_create_multidict_with_dict[ci-c] 100.3 µs 88.4 µs +13.48%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing asvetlov:claude/pair-header (d500aa8) with master (372d632)

Open in CodSpeed

# Conflicts:
#	multidict/_multilib/hashtable.h
@asvetlov
asvetlov marked this pull request as ready for review September 23, 2026 18:42
@asvetlov
asvetlov requested a review from webknjaz as a code owner September 23, 2026 18:42
Every `_multilib` header whose name is a bare noun defines the type that
noun names: `dict.h`, `bitmap.h`, `reflist.h`, `htkeys.h`, `istr.h`,
`update_marks.h`. `pair.h` defines no `pair_t`; it holds the routines
that read a `(key, value)` pair out of a caller-supplied object, so it
belongs with the headers named after what they do, `walk.h` for
`md_walk()` and `parser.h` for `parse2()`.

A rename and five include lines. `tools/codegen_diff.py` reports 0 of 191
functions different on CPython 3.13.2 and 0 of 196 on 3.14.7t.
@greptile-apps

greptile-apps Bot commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 4/5

Not merge-safe until the required news-fragment category is corrected.

Reviews (1) · Last reviewed commit: "Rename pair.h to unpack.h"

Comment thread CHANGES/1534.contrib.rst Resolved
@codecov

codecov Bot commented Sep 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (fdceefa) to head (d500aa8).
⚠️ Report is 3 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##            master     #1534   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           40        40           
  Lines         6820      6823    +3     
  Branches       596       596           
=========================================
+ Hits          6820      6823    +3     
Flag Coverage Δ
CI-GHA 100.00% <ø> (ø)
pytest 100.00% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@asvetlov
asvetlov merged commit 9f28f61 into aio-libs:master Sep 23, 2026
62 checks passed
@asvetlov
asvetlov deleted the claude/pair-header branch September 23, 2026 19:05
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