Repository navigation
Give istr its own vectorcall - #1537
Merged
Merged
Conversation
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.
Codecov Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
asvetlov
marked this pull request as ready for review
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.
Contributor
|
6 tasks done
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?
istr(x)went throughtype_call(), which packs the argument into atuple, and
istr_new()then unpacked it again withPyArg_ParseTupleAndKeywords(). The one-argument form, which is how anistrgets built in practice, needs none of that.istrnow has atp_vectorcallthat handles that form directly, andleaves
istr()and theencoding/errorssignature to a cold paththat rebuilds the tuple and calls
istr_new()as before. Subclasses areunaffected: CPython does not copy
tp_vectorcallto a subclass, soclass Sub(istr)still goes throughtype_call()andtp_new.There is one cost, and it is not in the
istrpath.getall()runs ninemore instructions per call, because the added code moves where the
compiler draws its inlining line and
md_calc_identity()lands out ofline in
_md_get_all_locked(). The multidict side of the callgrindprofile is otherwise identical, and no attempt to place the new code
differently moved that number.
C-extension-only. The pure-Python
istris a plainstrsubclass withno 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
mastercase by case,including the
encodinganderrorsarguments, the keyword forms, theerror messages for bad arguments, subclasses,
copyandpickle; theoutput is identical.
Is it a substantial burden for the maintainers to support this?
No.
Related issue number
N/A
Checklist
CONTRIBUTORS.txtCHANGES/foldermake doc-spellingpasses and any new technical words are added todocs/spelling_wordlist.txtmasterwas merged in after #1536 landed: that PR removed themod_statepointer fromistrobject, which this branch's rewrite ofistr_new()had carried into_istr_finish(). The numbers above werere-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.
istr('Content-Type')istr('content-type')istr(an_istr)CIMultiDict.getallCIMultiDict(dict)CIMultiDict(dict), mixed-case keysMultiDict(dict)CIMultiDict()MultiDictsubclass from a dictCIMultiDictsubclass from a dictCIMultiDict.__getitem__(str key)CIMultiDict.__getitem__(istr key)CIMultiDict.__getitem__(mixed-case key)MultiDict.__getitem__MultiDict.__contains__On the
getallregression: a function-level callgrind diff shows the costmoving from
identity.h:_md_get_all_lockedto a now separatemd_calc_identity.isra.0, which is the call overhead.NOINLINEon each ofthe new helpers, and on
istr_newitself, was tried; every variant stillreads 1368. Marking
md_calc_identity()ALWAYS_INLINEdoes fix it (1351)and speeds construction up by 3% to 4%, but costs 2.6% on
CIMultiDict.__getitem__and 4.6% on theistr-key form, so it does notbelong here.
Tests, CPython 3.14.7: