Skip to content

Commit adb591e

Browse files
authored
Continue using HashCodeCombiner in Razor.Language (dotnet#37725)
* Continue using HashCodeCombiner in Razor.Language References by Razor.Language end up getting loaded in VS and the source generator * Use HashCodeCombiner.Start
1 parent de9923e commit adb591e

33 files changed

+249
-88
lines changed

src/Razor/Microsoft.AspNetCore.Razor.Internal.SourceGenerator.Transport/Microsoft.AspNetCore.Razor.Internal.SourceGenerator.Transport.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
<ItemGroup>
3131
<Reference Include="Microsoft.CodeAnalysis.Common" />
3232
<Reference Include="Microsoft.CodeAnalysis.CSharp" />
33-
<Reference Include="Microsoft.Bcl.HashCode" />
3433
</ItemGroup>
3534

3635
</Project>

src/Razor/Microsoft.AspNetCore.Razor.Language/src/AllowedChildTagDescriptorComparer.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using Microsoft.Extensions.Internal;
67

78
namespace Microsoft.AspNetCore.Razor.Language
89
{
@@ -41,10 +42,10 @@ public virtual bool Equals(
4142
/// <inheritdoc />
4243
public virtual int GetHashCode(AllowedChildTagDescriptor descriptor)
4344
{
44-
var hash = new HashCode();
45-
hash.Add(descriptor.Name ?? string.Empty, StringComparer.Ordinal);
45+
var hash = HashCodeCombiner.Start();
46+
hash.Add(descriptor.Name, StringComparer.Ordinal);
4647

47-
return hash.ToHashCode();
48+
return hash.CombinedHash;
4849
}
4950
}
5051
}

src/Razor/Microsoft.AspNetCore.Razor.Language/src/BoundAttributeDescriptorComparer.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Linq;
7+
using Microsoft.Extensions.Internal;
78

89
namespace Microsoft.AspNetCore.Razor.Language
910
{
@@ -55,12 +56,12 @@ public int GetHashCode(BoundAttributeDescriptor descriptor)
5556
throw new ArgumentNullException(nameof(descriptor));
5657
}
5758

58-
var hash = new HashCode();
59-
hash.Add(descriptor.Kind ?? string.Empty, StringComparer.Ordinal);
60-
hash.Add(descriptor.Name ?? string.Empty, StringComparer.Ordinal);
59+
var hash = HashCodeCombiner.Start();
60+
hash.Add(descriptor.Kind, StringComparer.Ordinal);
61+
hash.Add(descriptor.Name, StringComparer.Ordinal);
6162
hash.Add(descriptor.IsEditorRequired);
62-
hash.Add(descriptor.TypeName ?? string.Empty, StringComparer.Ordinal);
63-
hash.Add(descriptor.Documentation ?? string.Empty, StringComparer.Ordinal);
63+
hash.Add(descriptor.TypeName, StringComparer.Ordinal);
64+
hash.Add(descriptor.Documentation, StringComparer.Ordinal);
6465

6566
if (descriptor.BoundAttributeParameters != null)
6667
{
@@ -88,7 +89,7 @@ public int GetHashCode(BoundAttributeDescriptor descriptor)
8889
}
8990
}
9091

91-
return hash.ToHashCode();
92+
return hash.CombinedHash;
9293
}
9394
}
9495
}

src/Razor/Microsoft.AspNetCore.Razor.Language/src/BoundAttributeParameterDescriptorComparer.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Linq;
7+
using Microsoft.Extensions.Internal;
78

89
namespace Microsoft.AspNetCore.Razor.Language
910
{
@@ -49,13 +50,13 @@ public virtual int GetHashCode(BoundAttributeParameterDescriptor descriptor)
4950
throw new ArgumentNullException(nameof(descriptor));
5051
}
5152

52-
var hash = new HashCode();
53-
hash.Add(descriptor.Kind ?? string.Empty, StringComparer.Ordinal);
54-
hash.Add(descriptor.Name ?? string.Empty, StringComparer.Ordinal);
55-
hash.Add(descriptor.TypeName ?? string.Empty, StringComparer.Ordinal);
53+
var hash = HashCodeCombiner.Start();
54+
hash.Add(descriptor.Kind, StringComparer.Ordinal);
55+
hash.Add(descriptor.Name, StringComparer.Ordinal);
56+
hash.Add(descriptor.TypeName, StringComparer.Ordinal);
5657
hash.Add(descriptor.Metadata?.Count);
5758

58-
return hash.ToHashCode();
59+
return hash.CombinedHash;
5960
}
6061
}
6162
}

src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/LinePragma.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Globalization;
6+
using Microsoft.Extensions.Internal;
67

