Skip to content

Reduce per-request work in StaticAssetsInvoker - #70170

Merged
halter73 merged 2 commits into
mainfrom
perf/staticassets-precomputed-headers
Nov 3, 2026
Merged

halter73 merged 2 commits into
mainfrom
perf/staticassets-precomputed-headers

Conversation

@davidfowl

@davidfowl davidfowl commented Oct 31, 2026 •

Copy link
Copy Markdown
Member

Reduce per-request work in StaticAssetsInvoker

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making.

Cut per-request allocations and CPU in the MapStaticAssets endpoint handler.

Description

StaticAssetsInvoker serves every MapStaticAssets request. Its ETag and Last-Modified values are fixed for each asset, but it formatted them on every request through a freshly allocated typed ResponseHeaders. Revalidation requests (If-None-Match: "<etag>", the most common 304 case) also went through the full EntityTagHeaderValue list parse.

Changes (internal only):

  • The ETag and Last-Modified header strings are formatted once in the constructor and written straight to Response.Headers. The values are the same: EntityTagHeaderValue.ToString() and HeaderUtilities.FormatDate, exactly what the typed setters used.
  • The typed ResponseHeaders wrapper is created only on the range and 416 paths, where Content-Range is set.
  • When If-None-Match is a single value that is ordinally equal to the asset's ETag, the request short-circuits to NotModified without parsing. Every other form (lists, weak tags, whitespace, *, mismatches) uses the existing parser. If-Match is 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 MapStaticAssets endpoint built from a Publish manifest (production mode, so there's no dev-time runtime handler) on a DefaultHttpContext. ContextOnly is the cost of creating the context alone. The table shows 3 runs each on 2 cores, with mean ± error.

Case Before (main) After Δ
HEAD (200) 668–704 ns ±11–13, 1784 B 636–643 ns ±11–13, 1680 B −8% time, −104 B
GET If-None-Match (304) 990–994 ns ±8–18, 2168 B 762–792 ns ±15, 1896 B −21% time, −272 B
GET full body (200) 34.8–35.7 µs, ~19.9 KB 34.8–36.2 µs, ~19.8 KB noise (I/O bound), ≈−100 B
GET Range (206) 35.2–37.0 µs, ~20.55 KB 34.7–38.0 µs, ~20.5 KB noise (I/O bound)
ContextOnly 274–285 ns, 1152 B 255–272 ns, 1152 B baseline

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

  • Microsoft.AspNetCore.StaticAssets.Tests: 72/72 pass.
  • New tests:
    • ResponseHeadersAreConsistentAcrossStatusCodes checks the exact ETag, Last-Modified, Accept-Ranges and Content-Range for 200, HEAD, 304, 206 and 416.
    • IfNoneMatchVariantsAreEvaluated covers exact, weak, padded, list and mismatching If-None-Match values.
    • IfMatchFailureTakesPrecedenceOverMatchingIfNoneMatch checks 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 real MapStaticAssets endpoint built from a publish manifest on a fresh DefaultHttpContext for each operation, and GlobalSetup asserts the 200 and 304 status codes.

Round Benchmark main Mean PR Mean Δ time main Allocated PR Allocated
1 Head200 688.9 ns 648.9 ns -5.8% 1,832 B 1,728 B
1 GetIfNoneMatch304 1,074.4 ns 788.3 ns -26.6% 2,240 B 1,944 B
2 Head200 714.2 ns 652.6 ns -8.6% 1,832 B 1,728 B
2 GetIfNoneMatch304 1,066.6 ns 797.2 ns -25.3% 2,240 B 1,944 B
3 Head200 718.2 ns 670.1 ns -6.7% 1,832 B 1,728 B
3 GetIfNoneMatch304 1,076.6 ns 855.0 ns -20.6% 2,240 B 1,944 B

Result: the If-None-Match revalidation (304) is 21–27% faster with 296 B/op less, and HEAD (200) is 6–9% faster with 104 B/op less, in every round. Both numbers include creating the DefaultHttpContext.

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). main is the PR's merge base 4f556f73a6. PR is the same tree with only this PR's src/**/src changes 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)

- 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]>
Copilot AI balanced review requested due to automatic review settings October 31, 2026 07:24
@github-actions github-actions Bot added the area-middleware Includes: URL rewrite, redirect, response cache/compression, session, and other general middlewares label Oct 31, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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

@PureWeen

PureWeen commented Nov 1, 2026

Copy link
Copy Markdown
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
halter73 merged commit e86aa05 into main Nov 3, 2026
27 checks passed
@halter73
halter73 deleted the perf/staticassets-precomputed-headers branch November 3, 2026 20:04
@halter73
halter73 temporarily deployed to copilot-pat-pool November 3, 2026 20:04 — with GitHub Actions Inactive
@halter73
halter73 temporarily deployed to copilot-pat-pool November 3, 2026 20:05 — with GitHub Actions Inactive
@halter73
halter73 temporarily deployed to copilot-pat-pool November 3, 2026 20:07 — with GitHub Actions Inactive
@halter73
halter73 temporarily deployed to copilot-pat-pool November 3, 2026 20:09 — with GitHub Actions Inactive
@halter73
halter73 temporarily deployed to copilot-pat-pool November 3, 2026 20:10 — with GitHub Actions Inactive
@halter73
halter73 temporarily deployed to copilot-pat-pool November 3, 2026 20:11 — with GitHub Actions Inactive
@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown
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

  • Conceptual article: Not required — Internal-only optimization; no public API, config, or observable behavior change.
  • Migration guidance: Not required — No action needed when upgrading.
  • Breaking change: Not required — Headers, status codes, and logs are unchanged; no compatibility impact.

This branch was previously deployed

1 inactive deployment
copilot-pat-pool — a9fbd501 Deployed Nov 3, 2026 by halter73 via conclusion #181
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-middleware Includes: URL rewrite, redirect, response cache/compression, session, and other general middlewares

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants