From d8b1aa848c190efae12ff6d02369bb272c098964 Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Mon, 22 Jan 2018 17:45:02 -0800 Subject: [PATCH] perf(conn): use AsyncRead::read_buf to try skipping zeroing of buffer --- src/proto/io.rs | 34 ++++++++++------------------------ 1 file changed, 10 insertions(+), 24 deletions(-) diff --git a/src/proto/io.rs b/src/proto/io.rs index 098a5959a5..4e63ba5e4b 100644 --- a/src/proto/io.rs +++ b/src/proto/io.rs @@ -94,35 +94,21 @@ impl Buffered { pub fn read_from_io(&mut self) -> Poll { use bytes::BufMut; self.read_blocked = false; - //TODO: use io.read_buf(), so we don't have to zero memory - //Reason this doesn't use it yet is because benchmarks show the - //slightest **decrease** in performance. Switching should be done - //when it doesn't cost anything. if self.read_buf.remaining_mut() < INIT_BUFFER_SIZE { self.read_buf.reserve(INIT_BUFFER_SIZE); - unsafe { // Zero out unused memory - let buf = self.read_buf.bytes_mut(); - let len = buf.len(); - ptr::write_bytes(buf.as_mut_ptr(), 0, len); - } } - unsafe { - let n = match self.io.read(self.read_buf.bytes_mut()) { - Ok(n) => n, - Err(e) => { - if e.kind() == io::ErrorKind::WouldBlock { - self.read_blocked = true; - return Ok(Async::NotReady); - } - return Err(e) + self.io.read_buf(&mut self.read_buf).map(|ok| { + match ok { + Async::Ready(n) => { + debug!("read {} bytes", n); + Async::Ready(n) + }, + Async::NotReady => { + self.read_blocked = true; + Async::NotReady } - }; - if n > 0 { - debug!("read {} bytes", n); - self.read_buf.advance_mut(n); } - Ok(Async::Ready(n)) - } + }) } pub fn buffer>(&mut self, buf: B) -> usize {