Skip to content

Give istr its own vectorcall - #1537

Merged
asvetlov merged 2 commits into
aio-libs:masterfrom
asvetlov:claude/istr-vectorcall
Sep 23, 2026
Merged

asvetlov merged 2 commits into
aio-libs:masterfrom
asvetlov:claude/istr-vectorcall

Conversation

@asvetlov

@asvetlov asvetlov commented Sep 23, 2026 •

Copy link
Copy Markdown
Member

What do these changes do?

istr(x) went through type_call(), which packs the argument into a
tuple, and istr_new() then unpacked it again with
PyArg_ParseTupleAndKeywords(). The one-argument form, which is how an
istr gets built in practice, needs none of that.

istr now has a tp_vectorcall that handles that form directly, and
leaves istr() and the encoding / errors signature to a cold path
that rebuilds the tuple and calls istr_new() as before. Subclasses are
unaffected: CPython does not copy tp_vectorcall to a subclass, so
class Sub(istr) still goes through type_call() and tp_new.

There is one cost, and it is not in the istr path. getall() runs nine
more instructions per call, because the added code moves where the
compiler draws its inlining line and md_calc_identity() lands out of
line in _md_get_all_locked(). The multidict side of the callgrind
profile is otherwise identical, and no attempt to place the new code
differently moved that number.

C-extension-only. The pure-Python istr is a plain str subclass with
no constructor of its own, so there is nothing to mirror there.

Are there changes in behavior for the user?

No. Every constructor form was compared against master case by case,
including the encoding and errors arguments, the keyword forms, the
error messages for bad arguments, subclasses, copy and pickle; the
output is identical.

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

No.

Related issue number

N/A

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

master was merged in after #1536 landed: that PR removed the
mod_state pointer from istrobject, which this branch's rewrite of
istr_new() had carried into _istr_finish(). The numbers above were
re-measured on the merged result.

Drafted with Claude Opus 5 via Claude Code; reviewed by @asvetlov.

Agent run details (optional, for reviewers)

Measured with callgrind, instructions retired per operation, PYTHONHASHSEED=0,
GC collected then disabled, each case run at N and 2N iterations with the totals
subtracted so startup and loop overhead cancel. Base and branch built and
measured back to back in one session, with the base at 88026da. CPython
3.14.7, GIL build, GCC 15.2.

benchmark base new delta
istr('Content-Type') 2599.9 2105.9 -494 (-19.0%)
istr('content-type') 2599.9 2105.9 -494 (-19.0%)
istr(an_istr) 1105.9 353.9 -752 (-68.0%)
CIMultiDict.getall 1359.0 1368.0 +9 (+0.7%)
CIMultiDict(dict) 4104.9 4104.9 0
CIMultiDict(dict), mixed-case keys 9077.9 9077.9 0
MultiDict(dict) 3117.9 3117.9 0
CIMultiDict() 955.9 955.9 0
MultiDict subclass from a dict 3937.9 3937.9 0
CIMultiDict subclass from a dict 4801.9 4801.9 0
CIMultiDict.__getitem__ (str key) 346.9 346.9 0
CIMultiDict.__getitem__ (istr key) 327.9 327.9 0
CIMultiDict.__getitem__ (mixed-case key) 944.9 944.9 0
MultiDict.__getitem__ 268.9 268.9 0
MultiDict.__contains__ 251.9 251.9 0

On the getall regression: a function-level callgrind diff shows the cost
moving from identity.h:_md_get_all_locked to a now separate
md_calc_identity.isra.0, which is the call overhead. NOINLINE on each of
the new helpers, and on istr_new itself, was tried; every variant still
reads 1368. Marking md_calc_identity() ALWAYS_INLINE does fix it (1351)
and speeds construction up by 3% to 4%, but costs 2.6% on
CIMultiDict.__getitem__ and 4.6% on the istr-key form, so it does not
belong here.

Tests, CPython 3.14.7:

pytest -q                                              2353 passed, 98 skipped
MULTIDICT_NO_EXTENSIONS=1 pytest -q --no-c-extensions  1132 passed, 8 skipped
pytest -q -m hypothesis                                 142 passed, 5 skipped
ASan + UBSan, PYTHONMALLOC=malloc, -k "not test_leak"  2342 passed, 98 skipped
make doc-spelling                                      build succeeded
PYENV_VERSION=3.13.2 pre-commit run --all-files        all passed

istr() went through type_call(), which packs the arguments into a tuple
and a dict, and istr_new() then unpacked them again with
PyArg_ParseTupleAndKeywords(). The one-argument form, which is how
every istr in practice gets built, needs none of that.

The vectorcall handles that form directly and leaves the encoding and
errors signature, and istr() itself, to a cold path that rebuilds the
tuple and calls istr_new() as before. Subclasses are unaffected: CPython
does not copy tp_vectorcall to a subclass, so class Sub(istr) still goes
through type_call() and tp_new.

getall() costs nine instructions more per call. The added code changes
where the compiler draws its inlining line, and md_calc_identity() lands
out of line in _md_get_all_locked(); nothing in the getall path itself
changed, and the multidict side of the callgrind profile is identical.
@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-vectorcall (1c49289) with master (88026da)

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 (88026da) to head (1c49289).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##            master     #1537   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           40        40           
  Lines         6823      6852   +29     
  Branches       596       596           
=========================================
+ Hits          6823      6852   +29     
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 marked this pull request as ready for review September 23, 2026 18:23
@asvetlov
asvetlov requested a review from webknjaz as a code owner September 23, 2026 18:23
aio-libs#1536 removed the mod_state pointer from istrobject, which this branch's
rewrite of istr_new() had carried into _istr_finish(). Dropped that store
there too; the rest of the merge is clean.
@greptile-apps

greptile-apps Bot commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

Safe to merge: the changed native constructor behavior matches the pre-change behavior across the exercised public forms.

What we checked:

  • Built the native extension before and after the change, ran the same executable regression probe against both versions, and verified that istr coverage with 32 tests and multidict coverage with 164 tests matched. T-Rex
  • The before/after logs show matching successful observable behavior with native extension locations and exit code 0; the authored executable source was uploaded verbatim and no repository sources were modified. T-Rex

Reviews (1) · Last reviewed commit: "Merge upstream master into the istr vect..."

@asvetlov
asvetlov merged commit b7f41d3 into aio-libs:master Sep 23, 2026
62 checks passed
@asvetlov
asvetlov deleted the claude/istr-vectorcall branch September 23, 2026 19:03
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