Skip to content

gh-95757: Speedup CALL_FUNCTION_EX for functions without named parameters - #163127

Open
cmaloney wants to merge 3 commits into
python:mainfrom
cmaloney:landable/call-ex-direct
Open

cmaloney wants to merge 3 commits into
python:mainfrom
cmaloney:landable/call-ex-direct

Conversation

@cmaloney

@cmaloney cmaloney commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

CALL_FUNCTION_EX unpacks the argument tuple and dict, then packs them again into a new tuple and dict for *args and **kwargs. Skip that when the result would be the same:

  • the tuple is reused as *args when the function has no positional parameters,
  • the dict is reused as **kwargs when no parameter can be passed by keyword, nothing else refers to the dict, and its keys are all strings.

This covers functions like def f(*args, **kwargs), def f(*args, flag=False) and def f(x, /, **kw). Other calls take the existing path.

One visible change: args is t can now be true for f(*t).

GIL build:

Benchmark main PR Change
a(**d20) 342 ns 79.7 ns -262 ns 4.29x faster
po(1, **d5) 112 ns 62.7 ns -49.3 ns 1.79x faster
a(*t, **d1) 81.0 ns 57.6 ns -23.4 ns 1.41x faster
c(*t) 26.6 ns 20.8 ns -5.8 ns 1.28x faster
kwo(*t) 30.7 ns 25.2 ns -5.5 ns 1.22x faster
fwd(*t) 66.2 ns 55.9 ns -10.3 ns 1.18x faster
deco(*t) 58.7 ns 54.5 ns -4.2 ns 1.08x faster
kwo(*t, **{'flag': True}) 82.4 ns 78.4 ns -4.0 ns 1.05x faster
e(1) 17.5 ns 17.1 ns -0.4 ns 1.03x faster
b(1, **d5) 146 ns 148 ns +2 ns 1.01x slower
b(**xy) 82.0 ns 84.1 ns +2.1 ns 1.03x slower
deco(1, y=2) 96.5 ns 99.4 ns +2.9 ns 1.03x slower
e(**xy) 60.4 ns 63.1 ns +2.7 ns 1.04x slower

Free-threaded build:

Benchmark main PR Change
a(**d20) 397 ns 85.9 ns -311 ns 4.61x faster
po(1, **d5) 126 ns 60.5 ns -65.5 ns 2.08x faster
a(*t, **d1) 85.8 ns 55.4 ns -30.4 ns 1.55x faster
c(*t) 28.5 ns 21.4 ns -7.1 ns 1.33x faster
kwo(*t) 32.9 ns 25.7 ns -7.2 ns 1.28x faster
fwd(*t) 76.8 ns 62.0 ns -14.8 ns 1.24x faster
deco(*t) 66.6 ns 60.6 ns -6.0 ns 1.10x faster
kwo(*t, **{'flag': True}) 88.1 ns 81.0 ns -7.1 ns 1.09x faster
deco(1, y=2) 103 ns 107 ns +4 ns 1.03x slower
b(**xy) 87.4 ns 91.1 ns +3.7 ns 1.04x slower
b(1, **d5) 156 ns 163 ns +7 ns 1.04x slower
e(**xy) 65.4 ns 68.6 ns +3.2 ns 1.05x slower

e(1) was not significantly different. The slower calls take neither shortcut; under callgrind e(**xy) and deco(1, y=2) run 3 to 4 more instructions per call than without this change, so most of the gap is code layout.

Linux x86-64, clang PGO+LTO, tail-call interpreter, pyperf:

import pyperf

runner = pyperf.Runner()
setup = """
def a(*args, **kw):
    pass

def b(x, *args, **kw):
    pass

def c(*args):
    pass

def e(x, y=0):
    pass

def kwo(*args, flag=False):
    pass

def po(x, /, **kw):
    pass

def fwd(*args, **kw):
    return c(*args, **kw)

def inner(x, y=0):
    return x

def deco(*args, **kw):
    return inner(*args, **kw)

t = (1, 2)
d1 = {'p': 1}
d5 = dict.fromkeys('pqrst', 1)
d20 = {f'k{i}': i for i in range(20)}
xy = {'x': 1, 'y': 2}
"""
for stmt in [
    "a(*t, **d1)",
    "a(**d20)",
    "b(1, **d5)",
    "b(**xy)",
    "c(*t)",
    "e(**xy)",
    "deco(1, y=2)",
    "deco(*t)",
    "kwo(*t)",
    "kwo(*t, **{'flag': True})",
    "po(1, **d5)",
    "fwd(*t)",
    "e(1)",
]:
    runner.timeit(stmt, stmt, setup)

