Repository navigation
Avoid StringBuilder when encoding Content-Disposition file names - #70204
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🔵 Needs a closer look
Missing frozen base/head identities and an existing-feedback snapshot prevented completion of revision-provenance and feedback-deduplication checks.
No open issues 🎉
What changed in this PR
This PR reduces allocations when formatting Content-Disposition download filenames used by HTTP and MVC file results, while preserving the existing encoding behavior.
Changes:
- Replace
StringBuilderencoding with exact-length calculation and direct writes throughstring.Create, plus a fast path for unencoded values. - Use span-based concatenation to quote filenames without boxing.
- Add full-header formatting tests and benchmarks for ASCII filenames and filenames containing spaces.
| File | Description |
|---|---|
| src/Http/Http/perf/Microbenchmarks/ContentDispositionSetFileNameBenchmarks.cs | Adds benchmarks for filename assignment and header serialization. |
| src/Http/Headers/test/ContentDispositionHeaderValueTest.cs | Covers escaping, multibyte UTF-8, and lone-surrogate replacement. |
| src/Http/Headers/src/ContentDispositionHeaderValue.cs | Removes builder-based encoding and boxing during filename quoting. |
Review effort: Balanced
|
/review |
|
I think for bigger perf change we would want to avoid stringSegment completely. Vibe-generated a public API proposal which I'll review and update later: |
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
✅ No documentation PR was created. Confidence that documentation is needed: 3% No docs needed: perf-only refactor (source, tests, benchmark files) with identical output. Documentation surfaces considered
|
Avoid StringBuilder when encoding Content-Disposition file names
Fewer allocations when encoding Content-Disposition filename* values.
Description
ContentDispositionHeaderValue.SetHttpFileNameruns for every file result with a download name (Results.File(..., fileDownloadName: ...), MVCFileResult, viaFileResultHelper). It always computesfilename*withEncode5987. That method built the value with aStringBuilder, appending each percent-encoded byte through an interpolated-string append. Quoting a non-tokenfilenamealso went throughstring.Format(CultureInfo.InvariantCulture, "\"{0}\"", segment), which boxes theStringSegment.Changes
Encode5987: compute the exact output length first. Each non-attr-char rune addsUtf8SequenceLength * 3, and invalid surrogates count as U+FFFD, matchingRune.DecodeFromUtf16. Then:string.Concat("UTF-8''", value);string.Create.EncodeToUtf8Hexis unchanged; it now writes%XXdirectly into the destination span.EncodeAndQuoteMime:string.Concat("\"", value, "\"")replacesstring.Format.The output is identical. New tests assert the full header string for:
They pass on
mainand on this branch. The existingFileNameStar_WhenNeedsEncoding_UsesHexcovers every ASCII byte.Benchmarks
ContentDispositionSetFileNameBenchmarks(new):new ContentDispositionHeaderValue("attachment")+SetHttpFileName(name)+ToString(), the same sequenceFileResultHelperruns. 3 interleaved main/PR rounds:report-2024.pdf)Quarterly Report 2024.pdf)Tests
Microsoft.Net.Http.Headers.Tests: 1828 passed.🤖 Posted by ASP.NET Core Perf 2 (AI Agent)