Skip to content

Sort<T, TComparer> extensions performance #39466

Closed

Description

@jkotas

Sort overloads that take TComparer as a generic argument were approved in #19969 . However, the implementation did not take advantage of the generic TComparer and the TComparer overload is actually the less efficient option.

This issue tracks fixing the implementation of Sort<T, TComparer> to take full advantage of the generic TComparer.

Full context: #37941 (comment)

Activity

  1. added
    untriaged
    on Jul 16, 2020
  2. added this to the Future milestone on Jul 16, 2020
  3. removed
    untriaged
    on Jul 16, 2020
  4. jkotas commented on Jul 16, 2020

    @jkotas
    MemberAuthor
  5. nietras commented on Jul 17, 2020

    @nietras
    Contributor

    @jkotas thanks. Moving conversation from #37941 (comment) here :)

    For time line, I do not expect we would be able to get this change into .NET 5.

    I was really hoping we could get this in. The changes shouldn't be that big. As far as I understood, from a standup video I think, "feature freeze"/branch for .NET 5 RC might happen around 8th of August. That's doable in my view. Could you perhaps expand on why you think it won't be possible? What's the deadline? :)

    "unsafe" cast of Comparison<T> to Comparison<object>
    Why would that be needed?

    The idea here is to replace the Comparison<T> C# code path with a TComparer path e.g.

        internal partial class ComparerArraySortHelper<T, TComparer>
            where TComparer : IComparer<T>
        {
            public void Sort(Span<T> keys, TComparer comparer)
            {

    we then need to support Comparison<T> via e.g.

        internal readonly struct ComparisonComparer<T> : IComparer<T>
        {
            private readonly Comparison<T> _comparison;
    
            public ComparisonComparer(Comparison<T> comparison) =>
                _comparison = comparison;
    
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            public int Compare([AllowNull] T x, [AllowNull] T y) =>
                _comparison(x, y);
        }

    Compare can now be inlined for value types via #10048. 👍

    However, this won't work for reference types. At least not to my knowledge. Inlining and generics and all. The idea then would be to support reference types via internal trickery (if needed!) and use below for all reference types:

        internal readonly struct ObjectComparisonComparer : IComparer<object>
        {
            private readonly Comparison<object> _comparison;
    
            public ObjectComparisonComparer(Comparison<object> comparison) =>
                _comparison = comparison;
    
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            public int Compare([AllowNull] object x, [AllowNull] object y) =>
                _comparison(x, y);
        }

    If the runtime can be changed to not having to have this, that would be great and would solve a long standing issue for me.

    whether it would make sense to delete the TComparer overloads from the public surface for now until we can get the right implementation for them in place.

    Very much hope we can get the implementation in and not change the public surface. The actual changes to the sorting code as such are minimal. :)

  6. jkotas commented on Jul 17, 2020

    @jkotas
    MemberAuthor

    I was really hoping we could get this in. The changes shouldn't be that big

    I agree for functional correctness. I suspect that this work will uncover codegen issues that we won't have time to fix in next 3 weeks.

  7. nietras commented on Jul 17, 2020

    @nietras
    Contributor

    will uncover codegen issues that we won't have time to fix in next 3 weeks.

    That is of course a possibility/concern, given my experience with codegen here incl. net5 previews I am pretty optimistic, though. :) We should try at least. But I need guidance on how to refactor the whole ArraySortHelper stuff and whether the Comparison trick is viable/acceptable. My guess would be that if you, @jkotas, did this you could do the code changes in a couple of hours. 😉

  8. jkotas commented on Jul 17, 2020

    @jkotas
    MemberAuthor

    Comparison trick is viable/acceptable

    If it was the only workaround for the codegen issues in the change, I think it would be acceptable. I suspect that the problem that it is trying to workaround is going to be present in more situations that are not possible to workaround.

  9. added
    tenet-performance
    on Nov 11, 2020
  10. dotnet-policy-service commented on Oct 19, 2026

    @dotnet-policy-service
    Contributor

    Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process.

    This process is part of our issue cleanup automation.

  11. dotnet-policy-service commented on Oct 19, 2026

    @dotnet-policy-service
    Contributor

    Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process.

    This process is part of our issue cleanup automation.

  12. added
    backlog-cleanup-candidate
    on Oct 19, 2026
  13. dotnet-policy-service commented on Nov 16, 2026

    @dotnet-policy-service
    Contributor

    This issue will now be closed since it had been marked no-recent-activity but received no further activity in the past 14 days. It is still possible to reopen or comment on the issue, but please note that the issue will be locked if it remains inactive for another 30 days.

  14. removed this from the Future milestone on Nov 16, 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

    area-System.Memorybacklog-cleanup-candidateno-recent-activitytenet-performance

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions