Repository navigation
Avoid intermediate allocation when decoding query string names/values - #70120
Conversation
QueryFeature and QueryHelpers decoded each encoded name/value into an intermediate char[] (ReadOnlyMemory) and then copied it into a string. Decode into a stack/pooled buffer and allocate only the final string. Co-authored-by: Copilot <[email protected]>
There was a problem hiding this comment.
🔵 Needs a closer look
The required frozen review bundle, head/base identities, and prior-feedback snapshot are missing, preventing revision-consistency and feedback-deduplication checks.
No open issues 🎉
What changed in this PR
Reduces intermediate allocations when parsing encoded query-string names and values in HttpRequest.Query and QueryHelpers.ParseNullableQuery.
Changes:
- Adds internal string-returning decoding helpers using stack or pooled buffers.
- Updates both query parsing paths to use direct string decoding.
- Preserves the unencoded fast path and existing decoding behavior for non-.NET targets.
| File | Description |
|---|---|
src/Shared/QueryStringEnumerable.cs |
Adds string-returning decoding helpers with a fallback for non-.NET targets. |
src/Http/WebUtilities/src/QueryHelpers.cs |
Uses the new helpers when accumulating query names and values. |
src/Http/Http/src/Features/QueryFeature.cs |
Adds equivalent private decoding helpers for HttpRequest.Query parsing. |
Review effort: Balanced
|
/review |
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
|
Retriggered CI with an empty commit (6d143e6) after an unrelated Failing test: Why it's unrelated to this PR:
The PR diff is unchanged; the new commit is empty. 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
Remove the stack buffer from the query string decode-to-string helpers. Names/values containing only '%' escapes are unescaped directly into a string with Uri.UnescapeDataString(ReadOnlySpan<char>); names/values containing '+' use an ArrayPool buffer that is cleared and returned in a finally block. Add tests covering both paths for short and long inputs. Co-authored-by: Copilot <[email protected]>
Replace the pooled buffer with string.Create for '+' replacement followed by Uri.UnescapeDataString, which returns the same instance when there is nothing to unescape. '%'-only input is unescaped directly from the span. This removes the buffer management entirely and is faster than both main and the previous stack/pooled-buffer version on the plus and encoded benchmarks. Tests cover '%'-only, '+'-only and mixed input at several lengths. Co-authored-by: Copilot <[email protected]>
|
I removed the runtime-sized
This is simpler than the stack/pooled version, and it's faster than that version too. Results vs main (median of 3 interleaved rounds):
The full table is in the PR description. The new tests cover 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
Co-authored-by: Copilot <[email protected]>
|
Investigated the
I pushed an empty commit, cd4a5d2, to rerun CI. 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
DeagleGross
left a comment
There was a problem hiding this comment.
after feedback update the benchmark numbers
Move the string-returning decode into src/Shared/QueryStringDecoder.cs and use it from both QueryFeature and QueryHelpers instead of keeping two copies. Co-authored-by: Copilot <[email protected]>
…ooled buffer QueryStringEnumerable.Decode now calls QueryStringDecoder.Decode, and DecodeToString decodes '+' and percent-escapes in place in a stack (<= 256 chars) or ArrayPool buffer, allocating only the final string. Co-authored-by: Copilot <[email protected]>
|
ASP.NET Core Perf 2 has stopped working on this PR: CI triage is posted and the review feedback is addressed in 6eb03f6. 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
CI triage for 2793e32: the 6 red checks come from two test failures, and neither one is related to the query-string decoding changes in this PR.
All builds passed, including Code check, Source-Build, and Windows/Linux/macOS. Both Helix subsets passed too, including the Http, WebUtilities and Shared tests that cover 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
Move the ReadOnlyMemory<char> decode used by EncodedNameValuePair into QueryStringDecoder.DecodeToMemory so all query decoding helpers live in one type. Co-authored-by: Copilot <[email protected]>
|
CI triage for 6eb03f6: the only red check is Platform tests / Test: Windows Server x64 (which also turns
Could a maintainer rerun the failed Windows Server x64 leg? 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
✅ No documentation PR was created. Confidence that documentation is needed: 5% No docs needed: internal allocation optimization with no public API or observable behavior change. Documentation surfaces considered
|
Decode query string names/values directly to strings
Avoid an intermediate allocation when decoding encoded query keys/values
Description
QueryFeature(HttpRequest.Query) andQueryHelpers.ParseNullableQuerydecode each name/value withQueryStringEnumerable.EncodedNameValuePair.DecodeName()/DecodeValue()and then call.ToString(). When a value contains+or%,Decodeallocates achar[]for the decoded characters, and the caller then copies that into a final string, so each encoded token costs two allocations.This PR adds an internal
QueryStringDecoder(src/Shared/QueryStringDecoder.cs), compiled into each assembly that usesQueryStringEnumerable.cs:Decode(ReadOnlySpan<char>, Span<char>)is the single decode implementation. It replaces+with a space, then runsUri.TryUnescapeDataStringin place.QueryStringEnumerable.Decode(DecodeName()/DecodeValue()) now calls it, and its behavior and allocation are unchanged.DecodeToString(ReadOnlyMemory<char>):chars.ToString(), the same cost as before.stackalloc char[256]buffer, or anArrayPool<char>rental for longer values, and allocates only the final string.QueryFeatureandQueryHelperscallDecodeToString. No unsafe code or public API changes.Benchmarks (latest revision)
These are the existing
QueryCollectionBenchmarksinsrc/Http/Http/perf/Microbenchmarks, plus a newEncodedWithPluscase (?key1=value+%231+value2). "Long" was a local-only run with a 600-char value (ab+%23×100). Environment: AMD EPYC, 2 vCPU Linux VM, .NET 12.0 alpha (repo SDK 12.0.100-alpha.1.26521.103), BenchmarkDotNet--config perflab --warmupCount 3 --iterationCount 15(InProcess, MemoryDiagnoser). The baseline is the merge base4f556f73a6, and "prev" is the previous PR revision b2c8493 (string.Create+Uri.UnescapeDataString). Runs were interleaved (base, prev, new) ×2; times are means of the 2 rounds in ns.QueryString,Single) run the same code path in all three builds (ContainsAny, thenToString) and still moved by up to ±7% between builds. I'm claiming the allocation reduction, not a throughput win.To reproduce:
dotnet run -c Release -- --config perflab --filter '*QueryCollection*'fromsrc/Http/Http/perf/Microbenchmarks.Tests
Microsoft.AspNetCore.Shared.Tests: 1394 passed, 0 failed (includesQueryStringDecoderTestwith stack/pool boundary cases).Microsoft.AspNetCore.Http.Tests: 271 passed, 7 skipped (Windows-only), 0 failed.Microsoft.AspNetCore.WebUtilities.Tests: 519 passed, 0 failed.Microsoft.AspNetCore.Components.Tests(Queryfilter, coversQueryParameterValueSupplierviaDecodeName/DecodeValue): 106 passed, 0 failed.There is no existing issue; this came out of a perf survey of the request parsing paths.
🤖 Posted by ASP.NET Core Perf 2 (AI Agent)