diff options
| author | kennytm <kennytm@gmail.com> | 2018-04-05 02:04:16 +0800 |
|---|---|---|
| committer | kennytm <kennytm@gmail.com> | 2018-04-05 02:17:03 +0800 |
| commit | 93ad4f2b9c093f204dbd5278c70256f6ec369b32 (patch) | |
| tree | 8e1b6b8bf672672d251712a367078a80b57a3218 /src/libstd/io | |
| parent | d6f19b676f2e2c5f47959d0841b1a24353bc06ad (diff) | |
| parent | 390f8367e74d72317ab4aa5097048243073968fa (diff) | |
| download | rust-93ad4f2b9c093f204dbd5278c70256f6ec369b32.tar.gz rust-93ad4f2b9c093f204dbd5278c70256f6ec369b32.zip | |
Rollup merge of #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.
Diffstat (limited to 'src/libstd/io')
| -rw-r--r-- | src/libstd/io/buffered.rs | 12 | ||||
| -rw-r--r-- | src/libstd/io/mod.rs | 10 |
2 files changed, 22 insertions, 0 deletions
diff --git a/src/libstd/io/buffered.rs b/src/libstd/io/buffered.rs index 3e9ae261ab6..d6eac748334 100644 --- a/src/libstd/io/buffered.rs +++ b/src/libstd/io/buffered.rs @@ -25,6 +25,12 @@ use memchr; /// results in a system call. A `BufReader` performs large, infrequent reads on /// the underlying [`Read`] and maintains an in-memory buffer of the results. /// +/// `BufReader` can improve the speed of programs that make *small* and +/// *repeated* read calls to the same file or network socket. It does not +/// help when reading very large amounts at once, or reading just one or a few +/// times. It also provides no advantage when reading from a source that is +/// already in memory, like a `Vec<u8>`. +/// /// [`Read`]: ../../std/io/trait.Read.html /// [`TcpStream::read`]: ../../std/net/struct.TcpStream.html#method.read /// [`TcpStream`]: ../../std/net/struct.TcpStream.html @@ -359,6 +365,12 @@ impl<R: Seek> Seek for BufReader<R> { /// `BufWriter` keeps an in-memory buffer of data and writes it to an underlying /// writer in large, infrequent batches. /// +/// `BufWriter` can improve the speed of programs that make *small* and +/// *repeated* write calls to the same file or network socket. It does not +/// help when writing very large amounts at once, or writing just one or a few +/// times. It also provides no advantage when writing to a destination that is +/// in memory, like a `Vec<u8>`. +/// /// When the `BufWriter` is dropped, the contents of its buffer will be written /// out. However, any errors that happen in the process of flushing the buffer /// when the writer is dropped will be ignored. Code that wishes to handle such diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index 63b631ace96..3b8c42ddb39 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -595,6 +595,11 @@ pub trait Read { /// Ok(()) /// } /// ``` + /// + /// (See also the [`std::fs::read`] convenience function for reading from a + /// file.) + /// + /// [`std::fs::read`]: ../fs/fn.read.html #[stable(feature = "rust1", since = "1.0.0")] fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize> { read_to_end(self, buf) @@ -633,6 +638,11 @@ pub trait Read { /// Ok(()) /// } /// ``` + /// + /// (See also the [`std::fs::read_to_string`] convenience function for + /// reading from a file.) + /// + /// [`std::fs::read_to_string`]: ../fs/fn.read_to_string.html #[stable(feature = "rust1", since = "1.0.0")] fn read_to_string(&mut self, buf: &mut String) -> Result<usize> { // Note that we do *not* call `.read_to_end()` here. We are passing |
