Repository navigation
Give the list-pair helpers a header of their own - #1534
Conversation
`_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%.
Merging this PR will improve performance by 13.48%
|
| 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)
# Conflicts: # multidict/_multilib/hashtable.h
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.
|
Codecov Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
What do these changes do?
_list_getitem_ref()and_list_item_gone()were left behind inhashtable.hby #1533 without being about the hash table, and with nocaller there any more. They move into a new
multidict/_multilib/pair.h, along with a newunpack_pair()that bothof their users now share:
itemsview.__contains__()inviews.hand_md_parse_item()inbulk_update.heach kept their own copy of theexact-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. Foldingthe 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_threadcatches 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.pyisuntouched.
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
existing suite covers both call sites, including the free-threaded
test_items_contains_list_shrunk_by_another_threadAGENTS.mdheader tableCONTRIBUTORS.txt— N/A, no such file in this repoCHANGES/foldermake doc-spellingpasses and any new technical words are added todocs/spelling_wordlist.txtAgent run details (optional, for reviewers)
Everything below was re-run after merging
masterat #1533, which moved_md_parse_item()intobulk_update.h.Tests:
pytest -q-> 2337 passed, 107 skippedpytest -q-> 2326 passed, 118 skippedMULTIDICT_NO_EXTENSIONS=1 pytest -q --no-c-extensions-> 1124 passed, 17 skippedEach leg ran in its own venv against its own copy of the tree; a shared
venv silently swaps which
.soan import resolves to, and a stale.soleft in the tree makes the pure-Python leg fail to collect
tests/test_capi.py.Lint:
pre-commit run --all-filespasses.make doc-spellingpasseswith no additions to
docs/spelling_wordlist.txt.Codegen,
tools/codegen_diff.py --ref upstream/master: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
.LCnnstringliteral label renumbering, which the tool's normalization does not cover;
the rest is the inlining reshuffle its docstring warns about.
md_update_from_seqgrows 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 2Niteration run so that interpreter startup cancels out:
MultiDict(pairs)extend(pairs)update(pairs)merge(pairs)tuple in items()list in items()items() & items()items() - items()An earlier draft dropped the wrong-length state from
unpack_pair()'sresult, which produced a smaller codegen diff, and the free-threaded
suite rejected it with the
IndexErrordescribed above.Drafted with Claude Code (Opus 5); to be reviewed by @asvetlov before it leaves draft.