-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Remove unnecessary lseek syscall when using std::fs::read #106664
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bors
merged 2 commits into
rust-lang:master
from
chenyukang:yukang/fix-106597-remove-lseek
Jan 11, 2023
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -250,7 +250,9 @@ pub fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> { | |
fn inner(path: &Path) -> io::Result<Vec<u8>> { | ||
let mut file = File::open(path)?; | ||
let mut bytes = Vec::new(); | ||
file.read_to_end(&mut bytes)?; | ||
let size = file.metadata().map(|m| m.len()).unwrap_or(0); | ||
bytes.reserve(size as usize); | ||
io::default_read_to_end(&mut file, &mut bytes)?; | ||
Ok(bytes) | ||
} | ||
inner(path.as_ref()) | ||
|
@@ -289,7 +291,9 @@ pub fn read_to_string<P: AsRef<Path>>(path: P) -> io::Result<String> { | |
fn inner(path: &Path) -> io::Result<String> { | ||
let mut file = File::open(path)?; | ||
let mut string = String::new(); | ||
file.read_to_string(&mut string)?; | ||
let size = file.metadata().map(|m| m.len()).unwrap_or(0); | ||
string.reserve(size as usize); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
io::default_read_to_string(&mut file, &mut string)?; | ||
Ok(string) | ||
} | ||
inner(path.as_ref()) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can do
let mut bytes = Vec::with_capacity(size as usize)
instead of two linesreserve
andVec::new()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I thought on this, but I have a dig on the implementation of
with_capacity
andreserve
, seems it's not simply share the same code path, so I'm not 100% sure is there any flaw to usewith_capacity
here.I just have a simple bench test, seems
with_capacity
perform better:with bench code: