Skip to content

Commit

Permalink
io: simplify poll_write_vectored for BufWriter
Browse files Browse the repository at this point in the history
Simplify branching and do what poll_write does: do not buffer
slices partially, optimizing for the most likely case where slices
are much smaller than the buffer, while retaining special treatment
for oversized slices.
  • Loading branch information
mzabaluev committed Nov 22, 2020
1 parent 02ec803 commit d34f468
Showing 1 changed file with 9 additions and 18 deletions.
27 changes: 9 additions & 18 deletions tokio/src/io/util/buf_writer.rs
Expand Up @@ -2,7 +2,6 @@ use crate::io::util::DEFAULT_BUF_SIZE;
use crate::io::{AsyncBufRead, AsyncRead, AsyncWrite, ReadBuf};

use pin_project_lite::pin_project;
use std::cmp;
use std::fmt;
use std::io::{self, IoSlice, Write};
use std::pin::Pin;
Expand Down Expand Up @@ -152,9 +151,8 @@ impl<W: AsyncWrite> AsyncWrite for BufWriter<W> {
Poll::Ready(Ok(total_len))
}
} else {
let mut total_written = 0;
let mut iter = bufs.iter();
if let Some(buf) = iter.by_ref().find(|&buf| !buf.is_empty()) {
let mut total_written = if let Some(buf) = iter.by_ref().find(|&buf| !buf.is_empty()) {
// This is the first non-empty slice to write, so if it does
// not fit in the buffer, we still get to flush and proceed.
if self.buf.len() + buf.len() > self.buf.capacity() {
Expand All @@ -167,26 +165,19 @@ impl<W: AsyncWrite> AsyncWrite for BufWriter<W> {
return me.inner.poll_write(cx, buf);
} else {
me.buf.extend_from_slice(buf);
total_written += buf.len();
buf.len()
}
debug_assert!(total_written != 0);
}
} else {
return Poll::Ready(Ok(0));
};
debug_assert!(total_written != 0);
for buf in iter {
let me = self.as_mut().project();
if buf.len() >= me.buf.capacity() {
// This slice should be written directly, but we have already
// buffered some of the input. Bail out, expecting it to be
// handled as the first slice in the next call to
// poll_write_vectored.
if me.buf.len() + buf.len() > me.buf.capacity() {
break;
} else {
let write_to = cmp::min(buf.len(), me.buf.capacity() - me.buf.len());
me.buf.extend_from_slice(&buf[..write_to]);
total_written += write_to;
if me.buf.capacity() == me.buf.len() {
// The buffer is full, bail out
break;
}
me.buf.extend_from_slice(buf);
total_written += buf.len();
}
}
Poll::Ready(Ok(total_written))
Expand Down

0 comments on commit d34f468

Please sign in to comment.