Repository navigation
Avoid LINQ and intermediate strings in SetCommaSeparatedValues - #70134
Conversation
Co-authored-by: Copilot <[email protected]>
There was a problem hiding this comment.
🟢 Approval recommended
There are no active review issues blocking the comma-separated header construction optimization.
No open issues 🎉
What changed in this PR
Optimizes comma-separated header construction in ASP.NET Core’s HTTP abstractions, reducing allocations while preserving existing quoting and joining behavior.
Changes:
- Replaces LINQ and intermediate strings with length calculation and direct writing via
string.Create, reusing single unquoted values. - Adds tests for quoting, null and empty values, header removal, and append behavior.
- Adds allocation benchmarks for single-value, multi-value, quoted, and append scenarios.
| File | Description |
|---|---|
| src/Http/Http/test/HeaderDictionaryTests.cs | Adds regression coverage for comma-separated header operations. |
| src/Http/Http.Abstractions/src/Internal/ParsingHelpers.cs | Implements allocation-efficient joining and inline quoting. |
| src/Http/Http.Abstractions/perf/Microbenchmarks/SetHeaderJoinedBenchmark.cs | Adds performance and allocation benchmarks for setting and appending headers. |
Review effort: Balanced
|
/azp run aspnetcore-ci, aspnetcore-components-e2e 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
|
/azp run aspnetcore-components-e2e 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
No pipelines are associated with this pull request. |
|
/azp list 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
Command 'list
**🤖' is not supported by Azure Pipelines.
See additional documentation. |
|
/azp run aspnetcore-components-e2e, 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
|
/review |
|
✅ No documentation PR was created. Confidence that documentation is needed: 3% No docs needed: internal perf refactor with a new benchmark and tests, and no user-visible change. Documentation surfaces considered
|
Build joined header values with a single string allocation.
Description
ParsingHelpers.SetHeaderJoinedandAppendHeaderJoined(backingSetCommaSeparatedValuesandAppendCommaSeparatedValues) usedstring.Join(',', values.Select(QuoteIfNeeded)). They now compute the exact length and write the result with a singlestring.Create, quoting inline.SetHeaderJoinedBenchmark.Tests: Microsoft.AspNetCore.Http.Tests 282 passed, including 25 new Set/Append cases (quoting, nulls, empties, removal, appending to multiple existing values) that also pass on the original code. Cors.Test 160/160.
🤖 Posted by ASP.NET Core Perf 2 (AI Agent)