Skip to content

Commit 87e30a6

Browse files
committed
Cleanups
1 parent c6cf9b2 commit 87e30a6

File tree

6 files changed

+125
-124
lines changed

6 files changed

+125
-124
lines changed

src/StaticWebAssetsSdk/Tasks/ApplyCompressionNegotiation.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,9 +365,9 @@ private void ApplyCompressedEndpointHeaders(List<StaticWebAssetEndpointResponseH
365365

366366
private void ApplyRelatedEndpointCandidateHeaders(List<StaticWebAssetEndpointResponseHeader> headers, StaticWebAssetEndpoint relatedEndpointCandidate, HashSet<string> compressedHeaders)
367367
{
368-
StaticWebAssetEndpointResponseHeader.PopulateFromMetadataValue(relatedEndpointCandidate.ResponseHeadersString, _headersList);
368+
StaticWebAssetEndpointResponseHeader.PopulateFromMetadataValue(relatedEndpointCandidate.ResponseHeadersString, _tempHeadersList);
369369

370-
foreach (var header in _headersList)
370+
foreach (var header in _tempHeadersList)
371371
{
372372
// We need to keep the headers that are specific to the compressed asset like Content-Length,
373373
// Last-Modified and ETag. Any other header we should add it.

src/StaticWebAssetsSdk/Tasks/Data/StaticWebAssetEndpointResponseHeader.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public static void PopulateFromMetadataValue(string value, List<StaticWebAssetEn
6060
var actualByteCount = _encoder.GetBytes(value, bytes);
6161
var reader = new Utf8JsonReader(bytes.Slice(0, actualByteCount));
6262
#else
63-
byte[] bytes = rentedBuffer = ArrayPool<byte>.Shared.Rent(maxByteCount);
63+
var bytes = rentedBuffer = ArrayPool<byte>.Shared.Rent(maxByteCount);
6464
var actualByteCount = _encoder.GetBytes(value, 0, value.Length, bytes, 0);
6565
var reader = new Utf8JsonReader(bytes.AsSpan(0, actualByteCount));
6666
#endif
@@ -137,7 +137,7 @@ internal static string ToMetadataValue(
137137
var (buffer, writer) = context;
138138

139139
writer.WriteStartArray();
140-
for (int i = 0; i < headers.Count; i++)
140+
for (var i = 0; i < headers.Count; i++)
141141
{
142142
var header = headers[i];
143143
writer.WriteStartObject();

src/StaticWebAssetsSdk/Tasks/OverrideHtmlAssetPlaceholders.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ public partial class OverrideHtmlAssetPlaceholders : Task
4343

4444
internal static readonly Regex _preloadRegex = new Regex(@"<link\s+rel=""preload""(\s+id=""(?<group>[^""]+)"")?\s*[/]?>");
4545

46+
// Reusable collections to avoid allocations
47+
private readonly List<StaticWebAssetEndpointProperty> _propertiesList = new(10);
48+
private readonly List<StaticWebAssetEndpointSelector> _selectorsList = new(4);
49+
4650
public override bool Execute()
4751
{
4852
var endpoints = StaticWebAssetEndpoint.FromItemGroup(Endpoints).Where(e => e.AssetFile.EndsWith(".js") || e.AssetFile.EndsWith(".mjs"));
@@ -172,12 +176,18 @@ internal List<ResourceAsset> CreateResourcesFromEndpoints(IEnumerable<StaticWebA
172176
foreach (var endpoint in endpoints)
173177
{
174178
// If there's a selector this means that this is an alternative representation for a resource, so skip it.
175-
if (endpoint.Selectors?.Length == 0)
179+
// Use the reusable list to check selectors efficiently
180+
StaticWebAssetEndpointSelector.PopulateFromMetadataValue(endpoint.SelectorsString, _selectorsList);
181+
if (_selectorsList.Count == 0)
176182
{
177183
var resourceAsset = new ResourceAsset(endpoint.Route);
178-
for (var i = 0; i < endpoint.EndpointProperties?.Length; i++)
184+
185+
// Use the reusable list to avoid allocations
186+
StaticWebAssetEndpointProperty.PopulateFromMetadataValue(endpoint.EndpointPropertiesString, _propertiesList);
187+
188+
for (var i = 0; i < _propertiesList.Count; i++)
179189
{
180-
var property = endpoint.EndpointProperties[i];
190+
var property = _propertiesList[i];
181191
if (property.Name.Equals("label", StringComparison.OrdinalIgnoreCase))
182192
{
183193
resourceAsset.Label = property.Value;

src/StaticWebAssetsSdk/Tasks/ResolveFingerprintedStaticWebAssetEndpointsForAssets.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ public class ResolveFingerprintedStaticWebAssetEndpointsForAssets : Task
2323

2424
[Output] public ITaskItem[] ResolvedEndpoints { get; set; }
2525

26+
// Reusable collection to avoid allocations
27+
private readonly List<StaticWebAssetEndpointProperty> _propertiesList = new(10);
28+
2629
public override bool Execute()
2730
{
2831
var candidateEndpoints = StaticWebAssetEndpoint.FromItemGroup(CandidateEndpoints);
@@ -96,11 +99,14 @@ public override bool Execute()
9699
return !Log.HasLoggedErrors;
97100
}
98101

99-
private static bool HasFingerprint(StaticWebAssetEndpoint endpoint)
102+
private bool HasFingerprint(StaticWebAssetEndpoint endpoint)
100103
{
101-
for (var i = 0; i < endpoint.EndpointProperties.Length; i++)
104+
// Use the reusable list to avoid allocations
105+
StaticWebAssetEndpointProperty.PopulateFromMetadataValue(endpoint.EndpointPropertiesString, _propertiesList);
106+
107+
for (var i = 0; i < _propertiesList.Count; i++)
102108
{
103-
var property = endpoint.EndpointProperties[i];
109+
var property = _propertiesList[i];
104110
if (string.Equals(property.Name, "fingerprint", StringComparison.OrdinalIgnoreCase))
105111
{
106112
return true;

0 commit comments

Comments
 (0)