Skip to content

Commit

Permalink
[StaticWebAssets] Improve globbing match performance (#44159)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
javiercn authored Nov 22, 2024
1 parent 131a081 commit c371ad2
Show file tree
Hide file tree
Showing 39 changed files with 3,418 additions and 982 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using Microsoft.AspNetCore.StaticWebAssets.Tasks.Utils;
using Microsoft.Build.Framework;
using Microsoft.Extensions.FileSystemGlobbing;

namespace Microsoft.AspNetCore.StaticWebAssets.Tasks;

Expand Down Expand Up @@ -60,12 +59,15 @@ public override bool Execute()
var includePatterns = SplitPattern(IncludePatterns);
var excludePatterns = SplitPattern(ExcludePatterns);

var matcher = new Matcher();
matcher.AddIncludePatterns(includePatterns);
matcher.AddExcludePatterns(excludePatterns);
var matcher = new StaticWebAssetGlobMatcherBuilder()
.AddIncludePatterns(includePatterns)
.AddExcludePatterns(excludePatterns)
.Build();

var matchingCandidateAssets = new List<StaticWebAsset>();

var matchContext = StaticWebAssetGlobMatcher.CreateMatchContext();

// Add each candidate asset to each compression configuration with a matching pattern.
foreach (var asset in candidates)
{
Expand All @@ -80,9 +82,10 @@ public override bool Execute()
}

var relativePath = asset.ComputePathWithoutTokens(asset.RelativePath);
var match = matcher.Match(relativePath);
matchContext.SetPathAndReinitialize(relativePath.AsSpan());
var match = matcher.Match(matchContext);

if (!match.HasMatches)
if (!match.IsMatch)
{
Log.LogMessage(
MessageImportance.Low,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public override bool Execute()
var resultAssets = new List<StaticWebAsset>();
foreach (var (key, group) in existingAssets)
{
if (!ComputeReferenceStaticWebAssetItems.TryGetUniqueAsset(group, out var selected))
if (!TryGetUniqueAsset(group, out var selected))
{
if (selected == null)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Build.Framework;
Expand Down Expand Up @@ -36,7 +36,7 @@ public override bool Execute()
var resultAssets = new List<StaticWebAsset>();
foreach (var (key, group) in currentProjectAssets)
{
if (!ComputeStaticWebAssetsForCurrentProject.TryGetUniqueAsset(group, out var selected))
if (!TryGetUniqueAsset(group, out var selected))
{
if (selected == null)
{
Expand Down
13 changes: 0 additions & 13 deletions src/StaticWebAssetsSdk/Tasks/Data/ContentTypeMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@
using System.Diagnostics;
using System.Globalization;
using Microsoft.Build.Framework;
using Microsoft.Extensions.FileSystemGlobbing;

namespace Microsoft.AspNetCore.StaticWebAssets.Tasks;

[DebuggerDisplay($"{{{nameof(GetDebuggerDisplay)}(),nq}}")]
internal struct ContentTypeMapping(string mimeType, string cache, string pattern, int priority)
{
private Matcher _matcher;

public string Pattern { get; set; } = pattern;

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

internal bool Matches(string identity)
{
if (_matcher == null)
{
_matcher = new Matcher();
_matcher.AddInclude(Pattern);
}
return _matcher.Match(identity).HasMatches;
}

private readonly string GetDebuggerDisplay() => $"Pattern: {Pattern}, MimeType: {MimeType}, Cache: {Cache}, Priority: {Priority}";
}
865 changes: 463 additions & 402 deletions src/StaticWebAssetsSdk/Tasks/Data/ContentTypeProvider.cs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/StaticWebAssetsSdk/Tasks/Data/StaticWebAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@ internal string EmbedTokens(string relativePath)
var pattern = StaticWebAssetPathPattern.Parse(relativePath, Identity);
var resolver = StaticWebAssetTokenResolver.Instance;
pattern.EmbedTokens(this, resolver);
return pattern.RawPattern;
return pattern.RawPattern.ToString();
}

internal FileInfo ResolveFile() => ResolveFile(Identity, OriginalItemSpec);
Expand Down
Loading

0 comments on commit c371ad2

Please sign in to comment.