rustc_metadata: Preprocess search paths for better performance

Over in Zed we've noticed that loading crates for a large-ish workspace can take almost 200ms. We've pinned it down to how rustc searches for paths, as it performs a linear search over the list of candidate paths. In our case the candidate list had about 20k entries which we had to iterate over for each dependency being loaded.

This commit introduces a simple FilesIndex that's just a sorted Vec under the hood. Since crates are looked up by both prefix and suffix, we perform a range search on said Vec (which constraints the search space based on prefix) and follow up with a linear scan of entries with matching suffixes.
FilesIndex is also pre-filtered before any queries are performed using available target information; query prefixes/sufixes are based on the target we are compiling for, so we can remove entries that can never match up front.

Overall, this commit brings down build time for us in dev scenarios by about 6%.
100ms might not seem like much, but this is a constant cost that each of our workspace crates has to pay, even when said crate is miniscule.
This commit is contained in:
Piotr Osiewicz
2024-11-11 18:34:46 +01:00
parent 251dc8ad84
commit 42e71bb8ea
8 changed files with 137 additions and 76 deletions

View File

@@ -44,8 +44,9 @@ use crate::config::{
InstrumentCoverage, OptLevel, OutFileName, OutputType, RemapPathScopeComponents,
SwitchWithOptPath,
};
use crate::filesearch::FileSearch;
use crate::parse::{ParseSess, add_feature_diagnostics};
use crate::search_paths::{PathKind, SearchPath};
use crate::search_paths::SearchPath;
use crate::{errors, filesearch, lint};
struct OptimizationFuel {
@@ -218,6 +219,9 @@ pub struct Session {
/// This is mainly useful for other tools that reads that debuginfo to figure out
/// how to call the compiler with the same arguments.
pub expanded_args: Vec<String>,
target_filesearch: FileSearch,
host_filesearch: FileSearch,
}
#[derive(PartialEq, Eq, PartialOrd, Ord)]
@@ -443,11 +447,11 @@ impl Session {
format!("__rustc_proc_macro_decls_{:08x}__", stable_crate_id.as_u64())
}
pub fn target_filesearch(&self, kind: PathKind) -> filesearch::FileSearch<'_> {
filesearch::FileSearch::new(&self.opts.search_paths, &self.target_tlib_path, kind)
pub fn target_filesearch(&self) -> &filesearch::FileSearch {
&self.target_filesearch
}
pub fn host_filesearch(&self, kind: PathKind) -> filesearch::FileSearch<'_> {
filesearch::FileSearch::new(&self.opts.search_paths, &self.host_tlib_path, kind)
pub fn host_filesearch(&self) -> &filesearch::FileSearch {
&self.host_filesearch
}
/// Returns a list of directories where target-specific tool binaries are located. Some fallback
@@ -1111,7 +1115,9 @@ pub fn build_session(
});
let asm_arch = if target.allow_asm { InlineAsmArch::from_str(&target.arch).ok() } else { None };
let target_filesearch =
filesearch::FileSearch::new(&sopts.search_paths, &target_tlib_path, &target);
let host_filesearch = filesearch::FileSearch::new(&sopts.search_paths, &host_tlib_path, &host);
let sess = Session {
target,
host,
@@ -1138,6 +1144,8 @@ pub fn build_session(
cfg_version,
using_internal_features,
expanded_args,
target_filesearch,
host_filesearch,
};
validate_commandline_args_with_session_available(&sess);