Avoid heap allocations when encoding integer and float parameters#1240
Merged
Conversation
Contributor
Author
|
Related to #1239 |
The sharding hasher and the query-rewrite paths formatted integers and floats with to_string() only to read the resulting bytes and immediately drop the String. On the per-query and per-message paths this is a wasted heap allocation on every call, since Parameter::new and BytesMut copy the bytes into an owned buffer right after. Format into stack itoa/ryu buffers instead. The bytes fed to the hasher and the wire encoder are unchanged for integers (decimal, no padding), so behavior is identical. While in the sharding hasher, compute the SHA1 shard key directly from the digest bytes instead of formatting the digest to a hex string, slicing the last eight characters, and parsing them back with from_str_radix. The result is the same value, without the allocation or the unwrap.
crodas
force-pushed
the
perf/hot-path-string-allocs
branch
from
July 20, 2026 15:12
79bfb59 to
faa7fdf
Compare
Contributor
Author
|
I just force-pushed; I forgot to sign my commit. |
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
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.
The sharding hasher and the query-rewrite paths formatted integers and floats with to_string() only to read the resulting bytes and immediately drop the String. On the per-query and per-message paths this is a wasted heap allocation on every call, since Parameter::new and BytesMut copy the bytes into an owned buffer right after.
Format into stack itoa/ryu buffers instead. The bytes fed to the hasher and the wire encoder are unchanged for integers (decimal, no padding), so behavior is identical.
While in the sharding hasher, compute the SHA1 shard key directly from the digest bytes instead of formatting the digest to a hex string, slicing the last eight characters, and parsing them back with from_str_radix. The result is the same value, without the allocation or the unwrap.