Repository navigation
gh-163213: Improve performance of keyword arguments collected by **kwargs - #163215
Open
eendebakpt wants to merge 2 commits into
Open
eendebakpt wants to merge 2 commits into
eendebakpt wants to merge 2 commits into
Conversation
When a keyword does not match a parameter name by identity, initialize_locals() compares it by value with every parameter name before it is added to **kwargs. Parameter names are interned, so an interned keyword that was not found by identity cannot match: skip that loop. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Matching non-interned and str subclass keywords by value is already exercised by the existing tests (and by dataclasses at import time). Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
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.
A keyword that does not name a parameter is first looked up among the parameter names by identity and then, as a fallback, compared with each of them by value before it goes into
**kwargs. Parameter names are always interned (intern_strings()in_PyCode_New()), so an interned keyword that was not found by identity cannot match, and the fallback loop can be skipped. Keywords that are not interned, including instances ofstrsubclasses, are compared as before.This speeds up calls that pass extra keywords to a function with named parameters and
**kwargs. Withthe timings are:
f(1, 2, x=1, y=2, z=3)f(1, 2, **d6)f(1, 2, **d1)o.m(x=1, y=2, z=3)o.m(**d6)f(1, 2, c=3)f(1, 2)In the last two rows no keyword reaches the changed code, so those rows are controls (improvement is noise or binary layout changes). In pyperformance,
sympy_expandis 3.5% faster. sympy passes six expansion hints through**hintsto a function with three named parameters.Benchmark script
Generated with with from Claude Code