GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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 #140009

New issue
Copy link
New issue
Copy link
Closed
Closed
Improve performance by replacing PyTuple_Pack with PyTuple_FromArray#140009
Copy link
Labels
easyinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usagePerformance or resource usagetype-featureA feature request or enhancementA feature request or enhancement

Description

@eendebakpt
eendebakpt
opened on Oct 12, 2025
Issue body actions

Feature 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

  • gh-140009: Improve performance of list_extend_dictitems by using PyTuple_FromArray #140010
  • gh-140009: Optimize dict object by replacing PyTuple_Pack with PyTuple_FromArray #144529
  • gh-140009: Optimize dict object by replacing PyTuple_Pack with PyTuple_FromArray #144531
  • gh-140009: Improve performance of datetime extension module by using PyTuple_FromArray #144532
  • gh-140009: Replace PyTuple_Pack with PyTuple_FromArray in _warnings #144760
  • gh-140009: Optimize dict.items() symmetric difference via PyTuple_FromArray #144771
  • gh-140009: Optimize JSON parsing with object_pairs_hook via PyTuple_FromArray #144772
  • gh-140009: Replace PyTuple_Pack with PyTuple_FromArray in _PyCode_ConstantKey #144773
  • gh-140009: Replace PyTuple_Pack with PyTuple_FromArray in codeobject.c and _pickle.c #144829
Pinned by eendebakpt
Pinned comment options
@eendebakpteendebakpton Feb 8, 2026

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.

View full comment

Activity

  1. eendebakpt
    added
    type-featureA feature request or enhancementA feature request or enhancement
    on Oct 12, 2025
  2. StanFromIreland
    added
    performancePerformance or resource usagePerformance or resource usage
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    on Oct 12, 2025
  3. eendebakpt
    added
    easy
    on Oct 12, 2025
  4. StanFromIreland commented on Oct 12, 2025

    @StanFromIreland
    StanFromIreland
    on Oct 12, 2025 · Hidden as resolved
    show commentMore actions
  5. eendebakpt commented on Oct 12, 2025

    @eendebakpt
    eendebakpt
    on Oct 12, 2025 · Hidden as resolved
    Author
    show commentMore actions
  6. kumaraditya303
    added a commit that references this issue on Oct 13, 2025

    gh-140009: Improve performance of `list_extend_dictitems` by using `P…

    e6aa515
  7. kurtmetzger commented on Oct 15, 2025

    @kurtmetzger
    kurtmetzger
    on Oct 15, 2025
    More actions

    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

  8. eendebakpt commented on Oct 15, 2025

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

  9. kurtmetzger commented on Oct 15, 2025

    @kurtmetzger
    kurtmetzger
    on Oct 15, 2025
    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?

  10. eendebakpt commented on Oct 16, 2025

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

  11. rashes2006
    added 3 commits that reference this issue on Feb 5, 2026

    Add NEWS entry for pythongh-140009

    da299f7

    pythongh-140009: Optimize dict object by replacing PyTuple_Pack with …

    d0f3355

    pythongh-140009: Optimize dict object by replacing PyTuple_Pack with …

    174a82b
  12. eendebakpt commented on Feb 8, 2026

    @eendebakpt
    eendebakpt
    on Feb 8, 2026 · Pinned by eendebakpt
    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.

  13. andrewloux
    added 9 commits that reference this issue on Feb 12, 2026

    pythongh-140009: Replace PyTuple_Pack with PyTuple_FromArray in _warn…

    57375b3

    pythongh-140009: Replace PyTuple_Pack with PyTuple_FromArray in _warn…

    cca09a7

    pythongh-140009: Use PyTuple_FromArray in dictitems_xor_lock_held

    714fb11

    pythongh-140009: Use PyTuple_FromArray in _json object_pairs_hook path

    92d3f1a

    pythongh-140009: Replace PyTuple_Pack with PyTuple_FromArray in _PyCo…

    166e579

    pythongh-140009: Use PyTuple_FromArray in dictitems_xor_lock_held

    451bec2

    pythongh-140009: Add news entry for json object_pairs_hook optimization

    2904c9b

    pythongh-140009: Add news entry for dict.items() symmetric difference…

    01ba9e3

    pythongh-140009: Trigger CI rerun

    290117b
  14. eendebakpt commented on May 19, 2026

    @eendebakpt
    eendebakpt
    on May 19, 2026
    ContributorAuthor
    More actions

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

  15. eendebakpt
    closed this as completedon May 19, 2026
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)(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usagePerformance or resource usagetype-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402520 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.