Skip to content

Commit c371ad2

Browse files
authored
[StaticWebAssets] Improve globbing match performance (#44159)
* Removes the usage of Microsoft.Extensions.FileSystemGlobbing in favor of a custom implementation optimized for our scenarios. * Improves the parallelism in DefineStaticWebAssetEndpoints. * Spanifies the string manipulation logic.
1 parent 131a081 commit c371ad2

39 files changed

+3418
-982
lines changed

src/StaticWebAssetsSdk/Tasks/Compression/ResolveCompressedAssets.cs

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

44
using Microsoft.AspNetCore.StaticWebAssets.Tasks.Utils;
55
using Microsoft.Build.Framework;
6-
using Microsoft.Extensions.FileSystemGlobbing;
76

87
namespace Microsoft.AspNetCore.StaticWebAssets.Tasks;
98

@@ -60,12 +59,15 @@ public override bool Execute()
6059
var includePatterns = SplitPattern(IncludePatterns);
6160
var excludePatterns = SplitPattern(ExcludePatterns);
6261

63-
var matcher = new Matcher();
64-
matcher.AddIncludePatterns(includePatterns);
65-
matcher.AddExcludePatterns(excludePatterns);
62+
var matcher = new StaticWebAssetGlobMatcherBuilder()
63+
.AddIncludePatterns(includePatterns)
64+
.AddExcludePatterns(excludePatterns)
65+
.Build();
6666

6767
var matchingCandidateAssets = new List<StaticWebAsset>();
6868

69+
var matchContext = StaticWebAssetGlobMatcher.CreateMatchContext();
70+
6971
// Add each candidate asset to each compression configuration with a matching pattern.
7072
foreach (var asset in candidates)
7173
{
@@ -80,9 +82,10 @@ public override bool Execute()
8082
}
8183

8284
var relativePath = asset.ComputePathWithoutTokens(asset.RelativePath);
83-
var match = matcher.Match(relativePath);
85+
matchContext.SetPathAndReinitialize(relativePath.AsSpan());
86+
var match = matcher.Match(matchContext);
8487

85-
if (!match.HasMatches)
88+
if (!match.IsMatch)
8689
{
8790
Log.LogMessage(
8891
MessageImportance.Low,

src/StaticWebAssetsSdk/Tasks/ComputeReferenceStaticWebAssetItems.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public override bool Execute()
4444
var resultAssets = new List<StaticWebAsset>();
4545
foreach (var (key, group) in existingAssets)
4646
{
47-
if (!ComputeReferenceStaticWebAssetItems.TryGetUniqueAsset(group, out var selected))
47+
if (!TryGetUniqueAsset(group, out var selected))
4848
{
4949
if (selected == null)
5050
{

src/StaticWebAssetsSdk/Tasks/ComputeStaticWebAssetsForCurrentProject.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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 Microsoft.Build.Framework;
@@ -36,7 +36,7 @@ public override bool Execute()
3636
var resultAssets = new List<StaticWebAsset>();
3737
foreach (var (key, group) in currentProjectAssets)
3838
{
39-
if (!ComputeStaticWebAssetsForCurrentProject.TryGetUniqueAsset(group, out var selected))
39+
if (!TryGetUniqueAsset(group, out var selected))
4040
{
4141
if (selected == null)
4242
{

src/StaticWebAssetsSdk/Tasks/Data/ContentTypeMapping.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@
44
using System.Diagnostics;
55
using System.Globalization;
66
using Microsoft.Build.Framework;
7-
using Microsoft.Extensions.FileSystemGlobbing;
87

98
namespace Microsoft.AspNetCore.StaticWebAssets.Tasks;
109

1110
[DebuggerDisplay($"{{{nameof(GetDebuggerDisplay)}(),nq}}")]
1211
internal struct ContentTypeMapping(string mimeType, string cache, string pattern, int priority)
1312
{
14-
private Matcher _matcher;
15-
1613
public string Pattern { get; set; } = pattern;
1714

1815
public string MimeType { get; set; } = mimeType;
@@ -27,15 +24,5 @@ internal struct ContentTypeMapping(string mimeType, string cache, string pattern
2724
contentTypeMappings.GetMetadata(nameof(Pattern)),
2825
int.Parse(contentTypeMappings.GetMetadata(nameof(Priority)), CultureInfo.InvariantCulture));
2926

30-
internal bool Matches(string identity)
31-
{
32-
if (_matcher == null)
33-
{
34-
_matcher = new Matcher();
35-
_matcher.AddInclude(Pattern);
36-
}
37-
return _matcher.Match(identity).HasMatches;
38-
}
39-
4027
private readonly string GetDebuggerDisplay() => $"Pattern: {Pattern}, MimeType: {MimeType}, Cache: {Cache}, Priority: {Priority}";
4128
}

src/StaticWebAssetsSdk/Tasks/Data/ContentTypeProvider.cs

Lines changed: 463 additions & 402 deletions
Large diffs are not rendered by default.

src/StaticWebAssetsSdk/Tasks/Data/StaticWebAsset.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,7 @@ internal string EmbedTokens(string relativePath)
918918
var pattern = StaticWebAssetPathPattern.Parse(relativePath, Identity);
919919
var resolver = StaticWebAssetTokenResolver.Instance;
920920
pattern.EmbedTokens(this, resolver);
921-
return pattern.RawPattern;
921+
return pattern.RawPattern.ToString();
922922
}
923923

924924
internal FileInfo ResolveFile() => ResolveFile(Identity, OriginalItemSpec);

0 commit comments

Comments
 (0)