mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-17 00:00:11 +02:00
codec: optimize buffer reserve for LengthDelimitedCodec::encode (#8333)
The reservation used `n`, the length after `length_adjustment` has been applied, but the encoder goes on to write `length_field_len` bytes of header plus `data.len()` bytes of payload. A positive `length_adjustment` therefore under-reserved by exactly the adjustment on every frame, and a negative one over-reserved, so the reservation did not match the write in either of the adjusted configurations shown in the module docs.
This commit is contained in:
@@ -631,8 +631,8 @@ impl Encoder<Bytes> for LengthDelimitedCodec {
|
||||
})?;
|
||||
|
||||
// Reserve capacity in the destination buffer to fit the frame and
|
||||
// length field (plus adjustment).
|
||||
dst.reserve(self.builder.length_field_len + n);
|
||||
// length field.
|
||||
dst.reserve(self.builder.length_field_len + data.len());
|
||||
|
||||
if self.builder.length_field_is_big_endian {
|
||||
dst.put_uint(n as u64, self.builder.length_field_len);
|
||||
|
||||
Reference in New Issue
Block a user