diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/TypeLoaderExports.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/TypeLoaderExports.cs index 6d71f0d880d0af..29500cf3089961 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/TypeLoaderExports.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/TypeLoaderExports.cs @@ -111,10 +111,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; } @@ -133,7 +142,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)