Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions rust/extractor/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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),
Comment thread
redsun82 marked this conversation as resolved.
Resolved
},
)
}
Expand Down Expand Up @@ -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"
);
}
}
}
2 changes: 1 addition & 1 deletion rust/extractor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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();

Expand Down
Loading