Skip to content

Take references in update() from a dict only when lower() can run code - #1598

Merged
asvetlov merged 1 commit into
aio-libs:masterfrom
asvetlov:update-dict-borrowed-refs
Sep 26, 2026
Merged

asvetlov merged 1 commit into
aio-libs:masterfrom
asvetlov:update-dict-borrowed-refs

Conversation

@asvetlov

@asvetlov asvetlov commented Sep 26, 2026 •

Copy link
Copy Markdown
Member

What do these changes do?

Since #1582, md_update_from_dict() takes its own reference to every key and value, because a str subclass key's lower() can run Python code that clears the source dict. That can only happen when the target is a CIMultiDict and the key is a str subclass other than istr. 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 the TypeError for a non-str key, moves out of _ci_str_to_identity() into a COLD helper. The ASCII exact-str path stays inline, and every CIMultiDict lookup gets a little cheaper.

Instructions per operation (Callgrind, benchmarks/callgrind_driver.py):

operation build MultiDict CIMultiDict
update GIL 39,230 -> 36,613 (-6.7%) 45,154 -> 42,661 (-5.5%)
update free-threaded 49,260 -> 44,748 (-9.2%) 55,060 -> 50,640 (-8.0%)
getitem_hit GIL 148 (flat) 209 -> 201 (-3.8%)
contains_hit GIL 134 (flat) 196 -> 188 (-4.1%)
ctor_items GIL 46,832 (flat) 59,324 -> 57,733 (-2.7%)

The pure-Python implementation holds references by construction and needs no change.

Are there changes in behavior for the user?

No, only faster update(), merge() and CIMultiDict key 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

  • I think the code is well written
  • Unit tests for the changes exist (the existing test_key_lower_mutates_source cases from Hold refs across lower() when copying keys from another source #1582 cover the path that still takes references)
  • Documentation reflects the changes (N/A, internal only)
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt (N/A)
  • 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

Drafted with Claude Code (Claude Opus 5.5); pending review by @asvetlov before leaving draft.

Agent run details (optional, for reviewers)

Tests:

  • C extension, 3.14.7: 2790 passed, 170 skipped
  • C extension, 3.14.7t: 2777 passed, 183 skipped
  • C extension, 3.10.17: 2790 passed, 170 skipped
  • Pure Python, 3.14.7 (MULTIDICT_NO_EXTENSIONS=1 --no-c-extensions): 1320 passed, 17 skipped
  • ASan + UBSan, 3.14.7 (MULTIDICT_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 skipped

Lint: pre-commit hooks pass. make doc-spelling passes.

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, dict as a flat control, gave update -5.4% GIL and -17.2% free-threaded on MultiDict, and -4.8% / -22.1% on CIMultiDict. 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.

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.
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided There is a change note present in this PR label Sep 26, 2026
@asvetlov
asvetlov marked this pull request as ready for review September 26, 2026 14:48
@asvetlov
asvetlov requested a review from webknjaz as a code owner September 26, 2026 14:48
@codspeed

codspeed Bot commented Sep 26, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 310 untouched benchmarks


Comparing asvetlov:update-dict-borrowed-refs (a350875) with master (602dedc)

Open in CodSpeed

@greptile-apps

greptile-apps Bot commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 4/5

[High risk] Refactors reference counting in dictionary update operations.

Do not merge until the news fragment satisfies the repository’s category requirement.

Reviews (1) · Last reviewed commit: "Take references in update() from a dict ..."

Comment thread CHANGES/1598.feature.rst Resolved
@asvetlov
asvetlov merged commit 39ac5df into aio-libs:master Sep 26, 2026
62 checks passed
@asvetlov
asvetlov deleted the update-dict-borrowed-refs branch September 26, 2026 15:08
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