gh-86199: Avoid an unnecessary dict copy for f(**kwargs) calls - #156384
Open
eendebakpt wants to merge 1 commit into
Open
gh-86199: Avoid an unnecessary dict copy for f(**kwargs) calls#156384eendebakpt wants to merge 1 commit into
eendebakpt wants to merge 1 commit into
Conversation
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 (pythongh-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>
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.
This PR addresses #86199 (performance regression in
f(**kw)) and #86795 (incorrect behavior ofPyObject_Callleading to crashes). The work is based on gh-92192 by @MojoVampire, which went stale.f(**d)with a small dict is ~1.4x faster in microbenchmarks.PyObject_Callnow honors its documented equivalence tocallable(*args, **kwargs): atp_callcallee can no longer mutate thecaller's kwargs dict.
It took some iterations before ending up at the current PR. The main optimization is due to the compiler pushing the
**operand as-is instead ofBUILD_MAP/DICT_MERGE.CALL_FUNCTION_EXconverts a non-exact mapping to an exact dict itself, reusing theDICT_MERGEmachinery so error messages are unchanged.PyObject_Callcopies the dict only on thetp_callpath, where the callee would otherwise receive the caller's dict directly. For the vectorcall protocoll the_PyStack_UnpackDicttakes a critical section while copying the dict's items out (retrying on a concurrent resize).