Skip to content

Commit

Permalink
Pre-allocate in fs::read and fs::read_string
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrubeck committed Jan 10, 2018
1 parent f62f774 commit 1745f43
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/libstd/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,12 @@ pub struct DirBuilder {
/// ```
#[unstable(feature = "fs_read_write", issue = "46588")]
pub fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
let mut bytes = Vec::new();
File::open(path)?.read_to_end(&mut bytes)?;
// Allocate one extra byte so the buffer doesn't need to grow before the final `read` call at
// the end of the file. Don't worry about `usize` overflow because this will fail anyway if
// the file doesn't fit into memory.
let mut bytes = Vec::with_capacity(metadata(&path)?.len() as usize + 1);

File::open(&path)?.read_to_end(&mut bytes)?;
Ok(bytes)
}

Expand Down Expand Up @@ -287,8 +291,12 @@ pub fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
/// ```
#[unstable(feature = "fs_read_write", issue = "46588")]
pub fn read_string<P: AsRef<Path>>(path: P) -> io::Result<String> {
let mut string = String::new();
File::open(path)?.read_to_string(&mut string)?;
// Allocate one extra byte so the buffer doesn't need to grow before the final `read` call at
// the end of the file. Don't worry about `usize` overflow because this will fail anyway if
// the file doesn't fit into memory.
let mut string = String::with_capacity(metadata(&path)?.len() as usize + 1);

File::open(&path)?.read_to_string(&mut string)?;
Ok(string)
}

Expand Down

0 comments on commit 1745f43

Please sign in to comment.