Skip to content

Pass the entry identity's hash to the ForEach visitor - #1561

Merged
asvetlov merged 2 commits into
aio-libs:masterfrom
asvetlov:claude/visitor-identity-hash-8d49bb
Sep 24, 2026
Merged

asvetlov merged 2 commits into
aio-libs:masterfrom
asvetlov:claude/visitor-identity-hash-8d49bb

Conversation

@asvetlov

Copy link
Copy Markdown
Member

What do these changes do?

The ForEach visitor gained the entry's identity in #1552, but not
the hash the mapping stores alongside it. A visitor that buckets or
groups entries by identity therefore had to hash it again, work both
walks had already done.

MultiDict_ItemVisitor now takes
(void *user_data, PyObject *identity, Py_hash_t hash, PyObject *key, PyObject *value), with the hash next to the identity it belongs to.
MultiDict_CyItemVisitor gains it in the same position, keeping its
own user_data-last order. The internal md_item_visitor_t in
_multilib/walk.h is widened to match, along with md_walk_all,
md_walk_with_hash and their two in-tree visitors.

md_walk_with_hash passes the hash it was already handed; every
matched entry's own entry->hash equals it, since that is the first
half of the walk's match predicate. md_walk_all reads entry->hash
before the visitor call, alongside the identity and value it already
reads there, since _md_ensure_key() and the visitor can both run
Python code that frees the entry.

The capsule is unreleased: it landed in #1480, after v6.9.1, and
MultiDict_CAPI_VERSION is still 1, so no client can have been built
against the narrower signature. That is why the typedef changes in
place instead of growing a parallel entry point at the end of the
struct with a version bump.

Are there changes in behavior for the user?

No, for Python-level users. A C or Cython client of the capsule has to
take the extra parameter, but the capsule has not shipped in a release
yet, so there is nothing to migrate.

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

No. Both walks have the hash in hand already, so the wider signature
costs neither a lookup nor a reference.

This one does not move the benchmarks. On the GIL build no
C-extension row changed at all; on the free-threaded build the three
that did move (update on CIMultiDict -0.24% and on MultiDict
+0.08%, popitem on CIMultiDict -0.02%) sit well inside the noise
floor the untouched pure-Python rows show across the same pair of
runs, which reaches 1.8%. docs/benchmark.rst is unchanged
accordingly.

Related issue number

None; this follows #1552 directly.

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt
  • 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, each in its own virtualenv against its own copy of the tree, so
a stale .so cannot make a leg lie:

CPython 3.13.12, C extension      2699 passed, 21 skipped
CPython 3.13.12, MULTIDICT_NO_EXTENSIONS=1 --no-c-extensions
                                  1258 passed, 17 skipped, 1268 deselected
CPython 3.14.7t, free-threaded    2687 passed, 33 skipped
CPython 3.10.17, oldest supported 2699 passed, 21 skipped

Every leg with a C extension built the Cython harness too and ran with
MULTIDICT_TEST_REQUIRE_CYAPI=1, so the _testcyapi half of
tests/test_capi.py is a hard failure rather than a skip.

The md_foreach harnesses now return (identity, hash, key, value),
and the tests assert the hash against hash(identity), which on a
CIMultiDict is the lower-cased form's hash and not the key's.

Lint: pre-commit run --all-files passes, including clang-format
and both mypy hooks. make doc-spelling passes.

Benchmarks: benchmarks/callgrind_driver.py, PYTHONHASHSEED=0, on
CPython 3.14.7 and 3.14.7t, base (pinned at the merge base cd92a6c)
and branch each built from a clean copy of its own tree in its own
virtualenv. Numbers in the section above.

Drafted with Claude Opus 5 in Claude Code. Left as a draft for @asvetlov to review before it goes out.

The visitor already receives the entry's identity; add the hash the
mapping stores alongside it, so a visitor bucketing or grouping
entries of its own does not have to hash the identity again.

