Skip to content

[StaticWebAssets] Backport perf fixes #47913

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

Draft
wants to merge 8 commits into
base: release/9.0.3xx
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Copyright (c) .NET Foundation. All rights reserved.
<StaticWebAssetsAdditionalPublishProperties>$(StaticWebAssetsAdditionalPublishProperties);_PublishingBlazorWasmProject=true</StaticWebAssetsAdditionalPublishProperties>
<StaticWebAssetsAdditionalEmbeddedPublishProperties>$(StaticWebAssetsAdditionalEmbeddedPublishProperties);_PublishingBlazorWasmProject=true</StaticWebAssetsAdditionalEmbeddedPublishProperties>
<StaticWebAssetStandaloneHosting Condition="'$(StaticWebAssetStandaloneHosting)' == '' and '$(StaticWebAssetProjectMode)' == 'Root'">true</StaticWebAssetStandaloneHosting>
<StaticWebAssetMakeReferencedAssetOriginalItemSpecAbsolute Condition="'$(StaticWebAssetMakeReferencedAssetOriginalItemSpecAbsolute)' == ''">true</StaticWebAssetMakeReferencedAssetOriginalItemSpecAbsolute>
<StaticWebAssetsGetEmbeddedPublishAssetsTargets>ComputeFilesToPublish;GetCurrentProjectEmbeddedPublishStaticWebAssetItems</StaticWebAssetsGetEmbeddedPublishAssetsTargets>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,16 +219,20 @@ Copyright (c) .NET Foundation. All rights reserved.
<!-- Build -->

<Target Name="ResolveBuildCompressedStaticWebAssets" DependsOnTargets="$(ResolveBuildCompressedStaticWebAssetsDependsOn)">

<PropertyGroup>
<_ResolveBuildCompressedStaticWebAssetsCachePath>$(_StaticWebAssetsManifestBase)rbcswa.dswa.cache.json</_ResolveBuildCompressedStaticWebAssetsCachePath>
</PropertyGroup>

<DefineStaticWebAssets
CandidateAssets="@(_CompressedStaticWebAssets)"
CacheManifestPath="$(_ResolveBuildCompressedStaticWebAssetsCachePath)"
>
<Output TaskParameter="Assets" ItemName="_CompressionBuildStaticWebAsset" />
<Output TaskParameter="AssetDetails" ItemName="_ResolveBuildCompressedStaticWebAssetsDetails" />
</DefineStaticWebAssets>

<DefineStaticWebAssetEndpoints
CandidateAssets="@(_CompressionBuildStaticWebAsset);@(_PrecompressedStaticWebAssets)"
ExistingEndpoints="@(StaticWebAssetEndpoint)"
ContentTypeMappings="@(StaticWebAssetContentTypeMapping)"
>
<Output TaskParameter="Endpoints" ItemName="_CompressionBuildStaticWebAssetEndpoint" />
Expand All @@ -242,7 +246,6 @@ Copyright (c) .NET Foundation. All rights reserved.

<ApplyCompressionNegotiation
CandidateEndpoints="@(StaticWebAssetEndpoint)"
AssetFileDetails="@(_ResolveBuildCompressedStaticWebAssetsDetails)"
CandidateAssets="@(_CompressionCurrentProjectBuildAssets)"
>
<Output TaskParameter="UpdatedEndpoints" ItemName="_UpdatedCompressionBuildEndpoints" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,19 @@ Copyright (c) .NET Foundation. All rights reserved.
to identify them and correctly clasify them. Modules from other projects or packages will already be correctly tagged when we
retrieve them.
-->

<PropertyGroup>
<_ResolveJsInitializerModuleStaticWebAssetsCachePath>$(_StaticWebAssetsManifestBase)rjimswa.dswa.cache.json</_ResolveJsInitializerModuleStaticWebAssetsCachePath>
</PropertyGroup>

<DefineStaticWebAssets Condition="@(_JSModuleCandidates) != ''"
CandidateAssets="@(_JSModuleCandidates)"
AssetTraitName="JSModule"
AssetTraitValue="JSLibraryModule"
RelativePathFilter="**/$(PackageId).lib.module.js"
PropertyOverrides="AssetTraitName;AssetTraitValue"
AssetMergeSource="$(StaticWebAssetMergeTarget)"
CacheManifestPath="$(_ResolveJsInitializerModuleStaticWebAssetsCachePath)"
>
<Output TaskParameter="Assets" ItemName="_JSModuleStaticWebAsset" />
</DefineStaticWebAssets>
Expand Down Expand Up @@ -404,6 +410,13 @@ Copyright (c) .NET Foundation. All rights reserved.
<_JSFileModuleCandidates Include="@(_JSFileModuleNoneCandidates)" />
</ItemGroup>

<PropertyGroup>
<_ResolveJSModuleStaticWebAssetsRazorCachePath>$(_StaticWebAssetsManifestBase)rjsmrazor.dswa.cache.json</_ResolveJSModuleStaticWebAssetsRazorCachePath>
</PropertyGroup>
<PropertyGroup>
<_ResolveJSModuleStaticWebAssetsCshtmlCachePath>$(_StaticWebAssetsManifestBase)rjsmcshtml.dswa.cache.json</_ResolveJSModuleStaticWebAssetsCshtmlCachePath>
</PropertyGroup>

<!-- Find JS module files -->
<!-- **/*.razor.js -->
<DefineStaticWebAssets
Expand All @@ -413,7 +426,8 @@ Copyright (c) .NET Foundation. All rights reserved.
ContentRoot="$(MSBuildProjectDirectory)"
SourceType="Discovered"
BasePath="$(StaticWebAssetBasePath)"
AssetMergeSource="$(StaticWebAssetMergeTarget)">
AssetMergeSource="$(StaticWebAssetMergeTarget)"
CacheManifestPath="$(_ResolveJSModuleStaticWebAssetsRazorCachePath)">
<Output TaskParameter="Assets" ItemName="_ComponentJSModule" />
</DefineStaticWebAssets>

Expand All @@ -425,7 +439,8 @@ Copyright (c) .NET Foundation. All rights reserved.
ContentRoot="$(MSBuildProjectDirectory)"
SourceType="Discovered"
BasePath="$(StaticWebAssetBasePath)"
AssetMergeSource="$(StaticWebAssetMergeTarget)">
AssetMergeSource="$(StaticWebAssetMergeTarget)"
CacheManifestPath="$(_ResolveJSModuleStaticWebAssetsCshtmlCachePath)">
<Output TaskParameter="Assets" ItemName="_MvcJSModule" />
</DefineStaticWebAssets>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ Copyright (c) .NET Foundation. All rights reserved.
Patterns="@(_CachedBuildStaticWebAssetDiscoveryPatterns)"
ProjectMode="$(StaticWebAssetProjectMode)"
AssetKind="Build"
MakeReferencedAssetOriginalItemSpecAbsolute="$(StaticWebAssetMakeReferencedAssetOriginalItemSpecAbsolute)"
Source="$(PackageId)"
>
<Output TaskParameter="StaticWebAssets" ItemName="_CachedBuildReferencedStaticWebAsset" />
Expand Down Expand Up @@ -227,6 +228,7 @@ Copyright (c) .NET Foundation. All rights reserved.
ProjectMode="$(StaticWebAssetProjectMode)"
AssetKind="Publish"
Source="$(PackageId)"
MakeReferencedAssetOriginalItemSpecAbsolute="$(StaticWebAssetMakeReferencedAssetOriginalItemSpecAbsolute)"
>
<Output TaskParameter="StaticWebAssets" ItemName="_CachedPublishReferencedStaticWebAsset" />
</ComputeReferenceStaticWebAssetItems>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,10 @@ Copyright (c) .NET Foundation. All rights reserved.
BeforeTargets="AssignTargetPaths"
DependsOnTargets="ResolveStaticWebAssetsConfiguration;UpdateExistingPackageStaticWebAssets">

<PropertyGroup>
<_ResolveProjectStaticWebAssetsCachePath>$(_StaticWebAssetsManifestBase)rpswa.dswa.cache.json</_ResolveProjectStaticWebAssetsCachePath>
</PropertyGroup>

<DefineStaticWebAssets
CandidateAssets="@(Content)"
FingerprintCandidates="$(StaticWebAssetsFingerprintContent)"
Expand All @@ -680,15 +684,14 @@ Copyright (c) .NET Foundation. All rights reserved.
SourceId="$(PackageId)"
ContentRoot="$(MSBuildProjectDirectory)\wwwroot\"
BasePath="$(StaticWebAssetBasePath)"
AssetMergeSource="$(StaticWebAssetMergeTarget)">
AssetMergeSource="$(StaticWebAssetMergeTarget)"
CacheManifestPath="$(_ResolveProjectStaticWebAssetsCachePath)">
<Output TaskParameter="Assets" ItemName="StaticWebAsset" />
<Output TaskParameter="AssetDetails" ItemName="_ResolveProjectStaticWebAssetsDetails" />
<Output TaskParameter="Assets" ItemName="_CurrentProjectStaticWebAsset" />
</DefineStaticWebAssets>

<DefineStaticWebAssetEndpoints
CandidateAssets="@(StaticWebAsset)"
AssetFileDetails="@(_ResolveProjectStaticWebAssetsDetails)"
ExistingEndpoints="@(StaticWebAssetEndpoint)"
CandidateAssets="@(_CurrentProjectStaticWebAsset)"
ContentTypeMappings="@(StaticWebAssetContentTypeMapping)"
>
<Output TaskParameter="Endpoints" ItemName="StaticWebAssetEndpoint" />
Expand Down
36 changes: 2 additions & 34 deletions src/StaticWebAssetsSdk/Tasks/ApplyCompressionNegotiation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,11 @@ public class ApplyCompressionNegotiation : Task
[Required]
public ITaskItem[] CandidateAssets { get; set; }

public ITaskItem[] AssetFileDetails { get; set; }

[Output]
public ITaskItem[] UpdatedEndpoints { get; set; }

public Func<string, long> TestResolveFileLength;

private Dictionary<string, ITaskItem> _assetFileDetails;

public override bool Execute()
{
if (AssetFileDetails != null)
{
_assetFileDetails = new(AssetFileDetails.Length, OSPath.PathComparer);
for (int i = 0; i < AssetFileDetails.Length; i++)
{
var item = AssetFileDetails[i];
_assetFileDetails[item.ItemSpec] = item;
}
}

var assetsById = CandidateAssets.Select(StaticWebAsset.FromTaskItem).ToDictionary(a => a.Identity);

var endpointsByAsset = CandidateEndpoints.Select(StaticWebAssetEndpoint.FromTaskItem)
Expand Down Expand Up @@ -213,24 +197,8 @@ public override bool Execute()
return true;
}

private string ResolveQuality(StaticWebAsset compressedAsset)
{
long length;
if(_assetFileDetails != null && _assetFileDetails.TryGetValue(compressedAsset.Identity, out var assetFileDetail))
{
length = long.Parse(assetFileDetail.GetMetadata("FileLength"));
}
else if (TestResolveFileLength != null)
{
length = TestResolveFileLength(compressedAsset.Identity);
}
else
{
length = new FileInfo(compressedAsset.Identity).Length;
}

return Math.Round(1.0 / (length + 1), 12).ToString("F12", CultureInfo.InvariantCulture);
}
private static string ResolveQuality(StaticWebAsset compressedAsset) =>
Math.Round(1.0 / (compressedAsset.FileLength + 1), 12).ToString("F12", CultureInfo.InvariantCulture);

private static bool IsCompatible(StaticWebAssetEndpoint compressedEndpoint, StaticWebAssetEndpoint relatedEndpointCandidate)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System.Security.Cryptography;
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 Expand Up @@ -275,7 +278,7 @@ private bool TryCreateCompressedAsset(StaticWebAsset asset, string outputPath, s
OriginalItemSpec = asset.Identity,
RelatedAsset = asset.Identity,
AssetRole = "Alternative",
AssetTraitName = "Content-Encoding",
AssetTraitName = "Content-Encoding",
AssetTraitValue = assetTraitValue,
ContentRoot = outputPath,
// Set integrity and fingerprint to null so that they get recalculated for the compressed asset.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class ComputeReferenceStaticWebAssetItems : Task

public bool UpdateSourceType { get; set; } = true;

public bool MakeReferencedAssetOriginalItemSpecAbsolute { get; set; }

[Output]
public ITaskItem[] StaticWebAssets { get; set; }

Expand Down Expand Up @@ -60,6 +62,14 @@ public override bool Execute()
if (ShouldIncludeAssetAsReference(selected, out var reason))
{
selected.SourceType = UpdateSourceType ? StaticWebAsset.SourceTypes.Project : selected.SourceType;
if (MakeReferencedAssetOriginalItemSpecAbsolute)
{
selected.OriginalItemSpec = Path.GetFullPath(selected.OriginalItemSpec);
}
else
{
selected.OriginalItemSpec = selected.OriginalItemSpec;
}
resultAssets.Add(selected);
}
Log.LogMessage(MessageImportance.Low, reason);
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
18 changes: 3 additions & 15 deletions src/StaticWebAssetsSdk/Tasks/Data/ContentTypeMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@
// The .NET Foundation licenses this file to you under the MIT license.

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 @@ -24,18 +22,8 @@ internal struct ContentTypeMapping(string mimeType, string cache, string pattern
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;
}
int.Parse(contentTypeMappings.GetMetadata(nameof(Priority)), CultureInfo.InvariantCulture));

private string GetDebuggerDisplay() => $"Pattern: {Pattern}, MimeType: {MimeType}, Cache: {Cache}, Priority: {Priority}";
private readonly string GetDebuggerDisplay() => $"Pattern: {Pattern}, MimeType: {MimeType}, Cache: {Cache}, Priority: {Priority}";
}
}
Loading