Skip to content

[release/9.0.1xx] [Static web assets] Reduce allocations on DefineStaticWebAssetEndpoints #43099

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

41 changes: 41 additions & 0 deletions src/StaticWebAssetsSdk/Tasks/Data/ContentTypeMapping.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
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;

public string Cache { get; set; } = cache;

public int Priority { get; } = priority;

internal static ContentTypeMapping FromTaskItem(ITaskItem contentTypeMappings) => new(
contentTypeMappings.ItemSpec,
contentTypeMappings.GetMetadata(nameof(Cache)),
contentTypeMappings.GetMetadata(nameof(Pattern)),
int.Parse(contentTypeMappings.GetMetadata(nameof(Priority))));

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

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

Large diffs are not rendered by default.

53 changes: 8 additions & 45 deletions src/StaticWebAssetsSdk/Tasks/DefineStaticWebAssetEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Build.Framework;
using Microsoft.Extensions.FileSystemGlobbing;
using Microsoft.NET.Sdk.StaticWebAssets.Tasks;
using System.Globalization;

Expand Down Expand Up @@ -50,6 +49,7 @@ public override bool Execute()
}

var contentTypeMappings = ContentTypeMappings.Select(ContentTypeMapping.FromTaskItem).OrderByDescending(m => m.Priority).ToArray();
var contentTypeProvider = new ContentTypeProvider(contentTypeMappings);
var endpoints = new List<StaticWebAssetEndpoint>();

foreach (var kvp in staticWebAssets)
Expand All @@ -61,7 +61,7 @@ public override bool Execute()
// When we define the endpoint, we apply the path to the asset as if it was coming from the current project.
// If the endpoint is then passed to a referencing project or packaged into a nuget package, the path will be
// adjusted at that time.
var assetEndpoints = CreateEndpoints(asset, contentTypeMappings);
var assetEndpoints = CreateEndpoints(asset, contentTypeProvider);

foreach (var endpoint in assetEndpoints)
{
Expand All @@ -84,7 +84,7 @@ public override bool Execute()
return !Log.HasLoggedErrors;
}

private List<StaticWebAssetEndpoint> CreateEndpoints(StaticWebAsset asset, ContentTypeMapping[] contentTypeMappings)
private List<StaticWebAssetEndpoint> CreateEndpoints(StaticWebAsset asset, ContentTypeProvider contentTypeMappings)
{
var routes = asset.ComputeRoutes();
var result = new List<StaticWebAssetEndpoint>();
Expand Down Expand Up @@ -219,56 +219,19 @@ private string GetFileLength(StaticWebAsset asset)
}
}

private (string mimeType, string cache) ResolveContentType(StaticWebAsset asset, ContentTypeMapping[] contentTypeMappings)
private (string mimeType, string cache) ResolveContentType(StaticWebAsset asset, ContentTypeProvider contentTypeProvider)
{
var relativePath = asset.ComputePathWithoutTokens(asset.RelativePath);
foreach (var mapping in contentTypeMappings)
var mapping = contentTypeProvider.ResolveContentTypeMapping(relativePath, Log);

if (mapping.MimeType != null)
{
if (mapping.Matches(Path.GetFileName(relativePath)))
{
Log.LogMessage(MessageImportance.Low, $"Matched {relativePath} to {mapping.MimeType} using pattern {mapping.Pattern}");
return (mapping.MimeType, mapping.Cache);
}
else
{
Log.LogMessage(MessageImportance.Low, $"No match for {relativePath} using pattern {mapping.Pattern}");
}
return (mapping.MimeType, mapping.Cache);
}

Log.LogMessage(MessageImportance.Low, $"No match for {relativePath}. Using default content type 'application/octet-stream'");

return ("application/octet-stream", null);
}

private class ContentTypeMapping
{
private readonly Matcher _matcher;

public ContentTypeMapping(string mimeType, string cache, string pattern, int priority)
{
Pattern = pattern;
MimeType = mimeType;
Cache = cache;
Priority = priority;
_matcher = new Matcher();
_matcher.AddInclude(pattern);
}

public string Pattern { get; set; }

public string MimeType { get; set; }

public string Cache { get; set; }

public int Priority { get; }

internal static ContentTypeMapping FromTaskItem(ITaskItem contentTypeMappings) => new(
contentTypeMappings.ItemSpec,
contentTypeMappings.GetMetadata(nameof(Cache)),
contentTypeMappings.GetMetadata(nameof(Pattern)),
int.Parse(contentTypeMappings.GetMetadata(nameof(Priority))));

internal bool Matches(string identity) => _matcher.Match(identity).HasMatches;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15798,7 +15798,7 @@
},
{
"Name": "Content-Type",
"Value": "application/octect-stream"
"Value": "application/octet-stream"
},
{
"Name": "ETag",
Expand Down Expand Up @@ -16094,7 +16094,7 @@
},
{
"Name": "Content-Type",
"Value": "application/octect-stream"
"Value": "application/octet-stream"
},
{
"Name": "ETag",
Expand Down Expand Up @@ -16131,7 +16131,7 @@
},
{
"Name": "Content-Type",
"Value": "application/octect-stream"
"Value": "application/octet-stream"
},
{
"Name": "ETag",
Expand Down Expand Up @@ -16168,7 +16168,7 @@
},
{
"Name": "Content-Type",
"Value": "application/octect-stream"
"Value": "application/octet-stream"
},
{
"Name": "ETag",
Expand Down Expand Up @@ -35383,7 +35383,7 @@
},
{
"Name": "Content-Type",
"Value": "application/octect-stream"
"Value": "application/octet-stream"
},
{
"Name": "ETag",
Expand Down Expand Up @@ -35434,7 +35434,7 @@
},
{
"Name": "Content-Type",
"Value": "application/octect-stream"
"Value": "application/octet-stream"
},
{
"Name": "ETag",
Expand Down Expand Up @@ -35683,7 +35683,7 @@
},
{
"Name": "Content-Type",
"Value": "application/x-gzip"
"Value": "text/plain"
},
{
"Name": "ETag",
Expand Down Expand Up @@ -36083,7 +36083,7 @@
},
{
"Name": "Content-Type",
"Value": "application/x-gzip"
"Value": "text/plain"
},
{
"Name": "ETag",
Expand Down Expand Up @@ -36183,7 +36183,7 @@
},
{
"Name": "Content-Type",
"Value": "application/octect-stream"
"Value": "application/octet-stream"
},
{
"Name": "ETag",
Expand Down Expand Up @@ -36234,7 +36234,7 @@
},
{
"Name": "Content-Type",
"Value": "application/octect-stream"
"Value": "application/octet-stream"
},
{
"Name": "ETag",
Expand Down Expand Up @@ -36283,7 +36283,7 @@
},
{
"Name": "Content-Type",
"Value": "application/octect-stream"
"Value": "application/octet-stream"
},
{
"Name": "ETag",
Expand Down Expand Up @@ -36334,7 +36334,7 @@
},
{
"Name": "Content-Type",
"Value": "application/octect-stream"
"Value": "application/octet-stream"
},
{
"Name": "ETag",
Expand Down Expand Up @@ -36383,7 +36383,7 @@
},
{
"Name": "Content-Type",
"Value": "application/octect-stream"
"Value": "application/octet-stream"
},
{
"Name": "ETag",
Expand Down Expand Up @@ -36434,7 +36434,7 @@
},
{
"Name": "Content-Type",
"Value": "application/octect-stream"
"Value": "application/octet-stream"
},
{
"Name": "ETag",
Expand Down
Loading
Loading