Skip to content
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

Optimize fs::write #134730

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion library/std/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,14 @@ pub fn read_to_string<P: AsRef<Path>>(path: P) -> io::Result<String> {
#[stable(feature = "fs_read_write_bytes", since = "1.26.0")]
pub fn write<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> io::Result<()> {
fn inner(path: &Path, contents: &[u8]) -> io::Result<()> {
File::create(path)?.write_all(contents)
// As an optimization we don't truncate.
// Instead we overwrite the existing content and then set the file length
// to the number of written bytes (whether or not writing succeeds).
let mut f = OpenOptions::new().write(true).create(true).open(path)?;
let r = f.write_all(contents);
let r2 = f.stream_position().and_then(|pos| f.set_len(pos));
// Return the most pertinent error, if any.
if r.is_err() { r } else { r2 }
}
inner(path.as_ref(), contents.as_ref())
}
Expand Down
Loading