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:
MildlyMeticulous
2026-08-09 16:47:16 +02:00
committed by GitHub
parent ed25636141
commit d87d860cc8
+2 -2
View File
@@ -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);