From 26bceb48eb9880279ac8dce08726b203127dcf61 Mon Sep 17 00:00:00 2001 From: Cody Maloney Date: Thu, 3 Dec 2026 19:50:58 -0800 Subject: [PATCH 1/3] gh-95757: Speedup CALL_FUNCTION_EX for functions without named 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 --- ...6-12-03-20-05-38.gh-issue-95757.4WHmNY.rst | 2 ++ Python/ceval.c | 29 ++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-12-03-20-05-38.gh-issue-95757.4WHmNY.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-12-03-20-05-38.gh-issue-95757.4WHmNY.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-12-03-20-05-38.gh-issue-95757.4WHmNY.rst new file mode 100644 index 00000000000000..e8d5771039bfcf --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-12-03-20-05-38.gh-issue-95757.4WHmNY.rst @@ -0,0 +1,2 @@ +Speed up calls that unpack arguments with ``*`` or ``**`` into Python +functions that have no named parameters, such as ``def f(*args, **kwargs)``. diff --git a/Python/ceval.c b/Python/ceval.c index 446e6a8be4da91..4555a4635253e0 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -2126,7 +2126,21 @@ _PyInterpreterFrame * _PyEvalFramePushAndInit_Ex(PyThreadState *tstate, _PyStackRef func, PyObject *locals, Py_ssize_t nargs, PyObject *callargs, PyObject *kwargs, _PyInterpreterFrame *previous) { + PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE( + PyStackRef_AsPyObjectBorrow(func)); bool has_dict = (kwargs != NULL && PyDict_GET_SIZE(kwargs) > 0); + /* Without named parameters callargs and kwargs already are *args and + **kwargs */ + bool direct = (co->co_argcount + co->co_kwonlyargcount == 0 + && (nargs == 0 || (co->co_flags & CO_VARARGS)) + && (kwargs == NULL + || ((co->co_flags & CO_VARKEYWORDS) + && _PyObject_IsUniquelyReferenced(kwargs) + && _PyDict_HasOnlyStringKeys(kwargs)))); + if (direct) { + nargs = 0; + has_dict = false; + } PyObject *kwnames = NULL; _PyStackRef *newargs; PyObject *const *object_array = NULL; @@ -2179,7 +2193,20 @@ _PyEvalFramePushAndInit_Ex(PyThreadState *tstate, _PyStackRef func, /* No need to decref func here because the reference has been stolen by _PyEvalFramePushAndInit. */ - Py_DECREF(callargs); + if (direct && new_frame != NULL) { + _PyStackRef *local = new_frame->localsplus; + if (co->co_flags & CO_VARARGS) { + PyStackRef_CLOSE(*local); + *local++ = PyStackRef_FromPyObjectSteal(callargs); + callargs = NULL; + } + if (kwargs != NULL) { + PyStackRef_CLOSE(*local); + *local = PyStackRef_FromPyObjectSteal(kwargs); + kwargs = NULL; + } + } + Py_XDECREF(callargs); Py_XDECREF(kwargs); return new_frame; error: From 119e8dfcad5f5496807636b87f9c47ac7d61f459 Mon Sep 17 00:00:00 2001 From: Cody Maloney Date: Thu, 3 Dec 2026 20:27:06 -0800 Subject: [PATCH 2/3] Pass *args and **kwargs through separately 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 --- Python/ceval.c | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/Python/ceval.c b/Python/ceval.c index 4555a4635253e0..07d1ac94158818 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -2129,16 +2129,19 @@ _PyEvalFramePushAndInit_Ex(PyThreadState *tstate, _PyStackRef func, PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE( PyStackRef_AsPyObjectBorrow(func)); bool has_dict = (kwargs != NULL && PyDict_GET_SIZE(kwargs) > 0); - /* Without named parameters callargs and kwargs already are *args and - **kwargs */ - bool direct = (co->co_argcount + co->co_kwonlyargcount == 0 - && (nargs == 0 || (co->co_flags & CO_VARARGS)) - && (kwargs == NULL - || ((co->co_flags & CO_VARKEYWORDS) - && _PyObject_IsUniquelyReferenced(kwargs) - && _PyDict_HasOnlyStringKeys(kwargs)))); - if (direct) { + /* callargs already is *args if there are no positional parameters */ + bool pass_args = co->co_argcount == 0 && (co->co_flags & CO_VARARGS); + /* kwargs already is **kwargs if no parameter can be passed by keyword */ + bool pass_kwargs = (has_dict + && (co->co_flags & CO_VARKEYWORDS) + && co->co_argcount == co->co_posonlyargcount + && co->co_kwonlyargcount == 0 + && _PyObject_IsUniquelyReferenced(kwargs) + && _PyDict_HasOnlyStringKeys(kwargs)); + if (pass_args) { nargs = 0; + } + if (pass_kwargs) { has_dict = false; } PyObject *kwnames = NULL; @@ -2193,14 +2196,16 @@ _PyEvalFramePushAndInit_Ex(PyThreadState *tstate, _PyStackRef func, /* No need to decref func here because the reference has been stolen by _PyEvalFramePushAndInit. */ - if (direct && new_frame != NULL) { - _PyStackRef *local = new_frame->localsplus; - if (co->co_flags & CO_VARARGS) { + if (new_frame != NULL) { + _PyStackRef *local = &new_frame->localsplus[co->co_argcount + + co->co_kwonlyargcount]; + if (pass_args) { PyStackRef_CLOSE(*local); - *local++ = PyStackRef_FromPyObjectSteal(callargs); + *local = PyStackRef_FromPyObjectSteal(callargs); callargs = NULL; } - if (kwargs != NULL) { + if (pass_kwargs) { + local += (co->co_flags & CO_VARARGS) != 0; PyStackRef_CLOSE(*local); *local = PyStackRef_FromPyObjectSteal(kwargs); kwargs = NULL; From 07f75da35dc356bf600dae92ab074d7d5c234fea Mon Sep 17 00:00:00 2001 From: Cody Maloney Date: Thu, 3 Dec 2026 20:59:42 -0800 Subject: [PATCH 3/3] Only touch the new frame when passing something through Co-Authored-By: Claude Opus 5.5 --- Python/ceval.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/ceval.c b/Python/ceval.c index 07d1ac94158818..f319f9d35ef746 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -2196,7 +2196,7 @@ _PyEvalFramePushAndInit_Ex(PyThreadState *tstate, _PyStackRef func, /* No need to decref func here because the reference has been stolen by _PyEvalFramePushAndInit. */ - if (new_frame != NULL) { + if ((pass_args || pass_kwargs) && new_frame != NULL) { _PyStackRef *local = &new_frame->localsplus[co->co_argcount + co->co_kwonlyargcount]; if (pass_args) {