Skip to content

Commit

Permalink
Write messages directly onto message buffer instead of allocating on …
Browse files Browse the repository at this point in the history
…own buffer (#283)

* initial commit

* comment

* fmt
  • Loading branch information
zainkabani authored Jan 17, 2023
1 parent 3f70956 commit ab0bad6
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,9 +493,21 @@ where
}
};

let mut buf = vec![0u8; len as usize - 4];
let mut bytes = BytesMut::with_capacity(len as usize + 1);

bytes.put_u8(code);
bytes.put_i32(len);

bytes.resize(bytes.len() + len as usize - mem::size_of::<i32>(), b'0');

match stream.read_exact(&mut buf).await {
match stream
.read_exact(
&mut bytes[mem::size_of::<u8>() + mem::size_of::<i32>()
..mem::size_of::<u8>() + mem::size_of::<i32>() + len as usize
- mem::size_of::<i32>()],
)
.await
{
Ok(_) => (),
Err(_) => {
return Err(Error::SocketError(format!(
Expand All @@ -505,12 +517,6 @@ where
}
};

let mut bytes = BytesMut::with_capacity(len as usize + 1);

bytes.put_u8(code);
bytes.put_i32(len);
bytes.put_slice(&buf);

Ok(bytes)
}

Expand Down

0 comments on commit ab0bad6

Please sign in to comment.