GeezerVison is showing github.com — Improve performance by replacing PyTuple_Pack with PyTuple_FromArray · Issue #140009 · python/cpython
Improve performance by replacing PyTuple_Pack with PyTuple_FromArray #140009DescriptionFeature or enhancement
Proposal:
The PyTuple_Pack method is relatively slow because it is a variadic function. For various cases (in particular tuples of size 1 and 2) we can replace PyTuple_Pack with PyTuple_FromArray (introduced in #111489)
Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response
Linked PRs
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. ActivityStanFromIreland commented on Oct 12, 2025 eendebakpt commented on Oct 12, 2025 Author show commentMore actions added a commit that references this issue on Oct 13, 2025gh-140009: Improve performance of `list_extend_dictitems` by using `P… kurtmetzger commented on Oct 15, 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 eendebakpt commented on Oct 15, 2025 ContributorAuthor More actions @kurtmetzger Yes, that would be it! But I would wait with pr's for #140132 (comment). If that is accepted, the PyTuple_FromSingle would be even better. kurtmetzger commented on Oct 15, 2025 @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 commented on Oct 16, 2025 ContributorAuthor More actions
@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?
Yes. Note however that #140079 by @sergey-miryanov is there as well, which overlaps. eendebakpt commented on Feb 8, 2026 ContributorAuthor More actions 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. eendebakpt commented on May 19, 2026 ContributorAuthor More actions Closing this. The most relevant cases have been covered by #145247
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|