MultiDict_ItemVisitor now takes (user_data, identity, hash, key,
value), with the hash next to the identity it belongs to.
MultiDict_CyItemVisitor gains it in the same position, keeping its
own user_data-last order. The internal md_item_visitor_t is widened
to match, along with both walks and their two in-tree visitors.

md_walk_with_hash passes the hash it was already handed, which every
matched entry's own hash equals by the walk's own predicate.
md_walk_all reads entry->hash before the visitor call, next to the
identity and value it already reads there, since _md_ensure_key() and
the visitor can both run Python code that frees the entry.

The capsule is still unreleased and MultiDict_CAPI_VERSION is still
1, so the typedef changes in place rather than growing a parallel
entry point.
@asvetlov
asvetlov marked this pull request as ready for review September 24, 2026 10:45
@asvetlov
asvetlov requested a review from webknjaz as a code owner September 24, 2026 10:45
@codspeed

codspeed Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 310 untouched benchmarks


Comparing asvetlov:claude/visitor-identity-hash-8d49bb (2d57ecd) with master (6c1c9a8)

Open in CodSpeed

@greptile-apps

greptile-apps Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

No outstanding findings block merging.

Reviews (2) · Last reviewed commit: "Update CHANGES/1561.feature.rst"

Comment thread CHANGES/1561.feature.rst Resolved
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided There is a change note present in this PR label Sep 24, 2026
@codecov

codecov Bot commented Sep 24, 2026

Copy link
Copy Markdown

Codecov Report

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

Additional details and impacted files
@@            Coverage Diff            @@
##            master     #1561   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           41        41           
  Lines         7014      7074   +60     
  Branches       610       615    +5     
=========================================
+ Hits          7014      7074   +60     
Flag Coverage Δ
CI-GHA 100.00% <100.00%> (ø)
pytest 100.00% <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 9b0cb7d into aio-libs:master Sep 24, 2026
62 checks passed
@asvetlov
asvetlov deleted the claude/visitor-identity-hash-8d49bb branch September 24, 2026 11:31
asvetlov added a commit that referenced this pull request Sep 26, 2026
<!-- Thank you for your contribution! -->

## What do these changes do?

Prepares the news fragments for 7.0.0.

The public C API (#1480) and the watchers API (#1551) are new in this
release, so the fragments describing later changes to them describe
states that never shipped. They are folded into the entries that added
the APIs: the visitor's identity and hash (#1552, #1561) into `1478`,
and the 32-watcher limit, the watcher registration race fix and the
failing-callback report (#1574, #1583, #1585) into `1551`. The
`MultiDict_SetDefault(NULL)` half of `1553` is dropped for the same
reason.

Intermediate header names that never shipped are removed as well: `1510`
no longer mentions `deferred_decref.h`, which #1530 merged into
`reflist.h`, and `1513` names `debug.h` directly, so `1576` goes.
`1572`, a fix to a tool added this cycle, is folded into `1529`. `1560`
and the last sentence of `1556` go too, since the release refresh of the
benchmark tables supersedes those intermediate ones.

Finally, the fragments that did not lead with a past-tense verb or
described the change as "now ..." are rewritten per `AGENTS.md`.

The benchmark table refresh from `RELEASE.md` step 2 is still to be
added to this branch before it leaves draft.

## 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

- [x] I think the code is well written
- [ ] Unit tests for the changes exist (N/A, changelog only)
- [x] Documentation reflects the changes
- [ ] If you provide code modification, please add yourself to
`CONTRIBUTORS.txt` (N/A)
- [ ] Add a new news fragment into the `CHANGES/` folder (N/A, edits
existing fragments; the benchmark refresh will add a `doc` fragment)
- [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); pending review by @asvetlov
before leaving draft.

<details>
<summary>Agent run details (optional, for reviewers)</summary>

`python -Im towncrier build --draft --version 7.0.0` renders cleanly.
The symlinked pairs `1512` -> `1492` and `1562` -> `1555`, and both
`1535` fragments, are intentional and left alone.
</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