You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
gh-140009: Replace PyTuple_Pack with PyTuple_FromArray
Summary
This PR replaces PyTuple_Pack calls with PyTuple_FromArray using stack-allocated arrays in two files:
Objects/codeobject.c: 11 replacements in _PyCode_ConstantKey() — called for every constant in every code object during compilation (handles bool, bytes, float, complex, tuple, frozenset, slice, and fallback branches)
Modules/_pickle.c: 3 replacements in fix_imports(), save_global(), and find_class() — dictionary key construction for protocol compatibility mappings
Motivation
PyTuple_FromArray avoids the variadic argument overhead of PyTuple_Pack by taking a C array directly. This follows the pattern established in PR #140010 (merged for Objects/listobject.c).
Note: the remaining cases of PyTuple_Pack(1, ...) and PyTuple_Pack(2, ...) are for the most part not performance critical. If opening a PR, please create proper benchmarks (using pyperf, hyperfine or some other benchmarking package) showing the performance gain for the change.
Since many of your changes deal with PyTuple_Pack(2, ...), I suggest that you include benchmarks to see if the changes are worthwhile.
The reason will be displayed to describe this comment to others. Learn more.
This is probably not necessary since nothing really changes from the user's perspective. Just mention that performance improved, similar to the NEWS entry for #140010
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
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.
gh-140009: Replace
PyTuple_PackwithPyTuple_FromArraySummary
This PR replaces
PyTuple_Packcalls withPyTuple_FromArrayusing stack-allocated arrays in two files:Objects/codeobject.c: 11 replacements in_PyCode_ConstantKey()— called for every constant in every code object during compilation (handles bool, bytes, float, complex, tuple, frozenset, slice, and fallback branches)Modules/_pickle.c: 3 replacements infix_imports(),save_global(), andfind_class()— dictionary key construction for protocol compatibility mappingsMotivation
PyTuple_FromArrayavoids the variadic argument overhead ofPyTuple_Packby taking a C array directly. This follows the pattern established in PR #140010 (merged forObjects/listobject.c).Pattern
Note:
Py_TYPE(op)returnsPyTypeObject *, so an explicit(PyObject *)cast is needed in the array initializers forcodeobject.c.Testing
test_code,test_compile,test_peepholer,test_symtable,test_codecs,test_marshal(743 tests)test_pickle,test_pickletools(1,157 tests)- Issue: Improve performance by replacing PyTuple_Pack with PyTuple_FromArray #140009
"