Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f6c4044

Browse files
committedFeb 27, 2024·
Reuse BytesMut too
1 parent 6a2144a commit f6c4044

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed
 

‎protocol/src/lib.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,9 @@ pub trait RawMessageDiscriminant {
709709
pub struct WireMessageWriter<W, T> {
710710
writer: W,
711711

712+
/// Scratch space for `Message` encoding
713+
scratch: BytesMut,
714+
712715
/// Scratch space for the raw header
713716
header: Vec<u8>,
714717
_phantom: std::marker::PhantomData<T>,
@@ -724,6 +727,7 @@ where
724727
pub fn new(writer: W) -> Self {
725728
Self {
726729
writer,
730+
scratch: BytesMut::new(),
727731
header: vec![],
728732
_phantom: std::marker::PhantomData,
729733
}
@@ -745,10 +749,11 @@ where
745749
let m = m.into();
746750
match m {
747751
WireMessage::Message(m) => {
748-
let mut out = bytes::BytesMut::new();
752+
// Serialize into our local BytesMut, to avoid allocation churn
753+
self.scratch.clear();
749754
let mut e = CrucibleEncoder::new();
750-
e.encode(m, &mut out)?;
751-
self.writer.write_all(&out).await?;
755+
e.encode(m, &mut self.scratch)?;
756+
self.writer.write_all(&self.scratch).await?;
752757
}
753758
WireMessage::RawMessage(m, data) => {
754759
// Manual implementation of CrucibleEncoder, for situations

0 commit comments

Comments
 (0)
Please sign in to comment.