Repository navigation
Lay out the common branch in line in lookup and identity - #1550
Merged
asvetlov merged 2 commits intoSep 24, 2026
Merged
Conversation
Two branches in the hot path were laid out with the frequent case behind a taken branch. In the single-key lookup probe loops the DKIX_DUMMY check now carries UNLIKELY. A dummy slot only exists once something has been deleted, so a pure lookup almost never sees one. The same hint is deliberately not added to the probe loops of the deleting methods: there it costs 0.85% on __delitem__, because deleting is what creates the dummies. In _key_to_identity() the test is inverted so the exact-str case falls through. As written, GCC put it out of line behind a taken branch and gave the straight line to the str subclass call instead.
asvetlov
marked this pull request as ready for review
September 24, 2026 06:28
Contributor
|
asvetlov
added a commit
to asvetlov/multidict
that referenced
this pull request
Sep 24, 2026
Takes master's copies of hashtable.h, atomic_helpers.h and the 1546 fragment: aio-libs#1546 landed there, so the copies this branch carried are redundant and master's also has aio-libs#1539, aio-libs#1549 and aio-libs#1550 on top.
asvetlov
added a commit
to asvetlov/multidict
that referenced
this pull request
Sep 24, 2026
aio-libs#1539, aio-libs#1549 and aio-libs#1550 landed since these were collected, and the add row moved most: 876 to 765 on the GIL build, 1,035 to 939 free-threaded. Also restore the note that d.add() starts from an empty mapping and inserts 200 keys. Without it the lead-in describes every row as running against a 200-key mapping, which is true of the other two but not of that one.
4 of 6 tasks
asvetlov
added a commit
that referenced
this pull request
Sep 24, 2026
<!-- Thank you for your contribution! --> ## What do these changes do? The `istr` section already tells people to build their keys once and reuse them, but not what that buys. This adds the instruction counts, for both builds, next to the plain `str` equivalents, and points out that a key which is not already lower case saves more still, since it has to be copied into a lower-cased string whose hash is not cached. Numbers come from `benchmarks/callgrind_driver.py --include-multidict-only`, the same harness and machine as the tables in `docs/benchmark.rst`. #1546 has landed, so this is down to its own two files. The figures were re-measured against master afterwards, since #1539, #1549 and #1550 also moved them; `d.add()` shifted most, 876 to 765 on the GIL build. ## Are there changes in behavior for the user? No, documentation only. ## Is it a substantial burden for the maintainers to support this? No, though the table joins `docs/benchmark.rst` in needing a refresh when the figures move. ## Related issue number None. ## Checklist - [x] I think the code is well written — N/A, documentation only - [ ] Unit tests for the changes exist — N/A - [x] Documentation reflects the changes - [ ] If you provide code modification, please add yourself to `CONTRIBUTORS.txt` — N/A - [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 Opus 5 in Claude Code; reviewed by @asvetlov. --------- Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What do these changes do?
Two branches on the hot path were being laid out with the frequent case behind
a taken branch, so the common path jumped out of line and back.
The DKIX_DUMMY check in the four single-key lookup probe loops
(
_md_contains_locked,_md_contains_lockfree,_md_get_one_locked,_md_get_one_lockfree) now carriesUNLIKELY. A dummy slot only appears oncesomething has been deleted, so a pure lookup almost never walks over one.
_key_to_identity()has its first test inverted so the exact-strcase is thefallthrough. Left as written, GCC put that case out of line and gave the
straight line to
PyUnicode_FromObject(), which only runs for astrsubclass.
The same
UNLIKELYis deliberately not added to the probe loops of thedeleting methods. Deleting is what creates dummy slots, so those loops hit them
often; measured there it costs 0.85% on
__delitem__and 0.5% onupdate().Three other candidates were measured and rejected for the same reason: the
resize check in
_md_add_with_hash_steal_refs(), anUNLIKELYon themd_calc_identity_hash()error return, andALWAYS_INLINEonhtkeys_get_index(). Numbers for those are in the footer.This is C only. The pure-Python backend has no equivalent notion of branch
layout, so there is nothing to mirror there.
Are there changes in behavior for the user?
No. Both hunks are pure code layout; the conditions tested and the values
returned are unchanged.
Is it a substantial burden for the maintainers to support this?
No. It is five lines of annotation plus one inverted
if.Related issue number
None.
Checklist
CONTRIBUTORS.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)
Measured with callgrind on a GIL build,
PYTHONHASHSEED=0, 20 keys, base9682ba1. Instructions per operation, so it is not wall clock and does notneed a quiet machine. Every number below was reproduced in a second
independent run.
The last two rows are regressions and reproduce exactly. Both are a flat +20
instructions per call rather than a per-item cost, and both look like inlining
shifting around the +112 bytes of text this adds; the wins on the neighbouring
constructor rows are larger. Everything not listed moved by less than 0.05%.
Nothing here is near the couple-of-percent threshold that would require
regenerating
docs/benchmark.rst.Free-threaded 3.14.7t, same method:
__getitem__-0.07%,__contains__-0.05%,
getone()-0.07%.Rejected candidates, same harness:
The third is worth recording: wrapping a call in
UNLIKELY()marks the wholestatement cold, so GCC stops inlining the callee there.
multidict_mp_as_subscriptwent from havingmd_calc_identity()inlined to aplain call, and got smaller (1445 to 1307 bytes) while getting slower.
Tests, all on this branch:
The pure-Python leg ran from its own tree and its own virtualenv, so no stale
.socould be picked up by the C half of the matrix.Drafted with Claude Opus 5 in Claude Code. Left as a draft for @asvetlov to review before it goes out.