Skip to content

Commit 03d8bb9

Browse files
EgorBoCopilot
andauthored
Speed up RuntimeType.GetHashCode (#134783)
Use mixed type-handle hashes in CoreCLR, preserving COM identity hashing (Windows only). ```cs using System; using System.Collections.Generic; using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Running; BenchmarkSwitcher.FromAssembly(typeof(Bench).Assembly).Run(args); public class Bench { private readonly Dictionary<Type, int> _dictionary = new() { [typeof(string)] = 42 }; [Benchmark] public int Lookup() => _dictionary[typeof(string)]; } ``` up to [25% faster](EgorBot/Benchmarks#623). --------- Co-authored-by: Copilot <[email protected]> Copilot-Session: 3ed54863-8a13-437b-aa0a-a6f356b253ed Copilot-Session: 033db876-d232-451a-94ab-ed732694508a
1 parent 127f0c2 commit 03d8bb9

5 files changed

Lines changed: 33 additions & 9 deletions

File tree

‎src/coreclr/System.Private.CoreLib/src/System/RuntimeType.CoreCLR.cs‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2341,6 +2341,22 @@ private static bool FilterApplyMethodBase(
23412341

23422342
#endregion
23432343

2344+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2345+
public override int GetHashCode()
2346+
{
2347+
// CLSID types share the __ComObject handle, so preserve their identity hashes.
2348+
if (IsGenericCOMObjectImpl())
2349+
return RuntimeHelpers.GetHashCode(this);
2350+
2351+
// Fibonacci hashing moves the entropy in aligned handles into the high bits.
2352+
#if TARGET_64BIT
2353+
return (int)(((ulong)(nuint)m_handle * 0x9E3779B97F4A7C15UL) >> 32);
2354+
#else
2355+
uint hash = (uint)(nuint)m_handle * 0x9E3779B9U;
2356+
return (int)(hash ^ (hash >> 16));
2357+
#endif
2358+
}
2359+
23442360
#region Private\Internal Members
23452361

23462362
internal unsafe TypeHandle GetNativeTypeHandle()
@@ -3318,8 +3334,7 @@ public override unsafe Guid GUID
33183334
Guid result;
33193335
#if FEATURE_COMINTEROP
33203336
Debug.Assert(OperatingSystem.IsWindows());
3321-
// The fully qualified name is needed since the RuntimeType has a TypeHandle property.
3322-
if (System.Runtime.CompilerServices.TypeHandle.AreSameType(th, System.Runtime.CompilerServices.TypeHandle.TypeHandleOf<__ComObject>()))
3337+
if (IsGenericCOMObjectImpl())
33233338
{
33243339
GetComObjectGuidWorker(this, &result);
33253340
}
@@ -4045,7 +4060,8 @@ internal void CallDefaultStructConstructor(ref byte data)
40454060
protected override bool IsCOMObjectImpl() => RuntimeTypeHandle.CanCastTo(this, (RuntimeType)typeof(__ComObject));
40464061

40474062
// We need to check the type handle values - not the instances - to determine if the runtime type is a generic ComObject.
4048-
internal bool IsGenericCOMObjectImpl() => TypeHandle.Value == typeof(__ComObject).TypeHandle.Value;
4063+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4064+
internal bool IsGenericCOMObjectImpl() => m_handle == RuntimeTypeHandle.ToIntPtr(typeof(__ComObject).TypeHandle);
40494065
#else
40504066
protected override bool IsCOMObjectImpl() => false;
40514067

‎src/coreclr/nativeaot/System.Private.CoreLib/src/System/RuntimeType.NativeAot.cs‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,15 @@ public override int GetHashCode()
212212
{
213213
MethodTable* pEEType = _pUnderlyingEEType;
214214
if (pEEType != null)
215-
return ((nuint)pEEType).GetHashCode();
215+
{
216+
// Fibonacci hashing moves the entropy in aligned handles into the high bits.
217+
#if TARGET_64BIT
218+
return (int)(((ulong)(nuint)pEEType * 0x9E3779B97F4A7C15UL) >> 32);
219+
#else
220+
uint hash = (uint)(nuint)pEEType * 0x9E3779B9U;
221+
return (int)(hash ^ (hash >> 16));
222+
#endif
223+
}
216224
return RuntimeHelpers.GetHashCode(this);
217225
}
218226

‎src/libraries/System.Private.CoreLib/src/System/Collections/Generic/EqualityComparer.cs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public override bool Equals(T? x, T? y)
153153

154154
[MethodImpl(MethodImplOptions.AggressiveInlining)]
155155
public override int GetHashCode([DisallowNull] T obj) =>
156-
obj?.GetHashCode() ?? 0;
156+
obj is null ? 0 : obj.GetHashCode();
157157

158158
// Equals method for the comparer itself.
159159
public override bool Equals([NotNullWhen(true)] object? obj) =>
@@ -222,7 +222,7 @@ public override bool Equals(T? x, T? y)
222222

223223
[MethodImpl(MethodImplOptions.AggressiveInlining)]
224224
public override int GetHashCode([DisallowNull] T obj) =>
225-
obj?.GetHashCode() ?? 0;
225+
obj is null ? 0 : obj.GetHashCode();
226226

227227
// Equals method for the comparer itself.
228228
public override bool Equals([NotNullWhen(true)] object? obj) =>
@@ -320,7 +320,7 @@ void ISerializable.GetObjectData(SerializationInfo info, StreamingContext contex
320320

321321
public override bool Equals(string? x, string? y) => string.Equals(x, y);
322322

323-
public override int GetHashCode([DisallowNull] string obj) => obj?.GetHashCode() ?? 0;
323+
public override int GetHashCode([DisallowNull] string obj) => obj is null ? 0 : obj.GetHashCode();
324324

325325
public bool Equals(ReadOnlySpan<char> span, string target)
326326
{

‎src/libraries/System.Private.CoreLib/src/System/RuntimeType.cs‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,6 @@ public override Type GetEnumUnderlyingType()
184184
return Enum.InternalGetUnderlyingType(this);
185185
}
186186

187-
public override int GetHashCode() => RuntimeHelpers.GetHashCode(this);
188-
189187
internal RuntimeModule GetRuntimeModule() => RuntimeTypeHandle.GetModule(this);
190188

191189
protected override TypeCode GetTypeCodeImpl()

‎src/mono/System.Private.CoreLib/src/System/RuntimeType.Mono.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,6 +1486,8 @@ public override int GenericParameterPosition
14861486

14871487
#endregion
14881488

1489+
public override int GetHashCode() => RuntimeHelpers.GetHashCode(this);
1490+
14891491
public static bool operator ==(RuntimeType? left, RuntimeType? right)
14901492
{
14911493
return ReferenceEquals(left, right);

0 commit comments

Comments
 (0)