Skip to content

Avoid Split/ToArray allocations in ContentDispositionHeaderValue MIME decoding - #70182

Merged
DeagleGross merged 5 commits into
mainfrom
perf/http-contentdisposition-mime
Nov 8, 2026
Merged

DeagleGross merged 5 commits into
mainfrom
perf/http-contentdisposition-mime

Conversation

@davidfowl

@davidfowl davidfowl commented Oct 31, 2026 •

Copy link
Copy Markdown
Member

Avoid Split/ToArray allocations in ContentDispositionHeaderValue MIME decoding

  • 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. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Avoid Split/ToArray allocations when decoding MIME-encoded file names.

Description

ContentDispositionHeaderValue.Name / FileName call TryDecodeMime on every get. For any quoted value of 10+ characters (e.g. name="attachment") it ran Split('?').ToArray(), which allocates a boxed enumerator, a list and an array, even though the value is almost never MIME-encoded. FormFeature and MultipartReader sections call these getters 3–4 times per section (IsFileDisposition, IsFormDisposition, Name, FileName).

This replaces the split with a span-based shape check: a "=? prefix, a ?=" suffix and exactly two inner ?. It accepts exactly the same inputs as parts.Length == 5 && parts[0] == "\"=" && parts[4] == "=\"". The decode path (Encoding.GetEncoding, Convert.FromBase64String) is unchanged. No public API or behavior change.

Benchmark

New ContentDispositionParseBenchmarks (TryParse + IsFileDisposition/IsFormDisposition + Name/FileName, mirroring FormFeature). InProcess, 3 interleaved rounds:

Round FormDataSection main PR FileSection main PR
1 256.1 ns / 408 B 204.8 ns / 264 B 489.0 ns / 888 B 396.5 ns / 456 B
2 269.0 ns / 408 B 200.8 ns / 264 B 473.6 ns / 888 B 337.3 ns / 456 B
3 251.2 ns / 408 B 207.7 ns / 264 B 483.5 ns / 888 B 339.1 ns / 456 B

Form field: ~20% faster, −35% allocations. File section: ~25% faster, −49% allocations.

Tests

Added FileName_MimeEncodedVariants_DecodedOrReturnedUnquoted, which covers near-miss shapes: too many or too few separators, bad prefix or suffix, Q encoding, empty name or data, and invalid base64. It passes on both main and this PR. All 1832 Headers tests pass.

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

… decoding

TryDecodeMime ran on every Name/FileName getter for quoted values and allocated
via Split('?').ToArray() even when the value was not MIME encoded. Replace with
a span-based shape check that accepts exactly the same inputs.

Co-authored-by: Copilot <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 31, 2026 15:32
@github-actions github-actions Bot added the area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions label Oct 31, 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

No active review issues were identified for the MIME decoding allocation-reduction change.

No open issues 🎉

What changed in this PR

Reduces allocations when reading ContentDispositionHeaderValue.Name and FileName, improving multipart form processing without changing the public API or intended decoding behavior.

Changes:

  • Replaces MIME decoding’s Split().ToArray() shape check with span-based parsing.
  • Adds tests for valid encodings, malformed shapes, and ordinary quoted filenames.
  • Adds benchmarks for form-field and file-section parsing and property access.
File Description
src/​Http/​Http/​perf/​Microbenchmarks/​ContentDispositionParseBenchmarks.cs Adds benchmarks modeling multipart section processing.
src/​Http/​Headers/​test/​ContentDispositionHeaderValueTest.cs Adds MIME decoding and fallback regression cases.
src/​Http/​Headers/​src/​ContentDispositionHeaderValue.cs Uses span-based MIME shape validation to avoid split allocations.

Review effort: Balanced

Co-authored-by: Copilot <[email protected]>

Copy link
Copy Markdown
Member Author

Retriggering CI with an empty commit (5616043). The only failure was aspnetcore-components-e2e / Blazor E2E tests on Linux (Mono client and WebAssembly) (4 of 3,803 tests, build 1657791). This PR only changes server-side header and cookie parsing in src/Http, and in the same time window the same leg failed on unrelated PRs #70184 (Kestrel) and #70185 (SignalR). Every other pipeline, including aspnetcore-ci with the Http tests, passed.

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

@davidfowl
davidfowl requested review from a team, SamMonoRT and wtgodbe as code owners October 31, 2026 18:14
@davidfowl
davidfowl force-pushed the perf/http-contentdisposition-mime branch from edd1026 to 5616043 Compare October 31, 2026 18:15

Copy link
Copy Markdown
Member Author

