From 3da053c8b46a69db4f88333392b2d2fcf0a90d94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20M=C3=A4rtens?= Date: Tue, 21 Jun 2022 16:57:31 +0200 Subject: [PATCH] switch from `read` to `read_buf` in tcp sink. also always reserve 4 times 1500 as soon as we are under 1500 bytes in order to have less allocations when sending many small msg. Note: maybe upgrading tokio helps against the connection issues, there is some mio related stuff: https://github.com/tokio-rs/tokio/releases/tag/tokio-1.18.0 Note: quick also still has the old read, but its more complicated, thus i only want to change it if its tested --- network/src/channel.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/network/src/channel.rs b/network/src/channel.rs index 51a294bbee..92fb798ec3 100644 --- a/network/src/channel.rs +++ b/network/src/channel.rs @@ -494,13 +494,15 @@ impl UnreliableSink for TcpSink { type DataFormat = BytesMut; async fn recv(&mut self) -> Result> { - self.buffer.resize(1500, 0u8); - match self.half.read(&mut self.buffer).await { + if self.buffer.capacity() < 1500 { + self.buffer.reserve(1500 * 4); // reserve multiple, so that we alloc less often + } + match self.half.read_buf(&mut self.buffer).await { Ok(0) => Err(ProtocolError::Custom(ProtocolsError::Tcp(io::Error::new( io::ErrorKind::BrokenPipe, "read returned 0 bytes", )))), - Ok(n) => Ok(self.buffer.split_to(n)), + Ok(_) => Ok(self.buffer.split()), Err(e) => Err(ProtocolError::Custom(ProtocolsError::Tcp(e))), } }