未验证 提交 82a17a58 编写于 作者: A Andy Gocke 提交者: GitHub

Improve SubstitutedMethodSymbol.GetHashCode (#41674)

The changes made in https://github.com/dotnet/roslyn/pull/41123 fix the
GetHashCode for SubstitutedMethodSymbol for more nullability scenarios,
but end up hashing all generic methods with a non-generic containing
type into the same bucket. This can cause large performance regressions
for the compiler.

This change ensures that substituted symbols which have substitutions
equivalent to their original methods' type parameter continue to be
equal, but substituted methods with different type substitutions are not
considered equal.
上级 f173f0ce
...@@ -368,7 +368,8 @@ private int ComputeHashCode() ...@@ -368,7 +368,8 @@ private int ComputeHashCode()
// (e.g, ignoring nullability) and want to retain the same hashcode. As such, consider only // (e.g, ignoring nullability) and want to retain the same hashcode. As such, consider only
// the original definition for the hashcode when we know equality is possible // the original definition for the hashcode when we know equality is possible
var containingHashCode = _containingType.GetHashCode(); var containingHashCode = _containingType.GetHashCode();
if (containingHashCode == this.OriginalDefinition.ContainingType.GetHashCode()) if (containingHashCode == this.OriginalDefinition.ContainingType.GetHashCode() &&
wasConstructedForAnnotations(this))
{ {
return code; return code;
} }
...@@ -402,6 +403,24 @@ private int ComputeHashCode() ...@@ -402,6 +403,24 @@ private int ComputeHashCode()
} }
return code; return code;
static bool wasConstructedForAnnotations(SubstitutedMethodSymbol method)
{
var typeArguments = method.TypeArgumentsWithAnnotations;
var typeParameters = method.OriginalDefinition.TypeParameters;
for (int i = 0; i < typeArguments.Length; i++)
{
if (!typeParameters[i].Equals(
typeArguments[i].Type,
TypeCompareKind.ConsiderEverything))
{
return false;
}
}
return true;
}
} }
public sealed override bool Equals(Symbol obj, TypeCompareKind compareKind) public sealed override bool Equals(Symbol obj, TypeCompareKind compareKind)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册