78
namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
89
{
@@ -61,7 +62,11 @@ public bool Equals(LinePragma other)
6162

6263
public override int GetHashCode()
6364
{
64-
return HashCode.Combine(StartLineIndex, LineCount, FilePath);
65+
var hash = HashCodeCombiner.Start();
66+
hash.Add(StartLineIndex);
67+
hash.Add(LineCount);
68+
hash.Add(FilePath);
69+
return hash;
6570
}
6671

6772
public override string ToString()

src/Razor/Microsoft.AspNetCore.Razor.Language/src/DefaultRazorDiagnostic.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using Microsoft.Extensions.Internal;
56

67
namespace Microsoft.AspNetCore.Razor.Language
78
{
@@ -68,7 +69,7 @@ public override bool Equals(RazorDiagnostic obj)
6869

6970
public override int GetHashCode()
7071
{
71-
var hash = new HashCode();
72+
var hash = HashCodeCombiner.Start();
7273
hash.Add(Descriptor.GetHashCode());
7374
hash.Add(Span.GetHashCode());
7475

@@ -77,7 +78,7 @@ public override int GetHashCode()
7778
hash.Add(Args[i]);
7879
}
7980

80-
return hash.ToHashCode();
81+
return hash;
8182
}
8283
}
8384
}

src/Razor/Microsoft.AspNetCore.Razor.Language/src/DirectiveDescriptorComparer.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Linq;
7+
using Microsoft.Extensions.Internal;
78

89
namespace Microsoft.AspNetCore.Razor.Language
910
{
@@ -38,8 +39,8 @@ public int GetHashCode(DirectiveDescriptor descriptor)
3839
throw new ArgumentNullException(nameof(descriptor));
3940
}
4041

41-
var hash = new HashCode();
42-
hash.Add(descriptor.Directive ?? string.Empty, StringComparer.Ordinal);
42+
var hash = HashCodeCombiner.Start();
43+
hash.Add(descriptor.Directive, StringComparer.Ordinal);
4344
hash.Add(descriptor.Kind);
4445

4546
if (descriptor.Tokens != null)
@@ -50,7 +51,7 @@ public int GetHashCode(DirectiveDescriptor descriptor)
5051
}
5152
}
5253

53-
return hash.ToHashCode();
54+
return hash.CombinedHash;
5455
}
5556
}
5657
}

src/Razor/Microsoft.AspNetCore.Razor.Language/src/DirectiveTokenDescriptorComparer.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using Microsoft.Extensions.Internal;
67

78
namespace Microsoft.AspNetCore.Razor.Language
89
{
@@ -33,7 +34,11 @@ public int GetHashCode(DirectiveTokenDescriptor descriptor)
3334
throw new ArgumentNullException(nameof(descriptor));
3435
}
3536

36-
return HashCode.Combine(descriptor.Kind, descriptor.Optional ? 1 : 0);
37+
var hashCodeCombiner = HashCodeCombiner.Start();
38+
hashCodeCombiner.Add(descriptor.Kind);
39+
hashCodeCombiner.Add(descriptor.Optional ? 1 : 0);
40+
41+
return hashCodeCombiner.CombinedHash;
3742
}
3843
}
3944
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
#nullable enable
5+
6+
using System.Collections;
7+
using System.Collections.Generic;
8+
using System.Runtime.CompilerServices;
9+
10+
namespace Microsoft.Extensions.Internal
11+
{
12+
internal struct HashCodeCombiner
13+
{
14+
private long _combinedHash64;
15+
16+
public int CombinedHash
17+
{
18+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
19+
get { return _combinedHash64.GetHashCode(); }
20+
}
21+
22+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
23+
private HashCodeCombiner(long seed)
24+
{
25+
_combinedHash64 = seed;
26+
}
27+
28+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
29+
public static implicit operator int(HashCodeCombiner self)
30+
{
31+
return self.CombinedHash;
32+
}
33+
34+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
35+
public void Add(int i)
36+
{
37+
_combinedHash64 = ((_combinedHash64 << 5) + _combinedHash64) ^ i;
38+
}
39+
40+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
41+
public void Add<T>(T? o)
42+
{
43+
Add(o?.GetHashCode() ?? 0);
44+
}
45+
46+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
47+
public void Add<TValue>(TValue value, IEqualityComparer<TValue> comparer)
48+
{
49+
var hashCode = value != null ? comparer.GetHashCode(value) : 0;
50+
Add(hashCode);
51+
}
52+
53+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
54+
public static HashCodeCombiner Start()
55+
{
56+
return new HashCodeCombiner(0x1505L);
57+
}
58+
}
59+
}

src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/AddTagHelperChunkGenerator.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Generic;
66
using System.Linq;
77
using System.Text;
8+
using Microsoft.Extensions.Internal;
89

