Skip to content

Commit eae615d

Browse files
committed
Remove unnecessary lseek syscall when using std::fs::read
1 parent 2afe585 commit eae615d

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

library/std/src/fs.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,9 @@ pub fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
250250
fn inner(path: &Path) -> io::Result<Vec<u8>> {
251251
let mut file = File::open(path)?;
252252
let mut bytes = Vec::new();
253-
file.read_to_end(&mut bytes)?;
253+
let size = file.metadata().map(|m| m.len()).unwrap_or(0);
254+
bytes.reserve(size as usize);
255+
io::default_read_to_end(&mut file, &mut bytes)?;
254256
Ok(bytes)
255257
}
256258
inner(path.as_ref())
@@ -289,7 +291,9 @@ pub fn read_to_string<P: AsRef<Path>>(path: P) -> io::Result<String> {
289291
fn inner(path: &Path) -> io::Result<String> {
290292
let mut file = File::open(path)?;
291293
let mut string = String::new();
292-
file.read_to_string(&mut string)?;
294+
let size = file.metadata().map(|m| m.len()).unwrap_or(0);
295+
string.reserve(size as usize);
296+
io::default_read_to_string(&mut file, &mut string)?;
293297
Ok(string)
294298
}
295299
inner(path.as_ref())

0 commit comments

Comments
 (0)