Skip to content

Avoid per-request policy list, enumerator and tag set allocations in OutputCacheMiddleware - #70199

Closed
davidfowl wants to merge 5 commits into
mainfrom
perf/mw-outputcache-policies
Closed

davidfowl wants to merge 5 commits into
mainfrom
perf/mw-outputcache-policies

Conversation

@davidfowl

@davidfowl davidfowl commented Oct 31, 2026 •

Copy link
Copy Markdown
Member

Avoid per-request policy list, enumerator and tag set allocations in OutputCacheMiddleware

  • 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.

Removes 3-4 allocations from every request that output caching handles.

Description

For each request with an output cache policy, OutputCacheMiddleware:

  • builds the policy list in TryGetRequestPolicies as a List<IOutputCachePolicy>. That is a list object plus a backing array that grows on Add.
  • iterates it with foreach over IReadOnlyList<IOutputCachePolicy>. Each loop boxes a List<T>.Enumerator, and there are 2 loops per request: CacheRequestAsync plus either ServeFromCacheAsync (hit) or ServeResponseAsync (miss).

This change:

  • counts the policies first and allocates a single array of the exact size. The order is unchanged: base policies, then endpoint IOutputCachePolicy metadata, then [OutputCache] attribute.
  • replaces the three foreach loops over the interface with indexed for loops.
  • creates OutputCacheContext.Tags on first access instead of in the constructor. Only TagsPolicy and the store path use it, so requests without tag policies no longer allocate an empty HashSet<string>. The public Tags getter keeps the same shape and semantics: it is never null and returns the same instance on every access.

The list was private to the request and never exposed. There are no behavior, cache key, stored format or header changes, and no public API changes.

Benchmark: new MiddlewareBenchmarks.CacheHit. It runs the full middleware with MemoryOutputCacheStore and serves a cache hit, including the DefaultHttpContext creation. Three interleaved A/B rounds:

BasePolicy Before (main) After
false (endpoint policy only, no tags) 1.817-1.879 us / 3,720 B 1.744-1.792 us / 3,520 B
true (base policy with a tag + endpoint policy) 1.947-2.029 us / 3,832 B 1.829-1.936 us / 3,704 B

This is an allocation win: 200 B fewer per request without tags and 128 B with tags (the set is still needed then). Time improves by about 3-5%, which is close to noise. Byte counts come from runs with SizeUnit.B; the repo's default summary rounds to KB.

Tests: Microsoft.AspNetCore.OutputCaching.Tests, 415/415. New tests check how TryGetRequestPolicies combines base, endpoint and attribute policies and their order, that policy tags (0, 1 or 2) still reach IOutputCacheStore.SetAsync, and that Tags returns a stable instance. They also pass against main.

🤖 Posted by ASP.NET Core Perf 2 (AI Agent)

davidfowl and others added 2 commits October 31, 2026 16:56
…heMiddleware

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 31, 2026 17:06
@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
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

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 block the output-caching allocation improvements.

No open issues 🎉

What changed in this PR

Reduces per-request allocations in output caching while preserving policy selection and execution order.

Changes:

  • Replaces the temporary policy list with an exact-sized array and uses indexed loops to avoid enumerator boxing.
  • Adds tests for policy-source combinations and ordering.
  • Adds a cache-hit benchmark with and without a base policy.
File Description
src/​Middleware/​OutputCaching/​test/​OutputCacheMiddlewareTests.cs Tests policy combinations, counts, and ordering.
src/​Middleware/​OutputCaching/​src/​OutputCacheMiddleware.cs Uses an exact-sized policy array and indexed iteration.
src/​Middleware/​OutputCaching/​perf/​Microbenchmarks/​MiddlewareBenchmarks.cs Measures middleware cache-hit performance and allocations.

Review effort: Balanced

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@davidfowl davidfowl changed the title Avoid per-request policy list and enumerator allocations in OutputCacheMiddleware Avoid per-request policy list, enumerator and tag set allocations in OutputCacheMiddleware Oct 31, 2026
@PureWeen

PureWeen commented Nov 1, 2026

Copy link
Copy Markdown
Member

/review

@davidfowl davidfowl added the agent:working An AI agent is actively working on this issue/PR label Nov 6, 2026 — with Copilot SWE Agent
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@davidfowl davidfowl removed the agent:working An AI agent is actively working on this issue/PR label Nov 6, 2026
@davidfowl davidfowl added the agent:working An AI agent is actively working on this issue/PR label Nov 6, 2026 — with Copilot SWE Agent

Copy link
Copy Markdown
Member Author

CI triage for head 3746e7e: aspnetcore-ci build 1667815 has one failed test, and it's unrelated to this change, which only touches OutputCaching (OutputCacheMiddleware, OutputCacheContext, tests and a benchmark).

All other aspnetcore-ci jobs and aspnetcore-quarantined-pr passed.

🤖 Posted by ASP.NET Core Perf 2 (AI Agent)

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime.
To make sure no conflicting changes have occurred, please rerun validation before merging. You can do this by leaving an /azp run comment here (requires commit rights), or by simply closing and reopening.

@dotnet-policy-service dotnet-policy-service Bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Nov 13, 2026
@davidfowl

Copy link
Copy Markdown
Member Author

Small benefit

@davidfowl davidfowl closed this Nov 16, 2026
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 pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants