Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tim-one opened this issue Jun 2, 2025 · 7 comments
Closed

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

tim-one opened this issue Jun 2, 2025 · 7 comments
Labels
performance Performance or resource usage type-feature A feature request or enhancement

Comments

@tim-one
Copy link
Member

tim-one commented Jun 2, 2025

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

@tim-one tim-one added type-feature A feature request or enhancement performance Performance or resource usage and removed type-feature A feature request or enhancement labels Jun 2, 2025
@pochmann3
Copy link
Contributor

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

@tim-one
Copy link
Member Author

tim-one commented Jun 2, 2025

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
Copy link
Member Author

tim-one commented Jun 2, 2025

Closed.

@tim-one tim-one closed this as completed Jun 2, 2025
@tim-one tim-one closed this as not planned Won't fix, can't repro, duplicate, stale Jun 2, 2025
@tim-one tim-one reopened this Jun 2, 2025
@tim-one tim-one closed this as completed Jun 2, 2025
@tim-one tim-one reopened this Jun 2, 2025
@tim-one tim-one closed this as completed Jun 2, 2025
@tim-one tim-one reopened this Jun 2, 2025
@tim-one tim-one closed this as completed Jun 2, 2025
@tim-one
Copy link
Member Author

tim-one commented Jun 2, 2025

Fighting with the UI to try to get this marked as "not planned".

@terryjreedy terryjreedy closed this as not planned Won't fix, can't repro, duplicate, stale Jun 2, 2025
@tim-one tim-one reopened this Jun 2, 2025
@terryjreedy terryjreedy closed this as not planned Won't fix, can't repro, duplicate, stale Jun 2, 2025
@terryjreedy
Copy link
Member

Stop reopening.

@tim-one
Copy link
Member Author

tim-one commented Jun 2, 2025

Thanks for closing this @terryjreedy - nothing I tried managed to get this closed as "not planned", so I kept opening it again to thrash at random in other ways. I'm done now 😉.

@terryjreedy
Copy link
Member

If correctly closed but incorrectly 'as completed', never reopen, but hit down arrow for more options, select 'close as not planned', (which does not do that yet) and then reclick Close when it has the gray barred circle symbol (instead of purple check mark) to actually close. Not the best UI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Performance or resource usage type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants