From fd7c69073623f608b7cd69c59476bd44d036321c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Petryka?= <35800402+MichalPetryka@users.noreply.github.com> Date: Tue, 14 Jul 2026 02:26:20 +0200 Subject: [PATCH 1/4] Move GetHashCode nullcheck to managed --- src/coreclr/vm/comutilnative.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/coreclr/vm/comutilnative.cpp b/src/coreclr/vm/comutilnative.cpp index 4412dfaec4eadf..8adecf0936dbc8 100644 --- a/src/coreclr/vm/comutilnative.cpp +++ b/src/coreclr/vm/comutilnative.cpp @@ -1585,8 +1585,7 @@ FCIMPL1(INT32, ObjectNative::TryGetHashCode, Object* obj) { FCALL_CONTRACT; - if (obj == NULL) - return 0; + _ASSERTE(obj != NULL); OBJECTREF objRef = ObjectToOBJECTREF(obj); return objRef->TryGetHashCode(); From c5a4ba0398f3447033e88408f80f93b2f9ee91b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Petryka?= <35800402+MichalPetryka@users.noreply.github.com> Date: Tue, 14 Jul 2026 02:28:29 +0200 Subject: [PATCH 2/4] Refactor TryGetHashCode to accept only non-null objects Updated TryGetHashCode to require a non-nullable object, ensuring that null checks are handled separately in the GetHashCode method. --- .../CompilerServices/RuntimeHelpers.CoreCLR.cs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.CoreCLR.cs index 584f6cd3e15965..77354873541f92 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.CoreCLR.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.CoreCLR.cs @@ -237,13 +237,18 @@ public static void PrepareDelegate(Delegate d) /// returned. /// [MethodImpl(MethodImplOptions.InternalCall)] - internal static extern int TryGetHashCode(object? o); + internal static extern int TryGetHashCode(object o); [LibraryImport(QCall, EntryPoint = "ObjectNative_GetHashCodeSlow")] private static partial int GetHashCodeSlow(ObjectHandleOnStack o); public static int GetHashCode(object? o) { + if (o is null) + { + return 0; + } + int hashCode = TryGetHashCode(o); if (hashCode == 0) { @@ -254,10 +259,6 @@ public static int GetHashCode(object? o) [MethodImpl(MethodImplOptions.NoInlining)] static int GetHashCodeWorker(object? o) { - if (o is null) - { - return 0; - } return GetHashCodeSlow(ObjectHandleOnStack.Create(ref o)); } } From 0d3c0197eb68ac83f3388aecaa601b99aa68fc3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Petryka?= <35800402+MichalPetryka@users.noreply.github.com> Date: Tue, 14 Jul 2026 03:01:29 +0200 Subject: [PATCH 3/4] Add AggressiveInlining to GetHashCode method --- .../System/Runtime/CompilerServices/RuntimeHelpers.CoreCLR.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.CoreCLR.cs index 77354873541f92..f7173786461a70 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.CoreCLR.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.CoreCLR.cs @@ -242,6 +242,7 @@ public static void PrepareDelegate(Delegate d) [LibraryImport(QCall, EntryPoint = "ObjectNative_GetHashCodeSlow")] private static partial int GetHashCodeSlow(ObjectHandleOnStack o); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int GetHashCode(object? o) { if (o is null) From 0ec6ca6b1438bc0e2329e8d910bb8ae516b878f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Petryka?= <35800402+MichalPetryka@users.noreply.github.com> Date: Tue, 14 Jul 2026 04:11:27 +0200 Subject: [PATCH 4/4] Remove AggressiveInlining from GetHashCode Removed aggressive inlining from GetHashCode method. --- .../System/Runtime/CompilerServices/RuntimeHelpers.CoreCLR.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.CoreCLR.cs index f7173786461a70..77354873541f92 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.CoreCLR.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.CoreCLR.cs @@ -242,7 +242,6 @@ public static void PrepareDelegate(Delegate d) [LibraryImport(QCall, EntryPoint = "ObjectNative_GetHashCodeSlow")] private static partial int GetHashCodeSlow(ObjectHandleOnStack o); - [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int GetHashCode(object? o) { if (o is null)