Repository navigation
Conversation
The header arrays were rented from ArrayPool but never returned (the entry's Dispose is a no-op), so every cache hit and every stored response allocated a fresh pool-sized array (minimum 16 elements). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🟢 Approval recommended
There are no active review issues for the header-array allocation changes.
No open issues 🎉
What changed in this PR
This PR reduces output-cache header allocation overhead by replacing pooled arrays that were not returned with directly allocated arrays, while preserving serialization and response-header behavior.
Changes:
- Allocate header arrays using the requested count during deserialization and response storage.
- Remove pool-return handling and simplify header-memory assignment.
| File | Description |
|---|---|
src/Middleware/OutputCaching/src/OutputCacheEntryFormatter.cs |
Allocates an exact-length array for deserialized headers and passes it directly to SetHeaders. |
src/Middleware/OutputCaching/src/OutputCacheEntry.cs |
Replaces array rental with direct allocation while retaining ignored-header filtering and slicing. |
Review effort: Balanced
|
/review |
|
Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime. |
Merge assessmentVerdict: Lean merge. It is a 16-line change that drops an
Reasons to merge
Reasons not to merge yet
What would change this verdict: A plan to make 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
Use exact-size header arrays for output cache entries
Allocate output cache header arrays at their exact size instead of renting them.
Description
OutputCacheEntryFormatter.Deserialize(every cache hit) andOutputCacheEntry.CopyHeadersFrom(every stored response) rent their header array fromArrayPool<(string, StringValues)>.Shared, but the arrays are never returned:OutputCacheEntry.Disposeis a no-op, and the entry keeps the array. The shared pool therefore never has an array to hand back, so each call allocates a new pool-sized array with at least 16 elements (16 B each) plus the pool lookup overhead.This change allocates an array of exactly the needed length. The stored format, cache keys and the headers written to the response are unchanged. Because nothing is rented, no array can be shared between entries.
Benchmark: the existing
EndToEndBenchmarks.ReadAsync, which deserializes a cached entry with 6 headers. Three interleaved A/B rounds:The store path (
CopyHeadersFrom) follows the same pattern: before, it rented a 16-element array for the 7 response headers; now it allocates a 7-element array.Tests: Microsoft.AspNetCore.OutputCaching.Tests, 403/403. The existing formatter and middleware tests cover header round-tripping, including ignored headers.
🤖 Posted by ASP.NET Core Perf 2 (AI Agent)