Skip to content

Commit

Permalink
perf(conn): use AsyncRead::read_buf to try skipping zeroing of buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmonstar committed Jan 23, 2018
1 parent be9d5e8 commit d8b1aa8
Showing 1 changed file with 10 additions and 24 deletions.
34 changes: 10 additions & 24 deletions src/proto/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,35 +94,21 @@ impl<T: AsyncRead + AsyncWrite> Buffered<T> {
pub fn read_from_io(&mut self) -> Poll<usize, io::Error> {
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<B: AsRef<[u8]>>(&mut self, buf: B) -> usize {
Expand Down

0 comments on commit d8b1aa8

Please sign in to comment.