JIT: see if guarded devirtualization for EqualityComparer methods pays off #9028
Labels
area-CodeGen-coreclr
CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
enhancement
Product code improvement that does NOT require public API changes/additions
optimization
tenet-performance
Performance related issue
Milestone
The changes for devirtualizing the
EqualityComparer<T>.Default
calls (dotnet/coreclr#14125) don't kick in in cases where the user can optionally supply a custom comparer -- see for instanceDictionary<TKey, TValue>
and many other generic collection types.Since the jit can now determine the type of the default comparer, it might be worthwhile for the jit to insert a runtime test to see if a particular
IEqualityComparer<T>
typed object is actually the default comparer type forT
, and if so, devirtualize and inline when the test succeeds.I have a partial prototype of the changes needed here: master...AndyAyersMS:GuardedDevirtualization. This has most of the analysis needed to determine when the guarded devirtualization could be done cheaply enough that it is viable. However there are still some roadblocks to work out:
IEqualityComparer<T>
methods as[Intrinsic]
leads to odd errors in the runtime when generating marshalling stubs. Not clear yet what is going on. For now the prototype just checks all interface calls for this interface type, which is likely going to slow the jit down considerably.impDevirtualizeCall
is invoked, because the call may not yet be at top level. The rough idea here is to detect and defer this case, mark the original indirect call as an inline candidate, wait for it to get hoisted to top level, and then retry.Update (Nov 2020): the guarded devirtualization transform is now available in the jit (see dotnet/coreclr#21270), but not enabled by default. We're still lacking the data needed to figure out which call sites would benefit, and at those sites, what class to guess for. PGO data can give us both of these.
Also note
Dictionary<TKey, TValue>
already contains explicit logic for accelerating the default equality comparer path (see dotnet/coreclr#15419), so opportunities for doing guarded devirtualization for objects with typeIEqualityComparer<T>
may not be as compelling. So a good first step here would be to find specific opportunities, likely classes likeDictionary
where there's an optional user-overridable comparer supplied to the.ctor
.category:cq
theme:devirtualization
skill-level:expert
cost:medium
The text was updated successfully, but these errors were encountered: