Skip to content

Commit

Permalink
Razor TagHelperDescriptor Hashing (dotnet#24551)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
TanayParikh authored Aug 4, 2020
1 parent cfd20ad commit 29ceed2
Show file tree
Hide file tree
Showing 19 changed files with 1,009 additions and 238 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
Expand Down Expand Up @@ -56,10 +56,20 @@ public virtual int GetHashCode(BoundAttributeDescriptor descriptor)
}

var hash = HashCodeCombiner.Start();
hash.Add(descriptor.Kind);
hash.Add(descriptor.Kind, StringComparer.Ordinal);
hash.Add(descriptor.Name, StringComparer.Ordinal);

if (descriptor.BoundAttributeParameters != null)
{
for (var i = 0; i < descriptor.BoundAttributeParameters.Count; i++)
{
hash.Add(descriptor.BoundAttributeParameters[i]);
}
}

hash.Add(descriptor.Metadata.Count);

return hash.CombinedHash;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
Expand Down Expand Up @@ -51,8 +51,10 @@ public virtual int GetHashCode(BoundAttributeParameterDescriptor descriptor)
}

var hash = HashCodeCombiner.Start();
hash.Add(descriptor.Kind);
hash.Add(descriptor.Kind, StringComparer.Ordinal);
hash.Add(descriptor.Name, StringComparer.Ordinal);
hash.Add(descriptor.TypeName, StringComparer.Ordinal);
hash.Add(descriptor.Metadata?.Count);

return hash.CombinedHash;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
Expand Down Expand Up @@ -39,11 +39,19 @@ public int GetHashCode(DirectiveDescriptor descriptor)
throw new ArgumentNullException(nameof(descriptor));
}

var hashCodeCombiner = HashCodeCombiner.Start();
hashCodeCombiner.Add(descriptor.Directive, StringComparer.Ordinal);
hashCodeCombiner.Add(descriptor.Kind);
var hash = HashCodeCombiner.Start();
hash.Add(descriptor.Directive, StringComparer.Ordinal);
hash.Add(descriptor.Kind);

return hashCodeCombiner.CombinedHash;
if (descriptor.Tokens != null)
{
for (var i = 0; i < descriptor.Tokens.Count; i++)
{
hash.Add(descriptor.Tokens[i]);
}
}

return hash.CombinedHash;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<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>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
Expand Down Expand Up @@ -57,6 +57,7 @@ public virtual int GetHashCode(RequiredAttributeDescriptor descriptor)

var hash = HashCodeCombiner.Start();
hash.Add(descriptor.Name, StringComparer.Ordinal);
hash.Add(descriptor.Value, StringComparer.Ordinal);

return hash.CombinedHash;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,50 @@ public virtual int GetHashCode(TagHelperDescriptor descriptor)
hash.Add(descriptor.Kind, StringComparer.Ordinal);
hash.Add(descriptor.AssemblyName, StringComparer.Ordinal);
hash.Add(descriptor.Name, StringComparer.Ordinal);
hash.Add(descriptor.DisplayName, StringComparer.Ordinal);
hash.Add(descriptor.CaseSensitive ? 1 : 0);

if (descriptor.BoundAttributes != null)
{
for (var i = 0; i < descriptor.BoundAttributes.Count; i++)
{
hash.Add(descriptor.BoundAttributes[i]);
}
}

if (descriptor.TagMatchingRules != null)
{
for (var i = 0; i < descriptor.TagMatchingRules.Count; i++)
{
hash.Add(descriptor.TagMatchingRules[i]);
}
}

if (descriptor.AllowedChildTags != null)
{
for (var i = 0; i < descriptor.AllowedChildTags.Count; i++)
{
hash.Add(descriptor.AllowedChildTags[i]);
}
}

if (descriptor.Diagnostics != null)
{
for (var i = 0; i < descriptor.Diagnostics.Count; i++)
{
hash.Add(descriptor.Diagnostics[i]);
}
}

if (descriptor.Metadata != null)
{
foreach (var kvp in descriptor.Metadata)
{
hash.Add(kvp.Value, StringComparer.Ordinal);
}
}

return hash.CombinedHash;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
Expand Down Expand Up @@ -48,8 +48,17 @@ public virtual int GetHashCode(TagMatchingRuleDescriptor rule)

var hash = HashCodeCombiner.Start();
hash.Add(rule.TagName, StringComparer.Ordinal);
hash.Add(rule.ParentTag, StringComparer.Ordinal);

if (rule.Attributes != null)
{
for (var i = 0; i < rule.Attributes.Count; ++i)
{
hash.Add(rule.Attributes[i]);
}
}

return hash.CombinedHash;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,31 @@
<BuildHelixPayload>false</BuildHelixPayload>
</PropertyGroup>

<ItemGroup>
<EmbeddedResource Include="TestFiles\**\*" />
<None Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

<ItemGroup>
<Reference Include="Microsoft.AspNetCore.Razor.Language" />
<Reference Include="Newtonsoft.Json" Version="12.0.3" />
<ProjectReference Include="..\..\test\Microsoft.AspNetCore.Razor.Test.Common\Microsoft.AspNetCore.Razor.Test.Common.csproj" />
<ProjectReference Include="..\..\test\Microsoft.AspNetCore.Razor.Test.ComponentShim\Microsoft.AspNetCore.Razor.Test.ComponentShim.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Include="$(SharedSourceRoot)RazorShared\TagHelperDescriptorJsonConverter.cs">
<Link>Shared\TagHelperDescriptorJsonConverter.cs</Link>
</Compile>
<Compile Include="$(SharedSourceRoot)RazorShared\RazorDiagnosticJsonConverter.cs">
<Link>Shared\RazorDiagnosticJsonConverter.cs</Link>
</Compile>
<Compile Include="$(SharedSourceRoot)RazorShared\JsonReaderExtensions.cs">
<Link>Shared\JsonReaderExtensions.cs</Link>
</Compile>
<EmbeddedResource Include="$(SharedSourceRoot)RazorShared\taghelpers.json">
<Link>TestFiles\taghelpers.json</Link>
</EmbeddedResource>
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="TestFiles\**\*" />
<None Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

</Project>
Loading

0 comments on commit 29ceed2

Please sign in to comment.