diff --git a/rust/extractor/src/config.rs b/rust/extractor/src/config.rs index bc884db75bb9..85e6f34f7bf8 100644 --- a/rust/extractor/src/config.rs +++ b/rust/extractor/src/config.rs @@ -24,6 +24,8 @@ use std::path::{Path, PathBuf}; use crate::toolchain::select_toolchain; +const MAX_PROC_MACRO_PROCESSES: usize = 4; + #[derive(Debug, PartialEq, Eq, Default, Serialize, Deserialize, Clone, Copy)] #[serde(rename_all = "lowercase")] pub enum Compression { @@ -203,7 +205,8 @@ impl Config { } } - pub fn to_cargo_config(&self, dir: &AbsPath) -> (CargoConfig, LoadCargoConfig) { + /// Builds the rust-analyzer loading configuration for extracting on `threads` threads. + pub fn to_cargo_config(&self, dir: &AbsPath, threads: usize) -> (CargoConfig, LoadCargoConfig) { let sysroot = self.sysroot(dir); let mut extra_args = self.cargo_extra_args.clone(); // The fixed toolchain may be older than the project's declared minimum. @@ -257,7 +260,9 @@ impl Config { with_proc_macro_server: self.proc_macro_server_choice(dir), prefill_caches: false, num_worker_threads: 1, - proc_macro_processes: 1, + // each process serves one expansion at a time and loads every proc-macro dylib, + // so a few processes are shared by all extraction threads to bound memory + proc_macro_processes: threads.clamp(1, MAX_PROC_MACRO_PROCESSES), }, ) } @@ -304,3 +309,26 @@ fn join_path_buf(lhs: &AbsPath, rhs: &Path) -> AbsPathBuf { }; lhs.join(path) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn proc_macro_processes_follow_threads_up_to_cap() { + let dir = AbsPathBuf::assert_utf8(std::env::temp_dir()); + // explicit sysroot paths avoid toolchain discovery + let config = Config { + sysroot: Some(dir.clone().into()), + sysroot_src: Some(dir.clone().into()), + ..Default::default() + }; + for (threads, expected) in [(0, 1), (1, 1), (2, 2), (4, 4), (32, 4)] { + let (_, load_config) = config.to_cargo_config(&dir, threads); + assert_eq!( + load_config.proc_macro_processes, expected, + "{threads} threads" + ); + } + } +} diff --git a/rust/extractor/src/main.rs b/rust/extractor/src/main.rs index 30a78611b9b2..f7f31cc2e365 100644 --- a/rust/extractor/src/main.rs +++ b/rust/extractor/src/main.rs @@ -365,7 +365,6 @@ fn main() -> anyhow::Result<()> { let workspaces = extractor.group_files(&manifests, &files); let cwd = cwd()?; log_project_toolchain(); - let (cargo_config, load_cargo_config) = config.to_cargo_config(&cwd); let library_mode = if config.extract_dependencies_as_source { SourceKind::Source } else { @@ -379,6 +378,7 @@ fn main() -> anyhow::Result<()> { let source_files = HashSet::from_iter(files.iter().cloned()); let threads = parallel::extraction_threads(); info!("extraction threads: {threads}"); + let (cargo_config, load_cargo_config) = config.to_cargo_config(&cwd, threads); let mut dependencies = DependencyFiles::default();