Skip to content

Commit 9a820f7

Browse files
authored
Rollup merge of #106664 - chenyukang:yukang/fix-106597-remove-lseek, r=cuviper
Remove unnecessary lseek syscall when using std::fs::read Fixes #106597 r? ```@bjorn3```
2 parents 6e0c404 + f7bc68b commit 9a820f7

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

library/std/src/fs.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,9 @@ pub struct DirBuilder {
249249
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)?;
252-
let mut bytes = Vec::new();
253-
file.read_to_end(&mut bytes)?;
252+
let size = file.metadata().map(|m| m.len()).unwrap_or(0);
253+
let mut bytes = Vec::with_capacity(size as usize);
254+
io::default_read_to_end(&mut file, &mut bytes)?;
254255
Ok(bytes)
255256
}
256257
inner(path.as_ref())
@@ -288,8 +289,9 @@ pub fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
288289
pub fn read_to_string<P: AsRef<Path>>(path: P) -> io::Result<String> {
289290
fn inner(path: &Path) -> io::Result<String> {
290291
let mut file = File::open(path)?;
291-
let mut string = String::new();
292-
file.read_to_string(&mut string)?;
292+
let size = file.metadata().map(|m| m.len()).unwrap_or(0);
293+
let mut string = String::with_capacity(size as usize);
294+
io::default_read_to_string(&mut file, &mut string)?;
293295
Ok(string)
294296
}
295297
inner(path.as_ref())

0 commit comments

Comments
 (0)