Skip to content

Commit

Permalink
Speed up GVMLookupForSlot (#85901)
Browse files Browse the repository at this point in the history
BasicMinimalApi spends about 1% of samples in this method.
  • Loading branch information
MichalStrehovsky authored May 8, 2023
1 parent fc1cb19 commit d0c9457
Showing 1 changed file with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,19 @@ public static object GenericLookupAndCast(object arg, IntPtr context, IntPtr sig

public static unsafe IntPtr GVMLookupForSlot(object obj, RuntimeMethodHandle slot)
{
Entry entry = LookupInCache(s_cache, (IntPtr)obj.GetMethodTable(), *(IntPtr*)&slot);
entry ??= CacheMiss((IntPtr)obj.GetMethodTable(), *(IntPtr*)&slot,
Entry entry = LookupInCache(s_cache, (IntPtr)obj.GetMethodTable(), RuntimeMethodHandle.ToIntPtr(slot));
if (entry != null)
return entry.Result;

return GVMLookupForSlotSlow(obj, slot);
}

private static unsafe IntPtr GVMLookupForSlotSlow(object obj, RuntimeMethodHandle slot)
{
Entry entry = CacheMiss((IntPtr)obj.GetMethodTable(), RuntimeMethodHandle.ToIntPtr(slot),
(IntPtr context, IntPtr signature, object contextObject, ref IntPtr auxResult)
=> RuntimeAugments.TypeLoaderCallbacks.ResolveGenericVirtualMethodTarget(new RuntimeTypeHandle(new EETypePtr(context)), *(RuntimeMethodHandle*)&signature));

return entry.Result;
}

Expand All @@ -132,7 +141,11 @@ internal static unsafe IntPtr OpenInstanceMethodLookup(IntPtr openResolver, obje
private static Entry LookupInCache(Entry[] cache, IntPtr context, IntPtr signature)
{
int key = ((context.GetHashCode() >> 4) ^ signature.GetHashCode()) & (cache.Length - 1);
#if DEBUG
Entry entry = cache[key];
#else
Entry entry = Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(cache), key);
#endif
while (entry != null)
{
if (entry.Context == context && entry.Signature == signature)
Expand Down

0 comments on commit d0c9457

Please sign in to comment.