Skip to content

Commit 89b61ea

Browse files
committed
Inline and remove FileSearch::search.
It has only a single callsite, and having all the code in one place will make it possible to optimize the search.
1 parent 47b5d95 commit 89b61ea

File tree

2 files changed

+47
-49
lines changed

2 files changed

+47
-49
lines changed

compiler/rustc_metadata/src/locator.rs

+46-35
Original file line numberDiff line numberDiff line change
@@ -394,44 +394,55 @@ impl<'a> CrateLocator<'a> {
394394
// of the crate id (path/name/id).
395395
//
396396
// The goal of this step is to look at as little metadata as possible.
397-
self.filesearch.search(|spf, kind| {
398-
let file = match &spf.file_name_str {
399-
None => return,
400-
Some(file) => file,
401-
};
402-
let (hash, found_kind) = if file.starts_with(&rlib_prefix) && file.ends_with(".rlib") {
403-
(&file[(rlib_prefix.len())..(file.len() - ".rlib".len())], CrateFlavor::Rlib)
404-
} else if file.starts_with(&rlib_prefix) && file.ends_with(".rmeta") {
405-
(&file[(rlib_prefix.len())..(file.len() - ".rmeta".len())], CrateFlavor::Rmeta)
406-
} else if file.starts_with(&dylib_prefix) && file.ends_with(&self.target.dll_suffix) {
407-
(
408-
&file[(dylib_prefix.len())..(file.len() - self.target.dll_suffix.len())],
409-
CrateFlavor::Dylib,
410-
)
411-
} else {
412-
if file.starts_with(&staticlib_prefix)
413-
&& file.ends_with(&self.target.staticlib_suffix)
397+
for search_path in self.filesearch.search_paths() {
398+
debug!("searching {}", search_path.dir.display());
399+
for spf in search_path.files.iter() {
400+
debug!("testing {}", spf.path.display());
401+
402+
let file = match &spf.file_name_str {
403+
None => continue,
404+
Some(file) => file,
405+
};
406+
let (hash, found_kind) = if file.starts_with(&rlib_prefix)
407+
&& file.ends_with(".rlib")
414408
{
415-
staticlibs
416-
.push(CrateMismatch { path: spf.path.clone(), got: "static".to_string() });
417-
}
418-
return;
419-
};
409+
(&file[(rlib_prefix.len())..(file.len() - ".rlib".len())], CrateFlavor::Rlib)
410+
} else if file.starts_with(&rlib_prefix) && file.ends_with(".rmeta") {
411+
(&file[(rlib_prefix.len())..(file.len() - ".rmeta".len())], CrateFlavor::Rmeta)
412+
} else if file.starts_with(&dylib_prefix) && file.ends_with(&self.target.dll_suffix)
413+
{
414+
(
415+
&file[(dylib_prefix.len())..(file.len() - self.target.dll_suffix.len())],
416+
CrateFlavor::Dylib,
417+
)
418+
} else {
419+
if file.starts_with(&staticlib_prefix)
420+
&& file.ends_with(&self.target.staticlib_suffix)
421+
{
422+
staticlibs.push(CrateMismatch {
423+
path: spf.path.clone(),
424+
got: "static".to_string(),
425+
});
426+
}
427+
continue;
428+
};
420429

421-
info!("lib candidate: {}", spf.path.display());
430+
info!("lib candidate: {}", spf.path.display());
431+
432+
let (rlibs, rmetas, dylibs) = candidates.entry(hash.to_string()).or_default();
433+
let path = fs::canonicalize(&spf.path).unwrap_or_else(|_| spf.path.clone());
434+
if seen_paths.contains(&path) {
435+
continue;
436+
};
437+
seen_paths.insert(path.clone());
438+
match found_kind {
439+
CrateFlavor::Rlib => rlibs.insert(path, search_path.kind),
440+
CrateFlavor::Rmeta => rmetas.insert(path, search_path.kind),
441+
CrateFlavor::Dylib => dylibs.insert(path, search_path.kind),
442+
};
443+
}
444+
}
422445

423-
let (rlibs, rmetas, dylibs) = candidates.entry(hash.to_string()).or_default();
424-
let path = fs::canonicalize(&spf.path).unwrap_or_else(|_| spf.path.clone());
425-
if seen_paths.contains(&path) {
426-
return;
427-
};
428-
seen_paths.insert(path.clone());
429-
match found_kind {
430-
CrateFlavor::Rlib => rlibs.insert(path, kind),
431-
CrateFlavor::Rmeta => rmetas.insert(path, kind),
432-
CrateFlavor::Dylib => dylibs.insert(path, kind),
433-
};
434-
});
435446
self.crate_rejections.via_kind.extend(staticlibs);
436447

437448
// We have now collected all known libraries into a set of candidates

compiler/rustc_session/src/filesearch.rs

+1-14
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::fs;
55
use std::iter::FromIterator;
66
use std::path::{Path, PathBuf};
77

8-
use crate::search_paths::{PathKind, SearchPath, SearchPathFile};
8+
use crate::search_paths::{PathKind, SearchPath};
99
use rustc_fs_util::fix_windows_verbatim_for_gcc;
1010
use tracing::debug;
1111

@@ -41,19 +41,6 @@ impl<'a> FileSearch<'a> {
4141
self.get_lib_path().join("self-contained")
4242
}
4343

44-
pub fn search<F>(&self, mut pick: F)
45-
where
46-
F: FnMut(&SearchPathFile, PathKind),
47-
{
48-
for search_path in self.search_paths() {
49-
debug!("searching {}", search_path.dir.display());
50-
for spf in search_path.files.iter() {
51-
debug!("testing {}", spf.path.display());
52-
pick(spf, search_path.kind);
53-
}
54-
}
55-
}
56-
5744
pub fn new(
5845
sysroot: &'a Path,
5946
triple: &'a str,

0 commit comments

Comments
 (0)