Always use unstable sort for simple types#14825
Merged
straight-shoota merged 1 commit intocrystal-lang:masterfrom Aug 8, 2024
Merged
Always use unstable sort for simple types#14825straight-shoota merged 1 commit intocrystal-lang:masterfrom
straight-shoota merged 1 commit intocrystal-lang:masterfrom
Conversation
ysbaddaden
approved these changes
Jul 23, 2024
Member
|
I suppose |
Member
|
I'm also wondering if we could generalize this property (e.g. via a module) so we don't need to hard-code a compatibility list in |
Contributor
Author
|
Two
|
Member
|
Argh. I wish we had readability of a slice as a type information, not a runtime property. |
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When calling
#sort!without a block, if two elements have the same binary representations whenever they compare equal using#<=>, then they are indistinguishable from each other and swapping is a no-op. This allows the use of unstable sorting which runs slightly faster and requires no temporary allocations, as opposed to stable sorting which allocates memory linear in the collection size.Following this criterion, the only reference type that supports unstable sorting is:
Primitive floats do not support it, as the signed zeros compare equal but have opposite sign bits. For simplicity, unions also aren't touched; either they don't have a meaningful, non-null
#<=>defined across the variant types, or they break the criterion (e.g.1_i32and1_i8have different type IDs).#sortalways delegates to#sort!. This does not affect#sort_by!since the projected element type alone doesn't guarantee the original elements themselves can be swapped in the same way.