Skip to content

Avoid LINQ and intermediate strings in SetCommaSeparatedValues - #70134

Merged
halter73 merged 1 commit into
mainfrom
perf/set-comma-separated-values
Nov 3, 2026
Merged

halter73 merged 1 commit into
mainfrom
perf/set-comma-separated-values

Conversation

@davidfowl

Copy link
Copy Markdown
Member
  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making.

Build joined header values with a single string allocation.

Description

ParsingHelpers.SetHeaderJoined and AppendHeaderJoined (backing SetCommaSeparatedValues and AppendCommaSeparatedValues) used string.Join(',', values.Select(QuoteIfNeeded)). They now compute the exact length and write the result with a single string.Create, quoting inline.

  • A single value that needs no quoting is stored as-is.
  • Append writes the existing value, a comma and the new values into the same allocation.
  • Output is unchanged.
  • CORS preflight responses use these APIs.
  • Adds SetHeaderJoinedBenchmark.
Benchmark Before After
SetSingleValue 50 ns / 120 B 21 ns / 0 B
SetManyValues (4) 121-149 ns / 184 B 64-77 ns / 64 B
SetValuesNeedingQuotes 82-84 ns / 192 B 36-42 ns / 40 B
AppendValues 130-131 ns / 240 B 97-100 ns / 104 B

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)

Copilot AI balanced review requested due to automatic review settings October 30, 2026 21:16
@github-actions github-actions Bot added the area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions label Oct 30, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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

Copy link
Copy Markdown
Member Author

/azp run aspnetcore-ci, aspnetcore-components-e2e

🤖 Posted by ASP.NET Core Perf 2 (AI Agent)

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

Copy link
Copy Markdown
Member Author

/azp run aspnetcore-components-e2e

🤖 Posted by ASP.NET Core Perf 2 (AI Agent)

@azure-pipelines

Copy link
Copy Markdown
No pipelines are associated with this pull request.

Copy link
Copy Markdown
Member Author

/azp list

🤖 Posted by ASP.NET Core Perf 2 (AI Agent)

@azure-pipelines

Copy link
Copy Markdown
Command 'list

**🤖' is not supported by Azure Pipelines.



Supported commands

  • help:
    • Get descriptions, examples and documentation about supported commands
    • Example: help "command_name"
  • list:
    • List all pipelines for this repository using a comment.
    • Example: "list"
  • run:
    • Run all pipelines or specific pipelines for this repository using a comment. Use this command by itself to trigger all related pipelines, or specify specific pipelines to run.
    • Example: "run" or "run pipeline_name, pipeline_name, pipeline_name"
  • where:
    • Report back the Azure DevOps orgs that are related to this repository and org
    • Example: "where"

See additional documentation.

Copy link
Copy Markdown
Member Author

/azp run aspnetcore-components-e2e,

🤖 Posted by ASP.NET Core Perf 2 (AI Agent)

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@PureWeen

PureWeen commented Nov 1, 2026

Copy link
Copy Markdown
Member

/review

@halter73
halter73 merged commit 83fa83a into main Nov 3, 2026
32 checks passed
@halter73
halter73 deleted the perf/set-comma-separated-values branch November 3, 2026 20:00
@halter73
halter73 temporarily deployed to copilot-pat-pool November 3, 2026 20:00 — with GitHub Actions Inactive
@halter73
halter73 temporarily deployed to copilot-pat-pool November 3, 2026 20:00 — with GitHub Actions Inactive
@halter73
halter73 temporarily deployed to copilot-pat-pool November 3, 2026 20:02 — with GitHub Actions Inactive
@halter73
halter73 temporarily deployed to copilot-pat-pool November 3, 2026 20:04 — with GitHub Actions Inactive
@halter73
halter73 temporarily deployed to copilot-pat-pool November 3, 2026 20:05 — with GitHub Actions Inactive
@halter73
halter73 temporarily deployed to copilot-pat-pool November 3, 2026 20:06 — with GitHub Actions Inactive
@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

✅ 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

  • Conceptual article: Not required — Internal allocation optimization in ParsingHelpers; no public API or observable behavior change.
  • Migration guidance: Not required — No upgrade action needed; output is unchanged.
  • Breaking change: Not required — No compatibility impact; tests pass on original code too.

This branch was previously deployed

1 inactive deployment
copilot-pat-pool — 8f0866e8 Deployed Nov 3, 2026 by halter73 via conclusion #174
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants