Skip to content

Commit e4328ae

Browse files
committed
Code review updates: all minor style fixes
- Renamed write_to_buffer to write_to_buf, for consistency - Fixed references to flush_buf - Optimized `write` to use one less `memchr` call
1 parent e022d34 commit e4328ae

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

src/libstd/io/buffered.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ impl<W: Write> BufWriter<W> {
565565
/// data. Writes as much as possible without exceeding capacity. Returns
566566
/// the number of bytes written.
567567
#[inline]
568-
fn write_to_buffer(&mut self, buf: &[u8]) -> usize {
568+
fn write_to_buf(&mut self, buf: &[u8]) -> usize {
569569
let available = self.buf.capacity() - self.buf.len();
570570
let amt_to_buffer = available.min(buf.len());
571571
self.buf.extend_from_slice(&buf[..amt_to_buffer]);
@@ -689,7 +689,7 @@ impl<W: Write> Write for BufWriter<W> {
689689
self.panicked = false;
690690
r
691691
} else {
692-
Ok(self.write_to_buffer(buf))
692+
Ok(self.write_to_buf(buf))
693693
}
694694
}
695695

@@ -703,7 +703,7 @@ impl<W: Write> Write for BufWriter<W> {
703703
self.panicked = false;
704704
r
705705
} else {
706-
self.write_to_buffer(buf);
706+
self.write_to_buf(buf);
707707
Ok(())
708708
}
709709
}
@@ -861,7 +861,7 @@ impl<W> fmt::Display for IntoInnerError<W> {
861861
/// Private helper struct for implementing the line-buffered writing logic.
862862
/// This shim temporarily wraps a BufWriter, and uses its internals to
863863
/// implement a line-buffered writer (specifically by using the internal
864-
/// methods like write_to_buffer and flush_buffer). In this way, a more
864+
/// methods like write_to_buf and flush_buf). In this way, a more
865865
/// efficient abstraction can be created than one that only had access to
866866
/// `write` and `flush`, without needlessly duplicating a lot of the
867867
/// implementation details of BufWriter. This also allows existing
@@ -925,15 +925,15 @@ impl<'a, W: Write> Write for LineWriterShim<'a, W> {
925925
}
926926
// Otherwise, arrange for the lines to be written directly to the
927927
// inner writer.
928-
Some(newline_idx) => newline_idx,
928+
Some(newline_idx) => newline_idx + 1,
929929
};
930930

931931
// Flush existing content to prepare for our write
932932
self.buffer.flush_buf()?;
933933

934934
// This is what we're going to try to write directly to the inner
935935
// writer. The rest will be buffered, if nothing goes wrong.
936-
let lines = &buf[..newline_idx + 1];
936+
let lines = &buf[..newline_idx];
937937

938938
// Write `lines` directly to the inner writer. In keeping with the
939939
// `write` convention, make at most one attempt to add new (unbuffered)
@@ -953,11 +953,10 @@ impl<'a, W: Write> Write for LineWriterShim<'a, W> {
953953
// the rest as possible). If there were any unwritten newlines, we
954954
// only buffer out to the last unwritten newline; this helps prevent
955955
// flushing partial lines on subsequent calls to LineWriterShim::write.
956-
let tail = &buf[flushed..];
957-
let buffered = match memchr::memrchr(b'\n', tail) {
958-
None => self.buffer.write_to_buffer(tail),
959-
Some(i) => self.buffer.write_to_buffer(&tail[..i + 1]),
960-
};
956+
let tail =
957+
if flushed < newline_idx { &buf[flushed..newline_idx] } else { &buf[newline_idx..] };
958+
959+
let buffered = self.buffer.write_to_buf(tail);
961960
Ok(flushed + buffered)
962961
}
963962

@@ -1056,7 +1055,7 @@ impl<'a, W: Write> Write for LineWriterShim<'a, W> {
10561055
// Now that the write has succeeded, buffer the rest (or as much of the
10571056
// rest as possible)
10581057
let buffered: usize =
1059-
tail.iter().map(|buf| self.buffer.write_to_buffer(buf)).take_while(|&n| n > 0).sum();
1058+
tail.iter().map(|buf| self.buffer.write_to_buf(buf)).take_while(|&n| n > 0).sum();
10601059

10611060
Ok(flushed + buffered)
10621061
}
@@ -1076,6 +1075,9 @@ impl<'a, W: Write> Write for LineWriterShim<'a, W> {
10761075
/// writer, it will also flush the existing buffer if it contains any
10771076
/// newlines, even if the incoming data does not contain any newlines.
10781077
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
1078+
// The default `write_all` calls `write` in a loop; we can do better
1079+
// by simply calling self.inner().write_all directly. This avoids
1080+
// round trips to `self.buffer` in the event of partial writes.
10791081
let newline_idx = match memchr::memrchr(b'\n', buf) {
10801082
// If there are no new newlines (that is, if this write is less than
10811083
// one line), just do a regular buffered write

0 commit comments

Comments
 (0)