@@ -709,6 +709,9 @@ pub trait RawMessageDiscriminant {
709
709
pub struct WireMessageWriter < W , T > {
710
710
writer : W ,
711
711
712
+ /// Scratch space for `Message` encoding
713
+ scratch : BytesMut ,
714
+
712
715
/// Scratch space for the raw header
713
716
header : Vec < u8 > ,
714
717
_phantom : std:: marker:: PhantomData < T > ,
@@ -724,6 +727,7 @@ where
724
727
pub fn new ( writer : W ) -> Self {
725
728
Self {
726
729
writer,
730
+ scratch : BytesMut :: new ( ) ,
727
731
header : vec ! [ ] ,
728
732
_phantom : std:: marker:: PhantomData ,
729
733
}
@@ -745,10 +749,11 @@ where
745
749
let m = m. into ( ) ;
746
750
match m {
747
751
WireMessage :: Message ( m) => {
748
- let mut out = bytes:: BytesMut :: new ( ) ;
752
+ // Serialize into our local BytesMut, to avoid allocation churn
753
+ self . scratch . clear ( ) ;
749
754
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 ?;
752
757
}
753
758
WireMessage :: RawMessage ( m, data) => {
754
759
// Manual implementation of CrucibleEncoder, for situations
0 commit comments