910
namespace Microsoft.AspNetCore.Razor.Language.Legacy
1011
{
@@ -49,14 +50,14 @@ public override bool Equals(object obj)
4950
/// <inheritdoc />
5051
public override int GetHashCode()
5152
{
52-
var combiner = new HashCode();
53+
var combiner = HashCodeCombiner.Start();
5354
combiner.Add(base.GetHashCode());
54-
combiner.Add(LookupText ?? string.Empty, StringComparer.Ordinal);
55-
combiner.Add(DirectiveText ?? string.Empty, StringComparer.Ordinal);
56-
combiner.Add(TypePattern ?? string.Empty, StringComparer.Ordinal);
57-
combiner.Add(AssemblyName ?? string.Empty, StringComparer.Ordinal);
55+
combiner.Add(LookupText, StringComparer.Ordinal);
56+
combiner.Add(DirectiveText, StringComparer.Ordinal);
57+
combiner.Add(TypePattern, StringComparer.Ordinal);
58+
combiner.Add(AssemblyName, StringComparer.Ordinal);
5859

59-
return combiner.ToHashCode();
60+
return combiner.CombinedHash;
6061
}
6162

6263
public override string ToString()

src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/AutoCompleteEditHandler.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using Microsoft.AspNetCore.Razor.Language.Syntax;
7+
using Microsoft.Extensions.Internal;
78

89
namespace Microsoft.AspNetCore.Razor.Language.Legacy
910
{
@@ -59,7 +60,11 @@ public override bool Equals(object obj)
5960
public override int GetHashCode()
6061
{
6162
// Hash code should include only immutable properties but Equals also checks the type.
62-
return HashCode.Combine(TypeHashCode, AutoCompleteAtEndOfSpan);
63+
var hashCodeCombiner = HashCodeCombiner.Start();
64+
hashCodeCombiner.Add(TypeHashCode);
65+
hashCodeCombiner.Add(AutoCompleteAtEndOfSpan);
66+
67+
return hashCodeCombiner.CombinedHash;
6368
}
6469
}
6570
}

src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/DirectiveTokenChunkGenerator.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Text;
6+
using Microsoft.Extensions.Internal;
67

78
namespace Microsoft.AspNetCore.Razor.Language.Legacy
89
{
@@ -26,7 +27,11 @@ public override bool Equals(object obj)
2627

2728
public override int GetHashCode()
2829
{
29-
return HashCode.Combine(base.GetHashCode(), Type);
30+
var combiner = HashCodeCombiner.Start();
31+
combiner.Add(base.GetHashCode());
32+
combiner.Add(Type);
33+
34+
return combiner.CombinedHash;
3035
}
3136

3237
public override string ToString()

src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/ImplicitExpressionEditHandler.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.IO;
99
using System.Linq;
1010
using Microsoft.AspNetCore.Razor.Language.Syntax;
11+
using Microsoft.Extensions.Internal;
1112

1213
namespace Microsoft.AspNetCore.Razor.Language.Legacy
1314
{
@@ -53,7 +54,10 @@ public override bool Equals(object obj)
5354
public override int GetHashCode()
5455
{
5556
// Hash code should include only immutable properties and base has none.
56-
return HashCode.Combine(AcceptTrailingDot);
57+
var hashCodeCombiner = HashCodeCombiner.Start();
58+
hashCodeCombiner.Add(AcceptTrailingDot);
59+
60+
return hashCodeCombiner;
5761
}
5862

5963
protected override PartialParseResultInternal CanAcceptChange(SyntaxNode target, SourceChange change)

src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/LiteralAttributeChunkGenerator.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System;
54
using System.Globalization;
5+
using Microsoft.Extensions.Internal;
66

77
namespace Microsoft.AspNetCore.Razor.Language.Legacy
88
{
@@ -33,7 +33,12 @@ public override bool Equals(object obj)
3333

3434
public override int GetHashCode()
3535
{
36-
return HashCode.Combine(Prefix, Value);
36+
var hashCodeCombiner = HashCodeCombiner.Start();
37+
38+
hashCodeCombiner.Add(Prefix);
39+
hashCodeCombiner.Add(Value);
40+
41+
return hashCodeCombiner;
3742
}
3843
}
3944
}

src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/LocationTagged.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Diagnostics;
66
using System.Globalization;
7+
using Microsoft.Extensions.Internal;
78

89
namespace Microsoft.AspNetCore.Razor.Language.Legacy
910
{
@@ -44,7 +45,11 @@ public override bool Equals(object obj)
4445

4546
public override int GetHashCode()
4647
{
47-
return HashCode.Combine(Location, Value);
48+
var hashCodeCombiner = HashCodeCombiner.Start();
49+
hashCodeCombiner.Add(Location);
50+
hashCodeCombiner.Add(Value);
51+
52+
return hashCodeCombiner.CombinedHash;
4853
}
4954

5055
public override string ToString()

0 commit comments

Comments
 (0)