Skip to content

Commit 8d517d3

Browse files
committed
io: reduce syscalls in poll_write
This applies the same optimization made in #4840 to writes.
1 parent 50795e6 commit 8d517d3

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

tokio/src/io/poll_evented.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,26 @@ feature! {
188188
&'a E: io::Write + 'a,
189189
{
190190
use std::io::Write;
191-
self.registration.poll_write_io(cx, || self.io.as_ref().unwrap().write(buf))
191+
192+
loop {
193+
let evt = ready!(self.registration.poll_write_ready(cx))?;
194+
195+
match self.io.as_ref().unwrap().write(buf) {
196+
Ok(n) => {
197+
// if we write only part of our buffer, this is sufficient on unix to show
198+
// that the socket buffer is full
199+
if n > 0 && (!cfg!(windows) && n < buf.len()) {
200+
self.registration.clear_readiness(evt);
201+
}
202+
203+
return Poll::Ready(Ok(n));
204+
},
205+
Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
206+
self.registration.clear_readiness(evt);
207+
}
208+
Err(e) => return Poll::Ready(Err(e)),
209+
}
210+
}
192211
}
193212

194213
#[cfg(feature = "net")]

0 commit comments

Comments
 (0)