Skip to content

Commit 29ceed2

Browse files
authored
Razor TagHelperDescriptor Hashing (#24551)
* Razor TagHelperDescriptor Hashing and Caching 1/2 PRs for https://github.com/dotnet/aspnetcore/issues/23170 The other PR will be in aspnetcore-tooling to utilize these changes. dotnet/razor#2307
1 parent cfd20ad commit 29ceed2

19 files changed

+1009
-238
lines changed

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
@@ -56,10 +56,20 @@ public virtual int GetHashCode(BoundAttributeDescriptor descriptor)
5656
}
5757

5858
var hash = HashCodeCombiner.Start();
59-
hash.Add(descriptor.Kind);
59+
hash.Add(descriptor.Kind, StringComparer.Ordinal);
6060
hash.Add(descriptor.Name, StringComparer.Ordinal);
6161

62+
if (descriptor.BoundAttributeParameters != null)
63+
{
64+
for (var i = 0; i < descriptor.BoundAttributeParameters.Count; i++)
65+
{
66+
hash.Add(descriptor.BoundAttributeParameters[i]);
67+
}
68+
}
69+
70+
hash.Add(descriptor.Metadata.Count);
71+
6272
return hash.CombinedHash;
6373
}
6474
}
65-
}
75+
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
@@ -51,8 +51,10 @@ public virtual int GetHashCode(BoundAttributeParameterDescriptor descriptor)
5151
}
5252

5353
var hash = HashCodeCombiner.Start();
54-
hash.Add(descriptor.Kind);
54+
hash.Add(descriptor.Kind, StringComparer.Ordinal);
5555
hash.Add(descriptor.Name, StringComparer.Ordinal);
56+
hash.Add(descriptor.TypeName, StringComparer.Ordinal);
57+
hash.Add(descriptor.Metadata?.Count);
5658

5759
return hash.CombinedHash;
5860
}

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
@@ -39,11 +39,19 @@ public int GetHashCode(DirectiveDescriptor descriptor)
3939
throw new ArgumentNullException(nameof(descriptor));
4040
}
4141

42-
var hashCodeCombiner = HashCodeCombiner.Start();
43-
hashCodeCombiner.Add(descriptor.Directive, StringComparer.Ordinal);
44-
hashCodeCombiner.Add(descriptor.Kind);
42+
var hash = HashCodeCombiner.Start();
43+
hash.Add(descriptor.Directive, StringComparer.Ordinal);
44+
hash.Add(descriptor.Kind);
4545

46-
return hashCodeCombiner.CombinedHash;
46+
if (descriptor.Tokens != null)
47+
{
48+
for (var i = 0; i < descriptor.Tokens.Count; i++)
49+
{
50+
hash.Add(descriptor.Tokens[i]);
51+
}
52+
}
53+
54+
return hash.CombinedHash;
4755
}
4856
}
4957
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;

src/Razor/Microsoft.AspNetCore.Razor.Language/src/Microsoft.AspNetCore.Razor.Language.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<Description>Razor is a markup syntax for adding server-side logic to web pages. This package contains the Razor parser and code generation infrastructure.</Description>

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
@@ -57,6 +57,7 @@ public virtual int GetHashCode(RequiredAttributeDescriptor descriptor)
5757

5858
var hash = HashCodeCombiner.Start();
5959
hash.Add(descriptor.Name, StringComparer.Ordinal);
60+
hash.Add(descriptor.Value, StringComparer.Ordinal);
6061

6162
return hash.CombinedHash;
6263
}

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

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,50 @@ public virtual int GetHashCode(TagHelperDescriptor descriptor)
120120
hash.Add(descriptor.Kind, StringComparer.Ordinal);
121121
hash.Add(descriptor.AssemblyName, StringComparer.Ordinal);
122122
hash.Add(descriptor.Name, StringComparer.Ordinal);
123+
hash.Add(descriptor.DisplayName, StringComparer.Ordinal);
124+
hash.Add(descriptor.CaseSensitive ? 1 : 0);
125+
126+
if (descriptor.BoundAttributes != null)
127+
{
128+
for (var i = 0; i < descriptor.BoundAttributes.Count; i++)
129+
{
130+
hash.Add(descriptor.BoundAttributes[i]);
131+
}
132+
}
133+
134+
if (descriptor.TagMatchingRules != null)
135+
{
136+
for (var i = 0; i < descriptor.TagMatchingRules.Count; i++)
137+
{
138+
hash.Add(descriptor.TagMatchingRules[i]);
139+
}
140+
}
141+
142+
if (descriptor.AllowedChildTags != null)
143+
{
144+
for (var i = 0; i < descriptor.AllowedChildTags.Count; i++)
145+
{
146+
hash.Add(descriptor.AllowedChildTags[i]);
147+
}
148+
}
149+
150+
if (descriptor.Diagnostics != null)
151+
{
152+
for (var i = 0; i < descriptor.Diagnostics.Count; i++)
153+
{
154+
hash.Add(descriptor.Diagnostics[i]);
155+
}
156+
}
157+
158+
if (descriptor.Metadata != null)
159+
{
160+
foreach (var kvp in descriptor.Metadata)
161+
{
162+
hash.Add(kvp.Value, StringComparer.Ordinal);
163+
}
164+
}
123165

124166
return hash.CombinedHash;
125167
}
126168
}
127-
}
169+
}

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
@@ -48,8 +48,17 @@ public virtual int GetHashCode(TagMatchingRuleDescriptor rule)
4848

4949
var hash = HashCodeCombiner.Start();
5050
hash.Add(rule.TagName, StringComparer.Ordinal);
51+
hash.Add(rule.ParentTag, StringComparer.Ordinal);
52+
53+
if (rule.Attributes != null)
54+
{
55+
for (var i = 0; i < rule.Attributes.Count; ++i)
56+
{
57+
hash.Add(rule.Attributes[i]);
58+
}
59+
}
5160

5261
return hash.CombinedHash;
5362
}
5463
}
55-
}
64+
}

src/Razor/Microsoft.AspNetCore.Razor.Language/test/Microsoft.AspNetCore.Razor.Language.Test.csproj

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,31 @@
1313
<BuildHelixPayload>false</BuildHelixPayload>
1414
</PropertyGroup>
1515

16-
<ItemGroup>
17-
<EmbeddedResource Include="TestFiles\**\*" />
18-
<None Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
19-
</ItemGroup>
20-
2116
<ItemGroup>
2217
<Reference Include="Microsoft.AspNetCore.Razor.Language" />
18+
<Reference Include="Newtonsoft.Json" Version="12.0.3" />
2319
<ProjectReference Include="..\..\test\Microsoft.AspNetCore.Razor.Test.Common\Microsoft.AspNetCore.Razor.Test.Common.csproj" />
2420
<ProjectReference Include="..\..\test\Microsoft.AspNetCore.Razor.Test.ComponentShim\Microsoft.AspNetCore.Razor.Test.ComponentShim.csproj" />
2521
</ItemGroup>
2622

23+
<ItemGroup>
24+
<Compile Include="$(SharedSourceRoot)RazorShared\TagHelperDescriptorJsonConverter.cs">
25+
<Link>Shared\TagHelperDescriptorJsonConverter.cs</Link>
26+
</Compile>
27+
<Compile Include="$(SharedSourceRoot)RazorShared\RazorDiagnosticJsonConverter.cs">
28+
<Link>Shared\RazorDiagnosticJsonConverter.cs</Link>
29+
</Compile>
30+
<Compile Include="$(SharedSourceRoot)RazorShared\JsonReaderExtensions.cs">
31+
<Link>Shared\JsonReaderExtensions.cs</Link>
32+
</Compile>
33+
<EmbeddedResource Include="$(SharedSourceRoot)RazorShared\taghelpers.json">
34+
<Link>TestFiles\taghelpers.json</Link>
35+
</EmbeddedResource>
36+
</ItemGroup>
37+
38+
<ItemGroup>
39+
<EmbeddedResource Include="TestFiles\**\*" />
40+
<None Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
41+
</ItemGroup>
42+
2743
</Project>

0 commit comments

Comments
 (0)