Repository navigation
perf: stop copying every query param value - #62
Merged
ShawnChen-Sirius merged 1 commit intoSep 30, 2026
Merged
Conversation
QueryParam::encode already returns a Cow that borrows when nothing needs escaping, but EncodedParams then called .to_vec() on every value anyway. So a big text param was copied even though we own the String and could just move it. Params are now consumed with into_encoded(), which moves text that needs no escaping and raw literals straight through. Arrays are written into a single String instead of a String per element, then join, then format!. About 5% faster on an 8MB text param and a 100k element array. Small params are unchanged.
ShawnChen-Sirius
approved these changes
Sep 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
QueryParam::encodereturns aCowso it can borrow when a value doesn't need escaping, butEncodedParamsthen called.to_vec()on every value regardless. That meant a large text param got copied even though we're consuming theQueryParamsand already own theString.Changes:
into_encoded(). Text that needs no escaping andRawvalues are moved through as-is, no copy. Anything that does need escaping gets encoded like before.String. Before, each element got its ownString, then they werejoined, then wrapped withformat!.Numbers from a quick timing loop (release, 20 runs each):
Array(UInt64): ~63ms → ~60msString: ~124ms → ~118msMost of the time is the engine parsing the value, so it's not a huge win, but it drops a full copy of every value on the way in. The encoded output is identical, the existing encoding tests all pass unchanged, and there's a new test that checks plain text and raw values really come back as the same allocation.
Note
Remove per-element string allocation in query param encoding
Rewrites query param value encoding in query_param.rs to write directly into a single output buffer instead of allocating an intermediate
Stringfor each nested element and joining them.QueryParam.encode_arrayand the newwrite_arrayhelper build array literals (brackets, separators, elements) in place, reserving capacity from the element count.write_nestedandwrite_quotedreplace the allocatingencode_nestedandquote_nested_stringhelpers; escaping rules for backslashes, tabs, newlines, and apostrophes are unchanged.QueryParam.into_encodedconsumes the value so unescaped text and raw owned strings move into the encoded result without copying.EncodedParams.encodepreallocates its buffer vectors from the collection length.Macroscope summarized 6d75a1d.