Repository navigation
Conversation
Build the single name/value overload with string.Create and use an ArrayPool-backed buffer instead of StringBuilder for the enumerable overloads. Output is unchanged. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues were identified for the allocation-reduction changes to QueryHelpers.AddQueryString.
No open issues 🎉
What changed in this PR
Reduces allocations in QueryHelpers.AddQueryString, which is used to construct URLs for authentication redirects and other HTTP scenarios, without changing its public API.
Changes:
- Uses
string.Createfor single-pair calls and a pooled character buffer for enumerable calls. - Adds reference-comparison tests covering encoding, fragments, null values, and buffer growth.
- Adds allocation and timing benchmarks for simple queries and authentication URLs.
| File | Description |
|---|---|
| src/Http/WebUtilities/test/QueryHelpersTests.cs | Adds compatibility tests against the previous algorithm. |
| src/Http/WebUtilities/src/QueryHelpers.cs | Replaces StringBuilder with direct string creation and pooled buffering. |
| src/Http/WebUtilities/perf/Microbenchmarks/QueryHelpersAddQueryStringBenchmark.cs | Adds single-pair and OAuth dictionary benchmarks. |
Review effort: Balanced
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
/review |
Query values may contain sensitive data, so clear the used portion of the rented buffer before returning it, both on Dispose and when growing. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
Retriggering CI once for an unrelated test failure on c17c895. Failing test: Why it's unrelated: this PR only changes Commits: my first 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
The Failing test: Why it's unrelated
I didn't retrigger CI because main E2E is currently flaky. A maintainer can rerun the failed job if needed. 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime. |
|
ASP.NET Core Perf 2 has stopped: @DeagleGross's review is addressed in 70459f6, with the A/B results in the threads and the PR description. 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
Build the result in a ValueStringBuilder that starts on a 256-char stackalloc buffer and falls back to ArrayPool, and encode names and values in place with UrlEncoder.Encode(ReadOnlySpan<char>, Span<char>, ...) so the result string is the only allocation. Growth is handled locally so pooled buffers are cleared before they are returned. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
DeagleGross
left a comment
There was a problem hiding this comment.
ValueStringBuilder doesn't clear its pooled arrays, which is why this needs the local Append, Grow and ReturnRentedBuffer helpers and the ref char[]? rentedBuffer parameters. RefPooledArrayBufferWriter<char> (src/Shared/Buffers) starts on a stackalloc span, grows through ArrayPool, and clears on grow and Dispose, so I think it removes all of that. The encode loop becomes Encode(source, writer.GetSpan(source.Length), out consumed, out written, isFinalBlock: true), then writer.Advance(written), and on DestinationTooSmall slice the source by consumed and loop.
can u try use that instead and re-run the benchmarks?
Merge assessmentVerdict: Merge after DeagleGross's
Reasons to merge
Reasons not to merge yet
What would change this verdict: a reply to DeagleGross with re-run numbers and a green CI run would make this Lean merge. 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
/review |
Reduce allocations in QueryHelpers.AddQueryString
Fewer allocations and faster QueryHelpers.AddQueryString.
Description
QueryHelpers.AddQueryStringbuilds its result with a default-capacityStringBuilder, which grows chunk by chunk. The single name/value overload also wraps the pair in an array and goes throughIEnumerableenumeration. The single-pair overload runs on hot paths such as cookie authentication login/access-denied redirects (ReturnUrl), and the dictionary overload runs when building OAuth/OIDC challenge URLs.Changes (all internal to
QueryHelpers, no public API change):ValueStringBuilder, starting onstackalloc char[256]and moving toArrayPool<char>for longer output. Names and values are encoded in place with the span-basedUrlEncoder.Encode(ReadOnlySpan<char>, Span<char>, ...), so the result string is the only allocation. Growth is done locally, so pooled buffers are cleared before they're returned.AddQueryString(string uri, string name, string value)no longer wraps the pair in an array and enumerates it. The separator is still decided exactly as before (is there a?before the first#).The output is identical: same separators, same
UrlEncoder.Defaultencoding,nullvalues skipped, anchor preserved.Benchmark
QueryHelpersAddQueryStringBenchmark(added): BenchmarkDotNet InProcess (--config perflab, 3 warmup / 15 iterations), 3 interleaved rounds, mean of the rounds. "Previous" is the earlierstring.Create/Encode(string)revision of this PR (cfd749c).ReturnUrl(single pair, value needs encoding)Simple(single pair, no encoding)LongValue(602-char value, no encoding)EncodeHeavy(short non-ASCII/reserved value)OAuth(7-entry dictionary)Percentages are relative to main. Compared with the previous revision, the span-based encoder saves allocations whenever something gets encoded, but it's slower when nothing does, because
Encode(string)returns the input instance without copying.Tests
Added
AddQueryStringMatchesReferenceImplementation, which compares both the single-pair and the enumerable overloads against the previousStringBuilderalgorithm. Cases cover empty URIs, a trailing?,#inside the anchor and multiple#, a?inside the fragment, non-ASCII and surrogate pairs, characters that need encoding,nullvalues, 1,400-char keys and values (longer once encoded) that force the pooled buffer to grow, lone surrogates, a 300-char value that expands 9× when encoded (several regrowths), surrogate pairs straddling the 256-char stack buffer, and a URI longer than the stack buffer. A mutation that skips regrowth afterDestinationTooSmallfails all 12 cases. All 518 WebUtilities tests pass.🤖 Posted by ASP.NET Core Perf 2 (AI Agent)