Skip to content

Commit 1c3449d

Browse files
committed
Do not store copy of the full path for each SearchPathFile
1 parent 516bd8c commit 1c3449d

2 files changed

Lines changed: 24 additions & 16 deletions

File tree

compiler/rustc_metadata/src/locator.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,8 @@ impl<'a> CrateLocator<'a> {
438438
}
439439
if let Some(matches) = spf.query(prefix, suffix) {
440440
for (hash, spf) in matches {
441-
info!("lib candidate: {}", spf.path.display());
441+
let spf_path = spf.path();
442+
info!("lib candidate: {}", spf_path.display());
442443

443444
let (rlibs, rmetas, dylibs, interfaces) =
444445
candidates.entry(hash).or_default();
@@ -447,21 +448,20 @@ impl<'a> CrateLocator<'a> {
447448
// ones we've already seen. This allows us to ignore crates
448449
// we know are exactual equal to ones we've already found.
449450
// Going to the same crate through different symlinks does not change the result.
450-
let path = try_canonicalize(&spf.path)
451-
.unwrap_or_else(|_| spf.path.to_path_buf());
451+
let path =
452+
try_canonicalize(&spf_path).unwrap_or_else(|_| spf_path.clone());
452453
if seen_paths.contains(&path) {
453454
continue;
454455
};
455456
seen_paths.insert(path);
456457
}
457458
// Use the original path (potentially with unresolved symlinks),
458459
// filesystem code should not care, but this is nicer for diagnostics.
459-
let path = spf.path.to_path_buf();
460460
match kind {
461-
CrateFlavor::Rlib => rlibs.insert(path),
462-
CrateFlavor::Rmeta => rmetas.insert(path),
463-
CrateFlavor::Dylib => dylibs.insert(path),
464-
CrateFlavor::SDylib => interfaces.insert(path),
461+
CrateFlavor::Rlib => rlibs.insert(spf_path),
462+
CrateFlavor::Rmeta => rmetas.insert(spf_path),
463+
CrateFlavor::Dylib => dylibs.insert(spf_path),
464+
CrateFlavor::SDylib => interfaces.insert(spf_path),
465465
};
466466
}
467467
}
@@ -471,10 +471,9 @@ impl<'a> CrateLocator<'a> {
471471
.flatten()
472472
{
473473
for (_, spf) in static_matches {
474-
crate_rejections.via_kind.push(CrateMismatch {
475-
path: spf.path.to_path_buf(),
476-
got: "static".to_string(),
477-
});
474+
crate_rejections
475+
.via_kind
476+
.push(CrateMismatch { path: spf.path(), got: "static".to_string() });
478477
}
479478
}
480479
}

compiler/rustc_session/src/search_paths.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,16 @@ impl FilesIndex {
6161
/// UTF-8, and so a non-UTF-8 filename couldn't be one we're looking for.)
6262
#[derive(Clone, Debug)]
6363
pub struct SearchPathFile {
64-
pub path: Arc<Path>,
65-
pub file_name_str: Arc<str>,
64+
// We store the directory in a shared Arc, as many files can share the same directory.
65+
dir: Arc<Path>,
66+
file_name_str: Arc<str>,
67+
}
68+
69+
impl SearchPathFile {
70+
/// Constructs the full path to the file.
71+
pub fn path(&self) -> PathBuf {
72+
self.dir.join(&*self.file_name_str)
73+
}
6674
}
6775

6876
#[derive(PartialEq, Clone, Copy, Debug, Hash, Eq, Encodable, Decodable, HashStable_Generic)]
@@ -129,18 +137,19 @@ impl SearchPath {
129137
}
130138

131139
pub fn new(kind: PathKind, dir: PathBuf) -> Self {
140+
let dir_file: Arc<Path> = dir.clone().into();
132141
// Get the files within the directory.
133142
let mut files = match std::fs::read_dir(&dir) {
134143
Ok(files) => files
135144
.filter_map(|e| {
136145
e.ok().and_then(|e| {
137146
e.file_name().to_str().map(|s| {
138147
let file_name_str: Arc<str> = s.into();
139-
SearchPathFile { path: e.path().into(), file_name_str }
148+
SearchPathFile { dir: dir_file.clone(), file_name_str }
140149
})
141150
})
142151
})
143-
.collect::<Vec<_>>(),
152+
.collect::<Vec<SearchPathFile>>(),
144153

145154
Err(..) => Default::default(),
146155
};

0 commit comments

Comments
 (0)