diff --git a/src/coreclr/tools/Common/TypeSystem/Common/MethodDesc.cs b/src/coreclr/tools/Common/TypeSystem/Common/MethodDesc.cs index 639455ff98ba2..93b12af20f141 100644 --- a/src/coreclr/tools/Common/TypeSystem/Common/MethodDesc.cs +++ b/src/coreclr/tools/Common/TypeSystem/Common/MethodDesc.cs @@ -491,7 +491,8 @@ private int AcquireHashCode() } /// - /// Compute HashCode. Should only be overridden by a MethodDesc that represents an instantiated method. + /// Compute HashCode. This hashcode is persisted into the image. + /// The algorithm to compute it must be in sync with the one used at runtime. /// protected virtual int ComputeHashCode() { diff --git a/src/coreclr/tools/Common/TypeSystem/IL/Stubs/DynamicInvokeMethodThunk.cs b/src/coreclr/tools/Common/TypeSystem/IL/Stubs/DynamicInvokeMethodThunk.cs index ee50cc4f0565f..9a02967189db3 100644 --- a/src/coreclr/tools/Common/TypeSystem/IL/Stubs/DynamicInvokeMethodThunk.cs +++ b/src/coreclr/tools/Common/TypeSystem/IL/Stubs/DynamicInvokeMethodThunk.cs @@ -112,6 +112,13 @@ public override MethodSignature Signature } } + protected override int ComputeHashCode() + { + // The hashcode of DynamicInvoke thunks is not persisted. It allows us to override default and + // use a hashcode with good distribution to reduce hash table collisions. + return base.ComputeHashCode() ^ _targetSignature.GetHashCode(); + } + public override string Name => "DynamicInvoke"; public override string DiagnosticName => "DynamicInvoke";