Repository navigation
Improve performance by replacing PyTuple_Pack with PyTuple_FromArray #140009
Description
Activity
- addedtype-featureA feature request or enhancementA feature request or enhancement
on Oct 12, 2025 - addedperformancePerformance or resource usagePerformance or resource usageinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
on Oct 12, 2025 StanFromIreland commented
on Oct 12, 2025 on Oct 12, 2025 · Hidden as resolvedshow commentMore actionseendebakpt commented
on Oct 12, 2025 on Oct 12, 2025 · Hidden as resolvedAuthorshow commentMore actions- added a commit that references this issue
on Oct 13, 2025 If I were to work on this, is it basically changing instances that look like this:
result = PyTuple_Pack(1, basestate);to this?
PyObject *items[1] = {basestate}; result = PyTuple_FromArray(items, 1);for all instances where the array would only be 1 or two items?
Thanks@kurtmetzger Yes, that would be it! But I would wait with pr's for #140132 (comment). If that is accepted, the
PyTuple_FromSinglewould be even better.@eendebakpt I see, so if the other gets accepted, it creates custom functions for arrays of lengths 1 and 2 that should be used like so:
PyTuple_MakePair(a, b)
or
PyTuple_MakeSingle(a)
to replace the current PyTuple_Pack calls?@eendebakpt I see, so if the other gets accepted, it creates custom functions for arrays of lengths 1 and 2 that should be used like so:
PyTuple_MakePair(a, b)orPyTuple_MakeSingle(a)to replace the current PyTuple_Pack calls?Yes. Note however that #140079 by @sergey-miryanov is there as well, which overlaps.
eendebakpt commented
on Feb 8, 2026 on Feb 8, 2026 · Pinned by eendebakptContributorAuthorMore actionsNote: the remaining cases of
PyTuple_Pack(1, ...)andPyTuple_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.- added 9 commits that reference this issue
on Feb 12, 2026 Closing this. The most relevant cases have been covered by #145247
Feature or enhancement
Proposal:
The
PyTuple_Packmethod is relatively slow because it is a variadic function. For various cases (in particular tuples of size 1 and 2) we can replacePyTuple_PackwithPyTuple_FromArray(introduced in #111489)Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response
Linked PRs
dict.items()symmetric difference viaPyTuple_FromArray#144771object_pairs_hookviaPyTuple_FromArray#144772Note: the remaining cases of
PyTuple_Pack(1, ...)andPyTuple_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.