Repository navigation
Conversation
- Skip parsing and the List allocation in DefaultOutputFormatterSelector when there is no Accept header. - Avoid boxing the Accept/Accept-Charset StringValues to IList<string>. - Pre-size the parsed accept list from the number of comma separated entries. - Use indexed loops over IList<IOutputFormatter> to avoid boxed enumerators. - Add ObjectResultExecutorBenchmark. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🟡 Changes recommended
Unbounded capacity estimation can inflate allocations for tolerated headers, and alternating response-body feature reuse makes the benchmark’s per-response allocation measurements inconsistent.
2 open issues
What changed in this PR
Reduces allocations in MVC output formatter content negotiation while aiming to preserve formatter selection behavior.
Changes:
- Avoids allocations for missing headers and boxing when parsing
StringValues. - Pre-sizes Accept-header storage and replaces enumerator-based loops with indexed loops.
- Adds parser and selector tests plus an end-to-end object-result execution benchmark.
| File | Description |
|---|---|
src/Mvc/perf/Microbenchmarks/Microsoft.AspNetCore.Mvc/ObjectResultExecutorBenchmark.cs |
Adds benchmarks for three Accept-header scenarios. |
src/Mvc/Mvc.Core/test/Infrastructure/DefaultOutputFormatterSelectorTest.cs |
Tests multi-value quality ordering and empty-header fallback. |
src/Mvc/Mvc.Core/test/Formatters/AcceptHeaderParserTest.cs |
Tests the StringValues overload with multiple, null, and empty values. |
src/Mvc/Mvc.Core/src/Infrastructure/DefaultOutputFormatterSelector.cs |
Optimizes media-type storage and formatter iteration. |
src/Mvc/Mvc.Core/src/Formatters/TextOutputFormatter.cs |
Returns an empty array when Accept-Charset is absent. |
src/Mvc/Mvc.Core/src/Formatters/AcceptHeaderParser.cs |
Adds a boxing-free overload sharing the existing parsing logic. |
Review effort: Balanced
…n benchmark Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
CI failure is unrelated to this change: the only failing test was 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
The re-run failed on another unrelated test: 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
/review |
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime. |

Reduce allocations in output formatter content negotiation
Remove small allocations from output formatter content negotiation
Description
Every
ObjectResult/OkObjectResultresponse goes throughDefaultOutputFormatterSelector. This PR removes a few small allocations from that path.DefaultOutputFormatterSelector.GetAcceptableMediaTypes: returnArray.Emptywhen there is noAcceptheader, so we no longer allocate aList<MediaTypeSegmentWithQuality>for it. When there is a header, the list is pre-sized from the number of comma-separated entries, so a singleapplication/jsonno longer allocates a 4-element backing array.AcceptHeaderParser: add an internalStringValuesoverload so the header isn't boxed toIList<string>. The per-value parsing loop is shared with the existing overload and is unchanged.TextOutputFormatter.GetAcceptCharsetHeaderValues: returnArray.Emptywhen there is noAccept-Charsetheader, which avoids boxing theStringValues.SelectFormatterNotUsingContentType/SelectFormatterUsingAnyAcceptableContentType: use indexed loops overIList<IOutputFormatter>andMediaTypeCollectioninstead offoreach, which boxed the enumerator.No public API or behavior changes. Formatter selection order, sorting and the selected content type are the same.
Results
I added
ObjectResultExecutorBenchmark, which runsObjectResultExecutor.ExecuteAsyncwith the HttpNoContent, String, Stream and SystemTextJson formatters. These are local BenchmarkDotNet runs (--config profile, 15 iterations) on a noisy 2-vCPU VM, with 3 interleaved A/B rounds. Each cell shows mean ± error.application/jsonapplication/jsonapplication/jsontext/html,...,*/*;q=0.8text/html,...,*/*;q=0.8text/html,...,*/*;q=0.8The numbers are re-measured after the review feedback (bounded initial capacity; the benchmark now uses a fresh response body feature per invocation, which adds a constant ~216 B to both sides). Round 2 ran in reverse order (new first). Time is neutral within noise: round 1's base sample for the no-Accept case was an outlier that didn't reproduce.
Allocations drop by 40–72 B per response in every case. Time is neutral. Most of the remaining allocation comes from the benchmark harness (
StreamResponseBodyFeature/StreamPipeWriter) and fromUtf8JsonWriter, not from negotiation.Tests
AcceptHeaderParserTestcases for theStringValuesoverload: multiple values, including empty and null entries, andStringValues.Empty.DefaultOutputFormatterSelectorTestcases for multipleAcceptheader values sorted by quality across values, and for an emptyAcceptheader value.Microsoft.AspNetCore.Mvc.Core.Test: 4514 passed, 0 failed, 3 skipped.🤖 Posted by ASP.NET Core Perf 2 (AI Agent)