Skip to content

Speed sorting tuples whose first elements have the same type #135043

Not planned
@tim-one

Description

@tim-one

Feature or enhancement

Proposal:

As listobject.c says,

/* The idea is that most tuple compares don't involve x[1:]. */

in the comment before unsafe_tuple_compare().

But the code nevertheless copies the tuple object's all-purpose comparison function, by calling PyObject_RichCompareBool(... Py_EQ) on both tuples' elements first, in a loop, to find the first position where the tuples differ. It only benefits if the result of that loop is "OK, the first difference is at index 0".

But the comment is right in its intent: most times comparing the 0th elements on their own would resolve the question.. v[0] < w[0] on its own implies v < w; if not, w[0] < v[0] implies w < v, so again we could get out early ("no, v is not less than w"). And if not that either, only then do we need the greater expense of the loop the code now starts with, although we can start at index 1 instead of 0 (we've already established that the 0th elements are equal).

Sorting tuples is common (all kinds of "decorate" patterns do this), and I expect this would pay handsomely.

I'll write the code if nobody else beats me to it, but I think this is a high bang-for-the-buck change well suited to an aspiring core dev. The change is confined to a single small function, and while delicate is not truly difficult.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Activity

added
type-featureA feature request or enhancement
performancePerformance or resource usage
and removed
type-featureA feature request or enhancement
on Jun 2, 2025
pochmann3

pochmann3 commented on Jun 2, 2025

@pochmann3
Contributor

Does this differ from this previous attempt which caused trouble and got reverted?

tim-one

tim-one commented on Jun 2, 2025

@tim-one
MemberAuthor

Good catch! I had forgotten that we've been in this pit before. I don't have the will or energy remaining to resume the fight, so I'll just close this. Thanks!

tim-one

tim-one commented on Jun 2, 2025

@tim-one
MemberAuthor

Closed.

reopened this on Jun 2, 2025
reopened this on Jun 2, 2025
reopened this on Jun 2, 2025

7 remaining items

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

    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

      Speed sorting tuples whose first elements have the same type · Issue #135043 · python/cpython

      Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

      Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant