postgres/io: pre-allocate buffer capacity and use extend_from_slice i… - #4353
postgres/io: pre-allocate buffer capacity and use extend_from_slice i…#4353Aditya-9-6 wants to merge 2 commits into
Conversation
…n put_length_prefixed Signed-off-by: Aditya <adityadahale96@gmail.com>
Signed-off-by: Aditya <adityadahale96@gmail.com>
|
They both end up in the specialization for Which, first of all, calls And then does a direct This has existed since at least 1.80, much older than our current MSRV: https://doc.rust-lang.org/1.80.0/src/alloc/vec/mod.rs.html#2591 So this is not an optimization, it's just unnecessary diff churn. Just calling |
Summary
This PR optimizes
put_length_prefixedinsqlx-postgres/src/io/buf_mut.rs.Rationale & Performance Benefit
Nearly all Postgres wire protocol messages sent to the database are length-prefixed.
put_length_prefixedreserves 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:
self.reserve(4).self.extend_from_slice(&[0; 4])slice copy instead of array iterator extension.