You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 13, 2026. It is now read-only.
Repository navigation
This repository was archived by the owner on Nov 13, 2026. It is now read-only.
Implement proper EqualityComparer<T> and Comparer<T> #763
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.
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?
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.
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.
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.
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?
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).
Activity
MichalStrehovsky commentedon Nov 14, 2017
So in rough terms, the feature involves recognizing calls to
get_Defaulton both types and redirecting the call to a method that is specialized for the givenT. The expansion might need to do unwrapping ifTisNullable.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 ofgetCallInfo. We would wantExpandIntrinsicForCallsiteto operate on & return RuntimeDetermined types, butgetCallInfocurrently 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
getCallInfoto 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:
RuntimeDeterminedTypeaMetadataTypeso that we can run the interface method resolution on it.GetMethodForInstantiatedTypeAPI onTypeSystemContextand come up with a replacement API that works for locating methods onRuntimeDeterminedTypetoo. Adding a new virtual method toDefType(e.g.MethodDesc GetMethodForTypicalMethod(MethodDesc typicalMethod)would probably work. This is needed so that APIs such asTryResolveConstrainedEnumMethodcan operate on bothInstantiatedTypeorRuntimeDeterminedType. We would likely makeGetMethodForInstantiatedType(and the corresponding API for runtime determined types) internal.getCallInfooperate on RuntimeDetermined types.After that, it should be possible to implement the feature in
ExpandIntrinsicForCallsite.jkotas commentedon Nov 14, 2017
I think we should be recognizing the calls to
Create.get_Defaulthas lazy initialization logic that should stay as is.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.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?
MichalStrehovsky commentedon Nov 14, 2017
Ah yes. Typo :).
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.
jkotas commentedon Nov 14, 2017
Where does this context show up?
My naïve thought was that the Create method can be always implemented as this:
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
typeofinEqualityComparerHelpers.GetUnknownEquatableComparerand 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.
MichalStrehovsky commentedon Nov 14, 2017
It's the generic dictionary used to give context to
GetUnknownEquatableComparer<T>.MichalStrehovsky commentedon Nov 14, 2017
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.jkotas commentedon Nov 14, 2017
Right. All sorts of generic constructs can be optimized better if we had RuntimeDeterminedTypes plumbed through the whole system...
MichalStrehovsky commentedon Nov 15, 2017
We might want to re-think how we cache the
_defaultvalue. Codegen can't see through the uselessEqualityComparer<T>.get_Defaultcall. I wonder if this is because of how we organize ourget_Default. CoreCLR doesn't seem to be caching it. @AndyAyersMS is this expected to get optimized away on CoreCLR?AndyAyersMS commentedon Nov 15, 2017
In CoreCLR the
get_Defaultmethod 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_Defaultmust 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_Defaultis 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).Implement efficient Array.IndexOf
Implement efficient Array.IndexOf
Implement efficient Array.IndexOf (#4977)