Retriggered CI with an empty commit (4035f7a). The previous run failed only Blazor E2E tests: ServerRoutingTest.NavigationLock_CanBlockExternalNavigation (Build Analysis reports a 27.2%% failure rate) and NavigationLock_CanBlockNavigation_ThenContinue. Both are Selenium navigation assertions in Components; this PR only changes ContentDispositionHeaderValue MIME decoding in Http/Headers.

Note: a malformed retrigger commit (edd1026) was pushed by mistake for under a minute and force-reverted to 5616043 before the empty commit was added. The PR diff is unchanged (3 files).

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

Copy link
Copy Markdown
Member Author

CI update: aspnetcore-ci passed. Only aspnetcore-components-e2e failed, on Blazor Selenium tests unrelated to this Http/Headers change:

  • VirtualizationTest.VariableHeight_HtmlTable_DoesNotProduceNestedTrElements
  • RedirectionTest.NavigationException_InAsyncContext_DoesNotBecomeUnobservedTaskException

The same pipeline is failing on main in 4 of the last 5 commits (b7d01de, 23ab95b, fe077f0, f00e4eb), so I'm not pushing another empty commit; that would mostly add CI load. I'm happy to retrigger once the pipeline is stable again.

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

@PureWeen

PureWeen commented Nov 1, 2026

Copy link
Copy Markdown
Member

/review

Co-authored-by: Copilot <[email protected]>

Copy link
Copy Markdown
Member Author

Retriggered CI with an empty commit. The only failing check was aspnetcore-components-e2e, leg Blazor E2E tests on Linux (CoreCLR), with one failing test: VirtualizationTest.VariableHeight_HtmlTable_DoesNotProduceNestedTrElements (a WaitAssert timeout). That leg passed on main at f7d14ca. This PR only changes ContentDispositionHeaderValue in src/Http/Headers, plus its test and a benchmark, so the Blazor virtualization failure is unrelated to the diff.

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

Co-authored-by: Copilot <[email protected]>

Copy link
Copy Markdown
Member Author

Retriggered CI again. This time CoreCLR and Mono client/WASM passed, and the only failure was aspnetcore-components-e2e, leg Blazor E2E tests on Linux (Mono server), with one test: ServerRoutingTest.NavigationLock_CanBlockExternalNavigation. That is the same NavigationLock flake this PR hit earlier. The Mono server leg also failed on main at f7d14ca. The test covers Blazor server navigation and is unrelated to this diff, which only touches ContentDispositionHeaderValue.

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

Copy link
Copy Markdown
Member Author

CI status for head 7eca5c8de2: the only failing test is Microsoft.AspNetCore.Components.E2ETest.Tests.VirtualizationTest.VariableHeight_HtmlTable_DoesNotProduceNestedTrElements in aspnetcore-components-e2e (Blazor E2E on Linux, Mono client and WebAssembly). Build Analysis puts its historical failure rate at 13.50%. Build Analysis and Build Insights are red only because of this test.

The failure is unrelated to this change. The diff touches no Components code, only:

  • src/Http/Headers/src/ContentDispositionHeaderValue.cs
  • src/Http/Headers/test/ContentDispositionHeaderValueTest.cs
  • src/Http/Http/perf/Microbenchmarks/ContentDispositionParseBenchmarks.cs (benchmark only)

The components-e2e legs on main show the same intermittent failures. Because this test is known to be flaky, we don't plan another retrigger.

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

@davidfowl davidfowl added the agent:working An AI agent is actively working on this issue/PR label Nov 6, 2026 — with Copilot SWE Agent
@davidfowl davidfowl removed the agent:working An AI agent is actively working on this issue/PR label Nov 6, 2026
@DeagleGross
DeagleGross enabled auto-merge (squash) November 7, 2026 23:05
@DeagleGross
DeagleGross merged commit 6394b4f into main Nov 8, 2026
37 checks passed
@DeagleGross
DeagleGross deleted the perf/http-contentdisposition-mime branch November 8, 2026 00:09
@github-actions

github-actions Bot commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

✅ No documentation PR was created.

Confidence that documentation is needed: 3%

No docs needed: internal perf refactoring with tests and a benchmark only.

Documentation surfaces considered

  • Conceptual article: Not required — Internal allocation optimization of MIME decoding; same accepted inputs, no public API or behavior change.
  • Migration guidance: Not required — No user action needed when upgrading.
  • Breaking change: Not required — No compatibility impact; no signals found.

This branch was previously deployed

1 inactive deployment
copilot-pat-pool — 7eca5c8d Deployed Nov 8, 2026 by DeagleGross via conclusion #237
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