Repository navigation
Reduce per-request work in StaticAssetsInvoker - #70170
Merged
Merged
Conversation
- Format the ETag and Last-Modified header values once per asset instead of per request. - Write response headers directly instead of allocating a typed ResponseHeaders wrapper. - Add an ordinal fast path for If-None-Match requests that echo the exact ETag. Co-authored-by: Copilot <[email protected]>
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues remain for the static-asset handler optimizations.
No open issues 🎉
What changed in this PR
Reduces per-request allocations and processing in the MapStaticAssets handler while preserving existing response and conditional-request behavior.
Changes:
- Caches formatted ETag and Last-Modified values and creates typed response headers only for range responses.
- Adds a parsing-free path for an exact single-value If-None-Match match while preserving If-Match precedence.
- Adds tests for response headers, ETag variants, and precondition precedence.
| File | Description |
|---|---|
src/StaticAssets/test/StaticAssetsIntegrationTests.cs |
Adds header consistency and conditional-request regression tests. |
src/StaticAssets/src/StaticAssetsInvoker.cs |
Caches header strings, defers typed response headers, and optimizes exact ETag matching. |
Review effort: Balanced
Member
|
/review |
Invokes the real MapStaticAssets endpoint from a publish manifest for a HEAD 200 and a GET If-None-Match 304 request. Co-authored-by: Copilot <[email protected]>
halter73
approved these changes
Nov 3, 2026
halter73
temporarily deployed
to
copilot-pat-pool
November 3, 2026 20:04 — with
GitHub Actions
Inactive
halter73
temporarily deployed
to
copilot-pat-pool
November 3, 2026 20:05 — with
GitHub Actions
Inactive
halter73
temporarily deployed
to
copilot-pat-pool
November 3, 2026 20:07 — with
GitHub Actions
Inactive
halter73
temporarily deployed
to
copilot-pat-pool
November 3, 2026 20:09 — with
GitHub Actions
Inactive
halter73
temporarily deployed
to
copilot-pat-pool
November 3, 2026 20:10 — with
GitHub Actions
Inactive
halter73
temporarily deployed
to
copilot-pat-pool
November 3, 2026 20:11 — with
GitHub Actions
Inactive
Contributor
|
✅ No documentation PR was created. Confidence that documentation is needed: 3% No docs needed: confident that this internal performance change (plus tests and benchmark project) needs no documentation. Documentation surfaces considered
|
halter73
temporarily deployed
to
copilot-pat-pool
November 3, 2026 20:12 — with
GitHub Actions
Inactive
This branch was previously deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Reduce per-request work in StaticAssetsInvoker
Cut per-request allocations and CPU in the
MapStaticAssetsendpoint handler.Description
StaticAssetsInvokerserves everyMapStaticAssetsrequest. Its ETag and Last-Modified values are fixed for each asset, but it formatted them on every request through a freshly allocated typedResponseHeaders. Revalidation requests (If-None-Match: "<etag>", the most common 304 case) also went through the fullEntityTagHeaderValuelist parse.Changes (internal only):
ETagandLast-Modifiedheader strings are formatted once in the constructor and written straight toResponse.Headers. The values are the same:EntityTagHeaderValue.ToString()andHeaderUtilities.FormatDate, exactly what the typed setters used.ResponseHeaderswrapper is created only on the range and 416 paths, whereContent-Rangeis set.If-None-Matchis a single value that is ordinally equal to the asset's ETag, the request short-circuits toNotModifiedwithout parsing. Every other form (lists, weak tags, whitespace,*, mismatches) uses the existing parser.If-Matchis still evaluated first, so 412 still takes precedence.Headers, status codes and logs are unchanged.
Benchmark
I used a local BenchmarkDotNet harness (not included in this PR). It invokes the real
MapStaticAssetsendpoint built from a Publish manifest (production mode, so there's no dev-time runtime handler) on aDefaultHttpContext.ContextOnlyis the cost of creating the context alone. The table shows 3 runs each on 2 cores, with mean ± error.Subtracting the 1152 B context, the invoker's own allocations drop by 16% on HEAD (632 → 528 B) and by 27% on 304 (1016 → 744 B).
Tests
ResponseHeadersAreConsistentAcrossStatusCodeschecks the exact ETag, Last-Modified, Accept-Ranges and Content-Range for 200, HEAD, 304, 206 and 416.IfNoneMatchVariantsAreEvaluatedcovers exact, weak, padded, list and mismatching If-None-Match values.IfMatchFailureTakesPrecedenceOverMatchingIfNoneMatchchecks that a failing If-Match still returns 412 when If-None-Match matches.Benchmark (3 interleaved rounds)
The numbers above came from a local harness. This PR now includes a benchmark as a new project,
src/StaticAssets/perf/Microbenchmarks(StaticAssetsInvokerBenchmark). It invokes the realMapStaticAssetsendpoint built from a publish manifest on a freshDefaultHttpContextfor each operation, andGlobalSetupasserts the 200 and 304 status codes.Result: the
If-None-Matchrevalidation (304) is 21–27% faster with 296 B/op less, andHEAD(200) is 6–9% faster with 104 B/op less, in every round. Both numbers include creating theDefaultHttpContext.Setup: BenchmarkDotNet v0.13.0
--config perflab(InProcess, MemoryDiagnoser, 3 warmup + 15 measured iterations); AMD EPYC 9V74, 2 vCPU, Ubuntu 24.04 container; .NET SDK 12.0.100-alpha.1.26521.103, .NET 12.0.0 runtime (X64 RyuJIT).mainis the PR's merge base4f556f73a6.PRis the same tree with only this PR'ssrc/**/srcchanges applied. Both builds use the same benchmark source. Run order: main, PR, main, PR, main, PR.🤖 Posted by ASP.NET Core Perf 2 (AI Agent)