Repository navigation
Conversation
…heMiddleware Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🟢 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>
|
/review |
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
CI triage for head 3746e7e: aspnetcore-ci build 1667815 has one failed test, and it's unrelated to this change, which only touches OutputCaching (
All other aspnetcore-ci jobs and aspnetcore-quarantined-pr passed. 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime. |
|
Small benefit |
Avoid per-request policy list, enumerator and tag set allocations in OutputCacheMiddleware
Removes 3-4 allocations from every request that output caching handles.
Description
For each request with an output cache policy,
OutputCacheMiddleware:TryGetRequestPoliciesas aList<IOutputCachePolicy>. That is a list object plus a backing array that grows onAdd.foreachoverIReadOnlyList<IOutputCachePolicy>. Each loop boxes aList<T>.Enumerator, and there are 2 loops per request:CacheRequestAsyncplus eitherServeFromCacheAsync(hit) orServeResponseAsync(miss).This change:
IOutputCachePolicymetadata, then[OutputCache]attribute.foreachloops over the interface with indexedforloops.OutputCacheContext.Tagson first access instead of in the constructor. OnlyTagsPolicyand the store path use it, so requests without tag policies no longer allocate an emptyHashSet<string>. The publicTagsgetter 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 withMemoryOutputCacheStoreand serves a cache hit, including theDefaultHttpContextcreation. Three interleaved A/B rounds: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
TryGetRequestPoliciescombines base, endpoint and attribute policies and their order, that policy tags (0, 1 or 2) still reachIOutputCacheStore.SetAsync, and thatTagsreturns a stable instance. They also pass against main.🤖 Posted by ASP.NET Core Perf 2 (AI Agent)