Skip to content

Avoid heap allocations when encoding integer and float parameters#1240

Merged
levkk merged 1 commit into
pgdogdev:mainfrom
crodas:perf/hot-path-string-allocs
Jul 20, 2026
Merged

Avoid heap allocations when encoding integer and float parameters#1240
levkk merged 1 commit into
pgdogdev:mainfrom
crodas:perf/hot-path-string-allocs

Conversation

@crodas

@crodas crodas commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

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

crodas commented Jul 20, 2026

Copy link
Copy Markdown
Contributor Author

Related to #1239

@levkk levkk left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

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
crodas force-pushed the perf/hot-path-string-allocs branch from 79bfb59 to faa7fdf Compare July 20, 2026 15:12
@crodas

crodas commented Jul 20, 2026

Copy link
Copy Markdown
Contributor Author

I just force-pushed; I forgot to sign my commit.

@codecov

codecov Bot commented Jul 20, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 58.33333% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...frontend/router/parser/rewrite/statement/update.rs 50.00% 3 Missing ⚠️
pgdog/src/net/parameter.rs 0.00% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@levkk
levkk merged commit 57a92f0 into pgdogdev:main Jul 20, 2026
44 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants