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 d5f1242
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion tokio/src/io/poll_evented.rs
Expand Up @@ -188,7 +188,26 @@ 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 write only part of our 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)),
}
}
}

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

0 comments on commit d5f1242

Please sign in to comment.