Commit fc0af8f
gh-86199: Avoid an unnecessary dict copy for f(**kwargs) calls
Since Python 3.9 a call with a lone ** unpacking compiled to
BUILD_MAP 0 + DICT_MERGE 1 + CALL_FUNCTION_EX, copying the kwargs dict on
every call. For vectorcall callees (all Python functions and most
builtins) that copy is wasted work: the dict is immediately unpacked onto
a flat argument vector.
The compiler now pushes the ** operand as-is. CALL_FUNCTION_EX converts a
non-exact mapping to a dict itself (reusing the DICT_MERGE machinery, so
error messages are unchanged), and PyObject_Call copies the dict only on
the tp_call path, where the callee would otherwise receive the caller's
dict directly -- so a callee still can never mutate the caller's kwargs,
and the documented PyObject_Call/callable(*args, **kwargs) equivalence now
holds for the C API too (gh-86795).
_PyStack_UnpackDict takes a critical section while copying the dict's
items out, retrying if the dict was resized in between, which makes
unpacking a shared dict safe on free-threaded builds.
f(**d) with a small dict is ~1.4x faster; calls without ** unpacking are
unaffected.
Co-authored-by: Josh Rosenberg <1178095+MojoVampire@users.noreply.github.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>1 parent ee521e8 commit fc0af8f
9 files changed
Lines changed: 538 additions & 23 deletions
File tree
- Lib/test
- test_free_threading
- Misc/NEWS.d/next/Core_and_Builtins
- Modules/_testinternalcapi
- Objects
- Python
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
95 | 95 | | |
96 | 96 | | |
97 | 97 | | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
98 | 122 | | |
99 | 123 | | |
100 | 124 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
Lines changed: 6 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
0 commit comments