Repository navigation
Avoid Split/ToArray allocations in ContentDispositionHeaderValue MIME decoding - #70182
Conversation
… 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]>
There was a problem hiding this comment.
🟢 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]>
|
Retriggering CI with an empty commit (5616043). The only failure was 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
edd1026 to
5616043
Compare
Co-authored-by: Copilot <[email protected]>
|
Retriggered CI with an empty commit (4035f7a). The previous run failed only Blazor E2E tests: 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) |
|
CI update:
The same pipeline is failing on 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
/review |
Co-authored-by: Copilot <[email protected]>
|
Retriggered CI with an empty commit. The only failing check was 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
Co-authored-by: Copilot <[email protected]>
|
Retriggered CI again. This time CoreCLR and Mono client/WASM passed, and the only failure was 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
CI status for head The failure is unrelated to this change. The diff touches no Components code, 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) |
|
✅ 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
|
Avoid Split/ToArray allocations in ContentDispositionHeaderValue MIME decoding
Avoid
Split/ToArrayallocations when decoding MIME-encoded file names.Description
ContentDispositionHeaderValue.Name/FileNamecallTryDecodeMimeon every get. For any quoted value of 10+ characters (e.g.name="attachment") it ranSplit('?').ToArray(), which allocates a boxed enumerator, a list and an array, even though the value is almost never MIME-encoded.FormFeatureandMultipartReadersections 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 asparts.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, mirroringFormFeature). InProcess, 3 interleaved rounds: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)