Skip to content

gh-163213: Improve performance of keyword arguments collected by **kwargs - #163215

Open
eendebakpt wants to merge 2 commits into
python:mainfrom
eendebakpt:initialize-locals-interned-kw
Open

eendebakpt wants to merge 2 commits into
python:mainfrom
eendebakpt:initialize-locals-interned-kw

Conversation

@eendebakpt

@eendebakpt eendebakpt commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

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 of str subclasses, are compared as before.

This speeds up calls that pass extra keywords to a function with named parameters and **kwargs. With

def f(a, b, c=0, **kwargs):
    pass

class C:
    def m(self, **kwargs):
        pass

o = C()
d1 = {'x': 1}
d6 = dict.fromkeys('pqrstu', 1)

the timings are:

Benchmark main this PR
f(1, 2, x=1, y=2, z=3) 274 ns 180 ns: 1.53x faster
f(1, 2, **d6) 576 ns 396 ns: 1.46x faster
f(1, 2, **d1) 270 ns 216 ns: 1.25x faster
o.m(x=1, y=2, z=3) 190 ns 163 ns: 1.17x faster
o.m(**d6) 515 ns 474 ns: 1.09x faster
f(1, 2, c=3) 122 ns 110 ns: 1.11x faster
f(1, 2) 86.7 ns 86.2 ns: 1.01x faster

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_expand is 3.5% faster. sympy passes six expansion hints through **hints to a function with three named parameters.

Benchmark script
import pyperf

runner = pyperf.Runner()
setup = """
def f(a, b, c=0, **kwargs):
    pass

class C:
    def m(self, **kwargs):
        pass

o = C()
d1 = {'x': 1}
d6 = dict.fromkeys('pqrstu', 1)
"""
for stmt in [
    "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)",
]:
    runner.timeit(stmt, stmt, setup)

Generated with with from Claude Code

eendebakpt and others added 2 commits December 5, 2026 18:44
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant