Skip to content

postgres/io: pre-allocate buffer capacity and use extend_from_slice i… - #4353

Open
Aditya-9-6 wants to merge 2 commits into
transact-rs:mainfrom
Aditya-9-6:feature/postgres-buffer-reserve-capacity
Open

postgres/io: pre-allocate buffer capacity and use extend_from_slice i…#4353
Aditya-9-6 wants to merge 2 commits into
transact-rs:mainfrom
Aditya-9-6:feature/postgres-buffer-reserve-capacity

Conversation

@Aditya-9-6

Copy link
Copy Markdown

Summary

This PR optimizes put_length_prefixed in sqlx-postgres/src/io/buf_mut.rs.

Rationale & Performance Benefit

Nearly all Postgres wire protocol messages sent to the database are length-prefixed. put_length_prefixed reserves the 4-byte length header at the beginning of each message.

Currently, it calls self.extend(&[0; 4]), which iterates over array elements and may trigger intermediate re-allocations if capacity is tight.

Optimizations made:

  • Pre-allocates space upfront with self.reserve(4).
  • Uses zero-copy self.extend_from_slice(&[0; 4]) slice copy instead of array iterator extension.
  • Streamlines database wire protocol encoding for high-throughput query pipelines.

…n put_length_prefixed

Signed-off-by: Aditya <adityadahale96@gmail.com>
Signed-off-by: Aditya <adityadahale96@gmail.com>
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.

1 participant