Skip to content

Commit 569e542

Browse files
committed
Auto merge of #80756 - sunfishcode:path-cleanup/rustc-incremental, r=nagisa
Optimize away some `fs::metadata` calls. This also eliminates a use of a `Path` convenience function, in support of #80741, refactoring `std::path` to focus on pure data structures and algorithms.
2 parents 9155a9d + 304643c commit 569e542

File tree

3 files changed

+34
-32
lines changed

3 files changed

+34
-32
lines changed

compiler/rustc_incremental/src/persist/file_format.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ pub fn read_file(
5252
path: &Path,
5353
nightly_build: bool,
5454
) -> io::Result<Option<(Vec<u8>, usize)>> {
55-
if !path.exists() {
56-
return Ok(None);
57-
}
58-
59-
let data = fs::read(path)?;
55+
let data = match fs::read(path) {
56+
Ok(data) => data,
57+
Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(None),
58+
Err(err) => return Err(err),
59+
};
6060

6161
let mut file = io::Cursor::new(data);
6262

compiler/rustc_incremental/src/persist/fs.rs

+16-14
Original file line numberDiff line numberDiff line change
@@ -922,22 +922,24 @@ fn all_except_most_recent(
922922
/// before passing it to std::fs::remove_dir_all(). This will convert the path
923923
/// into the '\\?\' format, which supports much longer paths.
924924
fn safe_remove_dir_all(p: &Path) -> io::Result<()> {
925-
if p.exists() {
926-
let canonicalized = p.canonicalize()?;
927-
std_fs::remove_dir_all(canonicalized)
928-
} else {
929-
Ok(())
930-
}
925+
let canonicalized = match std_fs::canonicalize(p) {
926+
Ok(canonicalized) => canonicalized,
927+
Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(()),
928+
Err(err) => return Err(err),
929+
};
930+
931+
std_fs::remove_dir_all(canonicalized)
931932
}
932933

933934
fn safe_remove_file(p: &Path) -> io::Result<()> {
934-
if p.exists() {
935-
let canonicalized = p.canonicalize()?;
936-
match std_fs::remove_file(canonicalized) {
937-
Err(ref err) if err.kind() == io::ErrorKind::NotFound => Ok(()),
938-
result => result,
939-
}
940-
} else {
941-
Ok(())
935+
let canonicalized = match std_fs::canonicalize(p) {
936+
Ok(canonicalized) => canonicalized,
937+
Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(()),
938+
Err(err) => return Err(err),
939+
};
940+
941+
match std_fs::remove_file(canonicalized) {
942+
Err(err) if err.kind() == io::ErrorKind::NotFound => Ok(()),
943+
result => result,
942944
}
943945
}

compiler/rustc_incremental/src/persist/save.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_serialize::opaque::Encoder;
66
use rustc_serialize::Encodable as RustcEncodable;
77
use rustc_session::Session;
88
use std::fs;
9+
use std::io;
910
use std::path::PathBuf;
1011

1112
use super::data::*;
@@ -101,19 +102,18 @@ where
101102
// Note: It's important that we actually delete the old file and not just
102103
// truncate and overwrite it, since it might be a shared hard-link, the
103104
// underlying data of which we don't want to modify
104-
if path_buf.exists() {
105-
match fs::remove_file(&path_buf) {
106-
Ok(()) => {
107-
debug!("save: remove old file");
108-
}
109-
Err(err) => {
110-
sess.err(&format!(
111-
"unable to delete old dep-graph at `{}`: {}",
112-
path_buf.display(),
113-
err
114-
));
115-
return;
116-
}
105+
match fs::remove_file(&path_buf) {
106+
Ok(()) => {
107+
debug!("save: remove old file");
108+
}
109+
Err(err) if err.kind() == io::ErrorKind::NotFound => (),
110+
Err(err) => {
111+
sess.err(&format!(
112+
"unable to delete old dep-graph at `{}`: {}",
113+
path_buf.display(),
114+
err
115+
));
116+
return;
117117
}
118118
}
119119

0 commit comments

Comments
 (0)