Repository navigation
Take references in update() from a dict only when lower() can run code - #1598
Merged
Merged
Conversation
A str subclass key's lower() can clear the source dict, so the loop held its own reference to every key and value. Only that case needs them: keep the borrowed references otherwise, and move the lower() call into a cold helper.
asvetlov
marked this pull request as ready for review
September 26, 2026 14:48
Contributor
|
5 of 6 tasks
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?
Since #1582,
md_update_from_dict()takes its own reference to every key and value, because astrsubclass key'slower()can run Python code that clears the source dict. That can only happen when the target is aCIMultiDictand the key is astrsubclass other thanistr. Every other key goes through code that runs no Python, so the dict keeps both alive and the borrowed references are enough. The loop now takes references only in that one case;extend()still takes them, since it moves them into the table.The
lower()call itself, together with theTypeErrorfor a non-strkey, moves out of_ci_str_to_identity()into aCOLDhelper. The ASCII exact-strpath stays inline, and everyCIMultiDictlookup gets a little cheaper.Instructions per operation (Callgrind,
benchmarks/callgrind_driver.py):MultiDictCIMultiDictupdateupdategetitem_hitcontains_hitctor_itemsThe pure-Python implementation holds references by construction and needs no change.
Are there changes in behavior for the user?
No, only faster
update(),merge()andCIMultiDictkey lookups.Is it a substantial burden for the maintainers to support this?
No. The condition is one line, next to the comment explaining it.
Related issue number
N/A
Checklist
test_key_lower_mutates_sourcecases from Hold refs across lower() when copying keys from another source #1582 cover the path that still takes references)CONTRIBUTORS.txt(N/A)CHANGES/foldermake doc-spellingpasses and any new technical words are added todocs/spelling_wordlist.txtDrafted with Claude Code (Claude Opus 5.5); pending review by @asvetlov before leaving draft.
Agent run details (optional, for reviewers)
Tests:
MULTIDICT_NO_EXTENSIONS=1 --no-c-extensions): 1320 passed, 17 skippedMULTIDICT_DEBUG_BUILD=1 MULTIDICT_ASAN_BUILD=1 MULTIDICT_NO_FREELIST=1,PYTHONMALLOC=malloc,-k "not test_leak and not test_freed_blocks_are_reused"): 2778 passed, 170 skippedLint: pre-commit hooks pass.
make doc-spellingpasses.Callgrind rows not listed moved less than 1%. Wall clock was measured only together with two sibling PRs (the version counter and the inlining annotations): an interleaved pyperf A/B, 3 rounds, pinned P-cores,
dictas a flat control, gaveupdate-5.4% GIL and -17.2% free-threaded onMultiDict, and -4.8% / -22.1% onCIMultiDict. That run also included a refcounting change since dropped from #1600, which accounts for part of the free-threaded gain.Not tested: musl, macOS, Windows and Android builds.