cc: @eendebakpt this I think accompanies GH-156384. Both make some cases faster

edit: Updated so that *args and **kwargs can each be forwarded independently (So something effecting one or the other only removes some of the shortcut). Adds slightly to runtime generally but makes more cases able to be sped up.

…parameters

`CALL_FUNCTION_EX` would unpack the argument tuple and dict then re-pack
it into a new tuple and dict. A function with no named parameters needs
exactly the tuple and dict it was called with, avoid the unnecessary
work. Other calls take the existing path.

This makes one visible change in that `args is t` can now be true
for `f(*t)`.

Full performance details below, some cases significantly faster and a
little slightly slower although in the noise on my machine.

GIL build, two builds per side pooled:

| Benchmark      | main    | PR      | Change   |                |
|----------------|:-------:|:-------:|:--------:|----------------|
| `a(**d20)`     | 339 ns  | 80.0 ns | -259 ns  | 4.24x faster   |
| `a(*t, **d5)`  | 116 ns  | 59.2 ns | -56.8 ns | 1.97x faster   |
| `a(*t, **d1)`  | 80.5 ns | 55.1 ns | -25.4 ns | 1.46x faster   |
| `c(*t)`        | 25.9 ns | 19.2 ns | -6.7 ns  | 1.35x faster   |
| `deco(*t)`     | 59.9 ns | 52.9 ns | -7.0 ns  | 1.13x faster   |
| `e(1)`         | 16.7 ns | 16.3 ns | -0.4 ns  | 1.02x faster   |
| `b(1, **d1)`   | 80.2 ns | 81.0 ns | +0.8 ns  | 1.01x slower   |
| `b(1, **d5)`   | 143 ns  | 145 ns  | +2 ns    | 1.01x slower   |
| `b(**xy)`      | 81.1 ns | 82.1 ns | +1.0 ns  | 1.01x slower   |
| `a(1, p=1)`    | 40.7 ns | 41.6 ns | +0.9 ns  | 1.02x slower   |
| `deco(1, y=2)` | 96.2 ns | 98.7 ns | +2.5 ns  | 1.03x slower   |

`e(**xy)` was not significantly different. Under callgrind `b(1, **d1)`, `b(**xy)` and `deco(1, y=2)` run 8 to 9 more instructions, mostly the added check; `b(1, **d5)` and `a(1, p=1)` run no more than main.

Free-threaded build, one build per side:

| Benchmark      | main    | PR      | Change   |              |
|----------------|:-------:|:-------:|:--------:|--------------|
| `a(**d20)`     | 388 ns  | 86.1 ns | -302 ns  | 4.50x faster |
| `a(*t, **d5)`  | 133 ns  | 58.2 ns | -74.8 ns | 2.28x faster |
| `a(*t, **d1)`  | 86.0 ns | 54.4 ns | -31.6 ns | 1.58x faster |
| `c(*t)`        | 27.6 ns | 20.7 ns | -6.9 ns  | 1.33x faster |
| `deco(*t)`     | 68.0 ns | 59.4 ns | -8.6 ns  | 1.14x faster |
| `deco(1, y=2)` | 105 ns  | 102 ns  | -3 ns    | 1.03x faster |
| `a(1, p=1)`    | 42.7 ns | 42.3 ns | -0.4 ns  | 1.01x faster |

The other five were not significantly different.

Linux x86-64, clang PGO+LTO, tail-call interpreter, pyperf:

```python
import pyperf

runner = pyperf.Runner()
setup = """
def a(*args, **kw):
    pass

def b(x, *args, **kw):
    pass

def c(*args):
    pass

def e(x, y=0):
    pass

def inner(x, y=0):
    return x

def deco(*args, **kw):
    return inner(*args, **kw)

t = (1, 2)
d1 = {'p': 1}
d5 = dict.fromkeys('pqrst', 1)
d20 = {f'k{i}': i for i in range(20)}
xy = {'x': 1, 'y': 2}
"""
for stmt in [
    "a(*t, **d1)",
    "a(*t, **d5)",
    "a(**d20)",
    "b(1, **d1)",
    "b(1, **d5)",
    "b(**xy)",
    "c(*t)",
    "e(**xy)",
    "deco(1, y=2)",
    "deco(*t)",
    "a(1, p=1)",
    "e(1)",
]:
    runner.timeit(stmt, stmt, setup)
```

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
cmaloney and others added 2 commits December 3, 2026 20:27
Reuse the tuple whenever there are no positional parameters, and the dict
whenever no parameter can be passed by keyword. This also covers functions
like `def f(*args, flag=False)` and `def f(x, /, **kw)`, and forwarding an
empty `**kw` to a function without `**kwargs`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant