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>
Contributor
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
This branch has not been 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.
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)