Skip to content
This repository was archived by the owner on Nov 13, 2026. It is now read-only.
This repository was archived by the owner on Nov 13, 2026. It is now read-only.

Implement proper EqualityComparer<T> and Comparer<T> #763

Closed

Description

@jkotas

#761 for CoreRT

Activity

  1. self-assigned this
    on Jan 30, 2016
  2. added this to the Future milestone on Jan 30, 2016
  3. removed their assignment
    on Nov 9, 2017
  4. MichalStrehovsky commented on Nov 14, 2017

    @MichalStrehovsky
    Member

    So in rough terms, the feature involves recognizing calls to get_Default on both types and redirecting the call to a method that is specialized for the given T. The expansion might need to do unwrapping if T is Nullable.

    The ideal place to do this redirection would be in ExpandIntrinsicForCallsite, but when compiling a shared method body, this will become problematic because of the design of getCallInfo. We would want ExpandIntrinsicForCallsite to operate on & return RuntimeDetermined types, but getCallInfo currently operates on canonical forms and only switches to RuntimeDetermined very late. This is problematic for other reasons too (complicated constrained calls in shared method bodies come to mind).

    I think making getCallInfo to operate on RuntimeDetermined types is a necessary prerequisite. (We would still not expose RuntimeDetermined types to RyuJIT.)

    Without actually writing code (just looking at what's there), I think we'll need:

    • Make RuntimeDeterminedType a MetadataType so that we can run the interface method resolution on it.
    • Revise the GetMethodForInstantiatedType API on TypeSystemContext and come up with a replacement API that works for locating methods on RuntimeDeterminedType too. Adding a new virtual method to DefType (e.g. MethodDesc GetMethodForTypicalMethod(MethodDesc typicalMethod) would probably work. This is needed so that APIs such as TryResolveConstrainedEnumMethod can operate on both InstantiatedType or RuntimeDeterminedType. We would likely make GetMethodForInstantiatedType (and the corresponding API for runtime determined types) internal.
    • Make getCallInfo operate on RuntimeDetermined types.

    After that, it should be possible to implement the feature in ExpandIntrinsicForCallsite.

  5. jkotas commented on Nov 14, 2017

    @jkotas
    MemberAuthor

    recognizing calls to get_Default

    I think we should be recognizing the calls to Create. get_Default has lazy initialization logic that should stay as is.

    redirecting the call to a method that is specialized for the given T

    We may just want to replace the IL body of the Create with the right new specialized_comparer() directly. We will need the logic to figure the specialized comparer type to support the comparer devirtualization, so we can use it here as well.

    when compiling a shared method body

    I though that the shared method body has to call type loader. Otherwise, we would need a special dictionary entry for the comparers. Am I missing something?

  6. MichalStrehovsky commented on Nov 14, 2017

    @MichalStrehovsky
    Member

    I think we should be recognizing the calls to Create

    Ah yes. Typo :).

    I though that the shared method body has to call type loader

    Not if T is a generic struct (that is instantiated over a reference type) implementing the interface. For that we need to be able to find the right type context to pass to the generic comparer and I think we can't get away without doing this through runtime determined types.

  7. jkotas commented on Nov 14, 2017

    @jkotas
    MemberAuthor

    right type context to pass to the generic comparer

    Where does this context show up?

    My naïve thought was that the Create method can be always implemented as this:

    private static Comparer<T> Create()
    {
        return EqualityComparerHelpers.GetUnknownEquatableComparer<T>();
    }
    

    You can even make this the default body of the method in C#, and leave it alone in ExpandIntrinsicForCallsite for shared methods. (Be careful about ProjectN though - when I have tried that, the DR picked up the typeof in EqualityComparerHelpers.GetUnknownEquatableComparer and opted in a lot of extra types into reflection.)

    The redirection in ExpandIntrinsicForCallsite is as optimization to not call the type loader as often, and to make the dependency tracking work.

  8. MichalStrehovsky commented on Nov 14, 2017

    @MichalStrehovsky
    Member

    Where does this context show up?

    It's the generic dictionary used to give context to GetUnknownEquatableComparer<T>.

  9. MichalStrehovsky commented on Nov 14, 2017

    @MichalStrehovsky
    Member

    Ah, I think I see. If we only redirect if shared generics are not involved and put EqualityComparerHelpers.GetUnknownEquatableComparer<T>(); in the method body (as actual C# code) outside of ProjectN, it eliminates the problem with context lookup (because everything will happen the "normal way").

    Based on my reading the NUTC code, it would also optimize the SomeStructImplementingTheInterface<__Canon>> case, but we can probably live without it.

  10. jkotas commented on Nov 14, 2017

    @jkotas
    MemberAuthor

    it would also optimize the SomeStructImplementingTheInterface<__Canon> case, but we can probably live without it.

    Right. All sorts of generic constructs can be optimized better if we had RuntimeDeterminedTypes plumbed through the whole system...

  11. MichalStrehovsky commented on Nov 15, 2017

    @MichalStrehovsky
    Member

    We might want to re-think how we cache the _default value. Codegen can't see through the useless EqualityComparer<T>.get_Default call. I wonder if this is because of how we organize our get_Default. CoreCLR doesn't seem to be caching it. @AndyAyersMS is this expected to get optimized away on CoreCLR?

        private static bool CompareThings(MyEnum c1, MyEnum c2)
        {
    00007FF75FED1A40  push        rdi  
    00007FF75FED1A41  push        rsi  
    00007FF75FED1A42  sub         rsp,28h  
    00007FF75FED1A46  mov         esi,ecx  
    00007FF75FED1A48  mov         edi,edx  
    00007FF75FED1A4A  call        S_P_CoreLib_System_Collections_Generic_EqualityComparer_1<repro_Program_MyEnum>__get_Default (07FF760004FC4h)  
    00007FF75FED1A4F  cmp         esi,edi  
    00007FF75FED1A51  sete        al  
    00007FF75FED1A54  movzx       eax,al  
            return EqualityComparer<MyEnum>.Default.Equals(c1, c2);
        }
    00007FF75FED1A57  add         rsp,28h  
    00007FF75FED1A5B  pop         rsi  
    00007FF75FED1A5C  pop         rdi  
    00007FF75FED1A5D  ret  
    
  12. AndyAyersMS commented on Nov 15, 2017

    @AndyAyersMS
    Member

    In CoreCLR the get_Default method is the intrinsic, and it has caching via an implicit static field that is initialized by the cctor.

    The cost of fetching the static field (which triggers the cctor to initialize it if necessary, which is what calls the Create in Core) is pretty high even when the class is already initialized. So when we know what type of object get_Default must return and we also know that object is not referenced in the comparer call that follows we suppress the field access too.

    So generally this all gets optimized away.

    In CoreRT there is no cctor and the jit doesn't know that get_Default is special and its side effects can be safely suppressed, and so it gets called and the result is ignored (since again the comparer methods don't look at the comparer object).

  13. added 2 commits that reference this issue on Nov 21, 2017
    02193f8
    64b5f4f
  14. added a commit that references this issue on Nov 21, 2017
    48b2ad6
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions