Skip to content

Commit 20238ef

Browse files
authored
Rollup merge of rust-lang#49594 - mbrubeck:docs, r=steveklabnik
Add some performance guidance to std::fs and std::io docs Adds more documentation about performance to various "read" functions in `fs` and `io`, and to `BufReader`/`BufWriter`, with the goal of helping developers choose the best option for a given task. r? @steveklabnik
2 parents 74ed5ac + ec71453 commit 20238ef

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

src/libstd/fs.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,9 @@ fn initial_buffer_size(file: &File) -> usize {
231231
/// Read the entire contents of a file into a bytes vector.
232232
///
233233
/// This is a convenience function for using [`File::open`] and [`read_to_end`]
234-
/// with fewer imports and without an intermediate variable.
234+
/// with fewer imports and without an intermediate variable. It pre-allocates a
235+
/// buffer based on the file size when available, so it is generally faster than
236+
/// reading into a vector created with `Vec::new()`.
235237
///
236238
/// [`File::open`]: struct.File.html#method.open
237239
/// [`read_to_end`]: ../io/trait.Read.html#method.read_to_end
@@ -270,7 +272,9 @@ pub fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
270272
/// Read the entire contents of a file into a string.
271273
///
272274
/// This is a convenience function for using [`File::open`] and [`read_to_string`]
273-
/// with fewer imports and without an intermediate variable.
275+
/// with fewer imports and without an intermediate variable. It pre-allocates a
276+
/// buffer based on the file size when available, so it is generally faster than
277+
/// reading into a string created with `String::new()`.
274278
///
275279
/// [`File::open`]: struct.File.html#method.open
276280
/// [`read_to_string`]: ../io/trait.Read.html#method.read_to_string

src/libstd/io/buffered.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ use memchr;
2525
/// results in a system call. A `BufReader` performs large, infrequent reads on
2626
/// the underlying [`Read`] and maintains an in-memory buffer of the results.
2727
///
28+
/// `BufReader` can improve the speed of programs that make *small* and
29+
/// *repeated* read calls to the same file or network socket. It does not
30+
/// help when reading very large amounts at once, or reading just one or a few
31+
/// times. It also provides no advantage when reading from a source that is
32+
/// already in memory, like a `Vec<u8>`.
33+
///
2834
/// [`Read`]: ../../std/io/trait.Read.html
2935
/// [`TcpStream::read`]: ../../std/net/struct.TcpStream.html#method.read
3036
/// [`TcpStream`]: ../../std/net/struct.TcpStream.html
@@ -359,6 +365,12 @@ impl<R: Seek> Seek for BufReader<R> {
359365
/// `BufWriter` keeps an in-memory buffer of data and writes it to an underlying
360366
/// writer in large, infrequent batches.
361367
///
368+
/// `BufWriter` can improve the speed of programs that make *small* and
369+
/// *repeated* write calls to the same file or network socket. It does not
370+
/// help when writing very large amounts at once, or writing just one or a few
371+
/// times. It also provides no advantage when writing to a destination that is
372+
/// in memory, like a `Vec<u8>`.
373+
///
362374
/// When the `BufWriter` is dropped, the contents of its buffer will be written
363375
/// out. However, any errors that happen in the process of flushing the buffer
364376
/// when the writer is dropped will be ignored. Code that wishes to handle such

src/libstd/io/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,9 @@ pub trait Read {
595595
/// Ok(())
596596
/// }
597597
/// ```
598+
///
599+
/// (See also the [`std::fs::read`] convenience function for reading from a
600+
/// file.)
598601
#[stable(feature = "rust1", since = "1.0.0")]
599602
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize> {
600603
read_to_end(self, buf)
@@ -633,6 +636,9 @@ pub trait Read {
633636
/// Ok(())
634637
/// }
635638
/// ```
639+
///
640+
/// (See also the [`std::fs::read_to_string`] convenience function for
641+
/// reading from a file.)
636642
#[stable(feature = "rust1", since = "1.0.0")]
637643
fn read_to_string(&mut self, buf: &mut String) -> Result<usize> {
638644
// Note that we do *not* call `.read_to_end()` here. We are passing

0 commit comments

Comments
 (0)