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
Proposal: Add Array.Sort<T>(T[], int, int, Comparison<T>) overload #23587
Comments
In addition, we could look to see what the impact would be of changing: IntrospectiveSort(keys, index, length, comparer.Compare); to instead be: IntrospectiveSort(keys, index, length, comparer is ComparisonComparer<T> cc ? cc._comparison : comparer.Compare); and the same for other such occurrences. Worst case this should cost a type check for no gain, but best case when |
Fortunately |
Nit: The parameter should be named |
Not quite true. This is generic code so it's fast only when T is a value type. Still, it should be faster than creating a new delegate. |
Looks good to us, but we should probably add the same API to |
FYI: The API review discussion was recorded - see https://youtu.be/b96co3sNzNI?t=2193 (4 min duration) |
Note that currently |
Ugh. That's probably a deal breaker. |
Seems like it 😢 |
The API was rejected after all based on the concerns raised above. FYI: Next round of the API review discussion was also recorded - see https://youtu.be/OZnaGV2omvI?t=360 (4 min duration) |
The PR dotnet/coreclr#8504 changed Array.Sort to be based internally on the
Comparison<T>
delegate rather than on theIComparer<T>
interface, as delegate dispatch is faster than interface dispatch and it enabled consolidating two implementations. That means if anIComparer<T>
is provided, its Compare method is wrapped in aComparison<T>
delegate.However, there is no overload of Array.Sort that takes the array, offset, length, and comparison delegate (the only overload that takes a delegate is
Sort(T[], Comparison<T>)
:https://github.com/dotnet/corefx/blob/155a46c0c59fddefc009c2a4affd0fd2b3bac2b3/src/System.Runtime/ref/System.Runtime.cs#L305-L309
so if you have a delegate and need to provide offset+length, you first need to allocate an object implementing
IComparer<T>
to wrap it so you can call the right overload, and then the implementation will wrap that with an allocatedComparison<T>
; that's two allocations and multiple levels of indirection when none of that is necessary.Instead, we should just add this overload:
cc: @mikedn, @jkotas, @joperezr, @AlexGhiondea
The text was updated successfully, but these errors were encountered: