Skip to content

Improve performance by replacing PyTuple_Pack with PyTuple_FromArray #140009

Description

@eendebakpt
Pinned by eendebakpt

Activity

  1. StanFromIreland commented on Oct 12, 2025

    @StanFromIreland
  2. eendebakpt commented on Oct 12, 2025

    @eendebakpt
    Author
  3. added a commit that references this issue on Oct 13, 2025
  4. kurtmetzger commented on Oct 15, 2025

    @kurtmetzger

    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

  5. eendebakpt commented on Oct 15, 2025

    @eendebakpt
    ContributorAuthor

    @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.

  6. kurtmetzger commented on Oct 15, 2025

    @kurtmetzger

    @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?

  7. eendebakpt commented on Oct 16, 2025

    @eendebakpt
    ContributorAuthor

    @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.

  8. added 3 commits that reference this issue on Feb 5, 2026
  9. eendebakpt commented on Feb 8, 2026

    @eendebakpt
    ContributorAuthor

    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.

  10. added 9 commits that reference this issue on Feb 12, 2026
  11. eendebakpt commented on May 19, 2026

    @eendebakpt
    ContributorAuthor

    Closing this. The most relevant cases have been covered by #145247

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    easyinterpreter-core(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usagetype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions