Skip to content

Document what istr keys are worth on a CIMultiDict - #1547

Merged
asvetlov merged 9 commits into
aio-libs:masterfrom
asvetlov:claude/istr-performance-numbers
Sep 24, 2026
Merged

asvetlov merged 9 commits into
aio-libs:masterfrom
asvetlov:claude/istr-performance-numbers

Conversation

@asvetlov

@asvetlov asvetlov commented Sep 23, 2026 •

Copy link
Copy Markdown
Member

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

  • I think the code is well written — N/A, documentation only
  • Unit tests for the changes exist — N/A
  • 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
  • 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.

On the free-threaded build every lock-free reader that brings
num_active_readers back to zero calls _md_drain_retired(), which without
a second thread is every lookup. The first thing it does is a
sequentially consistent exchange on md->retired, and that list is empty
almost every time.

Check the list with a relaxed load first and move the rest of the drain
behind a call, so a lookup that has nothing to free pays one load rather
than an out-of-line call and a read-modify-write. Nothing is stranded by
the narrower window: a table retired after the load reads the same as
one retired just after the exchange, and _md_retire() drains again once
its push is visible.

Instruction counts on CPython 3.14, free-threaded: `key in md` -10.6%,
`md[key]` -7.9%, a missing `md.get(key)` -5.0%, and -8.1%, -6.2% and
-3.3% for the CIMultiDict equivalents. No other benchmarked operation
moves beyond the measurement's noise floor. The GIL build emits
byte-identical code, confirmed with tools/codegen_diff.py.
The numbers on this page were collected before the retired-table drain
change, and before aio-libs#1533 through aio-libs#1538 landed, so the refresh moves more
cells than that change alone touches; the free-threaded read rows are
the ones it accounts for.
The page already advised creating istr keys once and reusing them, but
not how much it saves. Add the instruction counts next to the plain str
equivalents, for both builds, and note that a key which is not already
lower case saves more still.
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided There is a change note present in this PR label Sep 23, 2026
@codspeed

codspeed Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 310 untouched benchmarks


Comparing asvetlov:claude/istr-performance-numbers (743cf44) with master (cd92a6c)

Open in CodSpeed

@codecov

codecov Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (a249b3c) to head (743cf44).
⚠️ Report is 3 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##            master     #1547   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           41        41           
  Lines         6974      7014   +40     
  Branches       606       610    +4     
=========================================
+ Hits          6974      7014   +40     
Flag Coverage Δ
CI-GHA 100.00% <ø> (ø)
pytest 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 marked this pull request as ready for review September 24, 2026 06:37
@asvetlov
asvetlov requested a review from webknjaz as a code owner September 24, 2026 06:37
@greptile-apps

greptile-apps Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

No outstanding findings block merging.

Reviews (4) · Last reviewed commit: "Update docs/multidict.rst"

Comment thread docs/multidict.rst Resolved
@greptile-apps

This comment has been minimized.

Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
Comment thread docs/multidict.rst Resolved
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.
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.
Comment thread docs/multidict.rst Resolved
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
@asvetlov
asvetlov merged commit 1506c80 into aio-libs:master Sep 24, 2026
62 checks passed
@asvetlov
asvetlov deleted the claude/istr-performance-numbers branch September 24, 2026 08:44
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