Skip to content

Commit

Permalink
io: reduce syscalls in poll_write
Browse files Browse the repository at this point in the history
This applies the same optimization made in #4840 to writes.
  • Loading branch information
Noah-Kennedy committed Sep 2, 2022
1 parent 50795e6 commit bbb96fc
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion tokio/src/io/poll_evented.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,28 @@ feature! {
&'a E: io::Write + 'a,
{
use std::io::Write;
self.registration.poll_write_io(cx, || self.io.as_ref().unwrap().write(buf))

loop {
let evt = ready!(self.registration.poll_write_ready(cx))?;

match self.io.as_ref().unwrap().write(buf) {
Ok(n) => {
// if we read a partially full buffer, this is sufficient on unix to show
// that the socket buffer has been drained
if n > 0 && (!cfg!(windows) && n < buf.len()) {
self.registration.clear_readiness(evt);
}

return Poll::Ready(Ok(n));
},
Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
self.registration.clear_readiness(evt);
}
Err(e) => return Poll::Ready(Err(e)),
}
}

// self.registration.poll_write_io(cx, || self.io.as_ref().unwrap().write(buf))
}

#[cfg(feature = "net")]
Expand Down

0 comments on commit bbb96fc

Please sign in to comment.