Cache ModelMetadata HashCode

This commit is contained in:
Ryan Brandenburg 2016-10-06 12:16:38 -07:00
parent a80d7d744a
commit 69b4b64fd5
1 changed files with 9 additions and 1 deletions

View File

@ -23,6 +23,8 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
/// </summary>
public static readonly int DefaultOrder = 10000;
private int? _hashCode;
/// <summary>
/// Creates a new <see cref="ModelMetadata"/>.
/// </summary>
@ -405,7 +407,13 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
/// <inheritdoc />
public override int GetHashCode()
{
return Identity.GetHashCode();
// Normaly caching the hashcode would be dangerous, but Identity is deeply immutable so this is safe.
if (_hashCode == null)
{
_hashCode = Identity.GetHashCode();
}
return _hashCode.Value;
}
private void InitializeTypeInformation()