Skip to content

Commit 8ced51d

Browse files
Paolo TranquilliCopilot
andcommitted
Rust: Use a single proc-macro server again
Stateful proc macros like `enum_dispatch` keep process-global state across expansions, so spreading expansions over a pool of servers can lose generated items. Add an integration test exercising this pattern under parallel extraction. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent d7bfbf3 commit 8ced51d

21 files changed

Lines changed: 541 additions & 31 deletions

File tree

‎rust/extractor/src/config.rs‎

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ use std::path::{Path, PathBuf};
2424

2525
use crate::toolchain::select_toolchain;
2626

27-
const MAX_PROC_MACRO_PROCESSES: usize = 4;
28-
2927
#[derive(Debug, PartialEq, Eq, Default, Serialize, Deserialize, Clone, Copy)]
3028
#[serde(rename_all = "lowercase")]
3129
pub enum Compression {
@@ -205,8 +203,7 @@ impl Config {
205203
}
206204
}
207205

208-
/// Builds the rust-analyzer loading configuration for extracting on `threads` threads.
209-
pub fn to_cargo_config(&self, dir: &AbsPath, threads: usize) -> (CargoConfig, LoadCargoConfig) {
206+
pub fn to_cargo_config(&self, dir: &AbsPath) -> (CargoConfig, LoadCargoConfig) {
210207
let sysroot = self.sysroot(dir);
211208
let mut extra_args = self.cargo_extra_args.clone();
212209
// The fixed toolchain may be older than the project's declared minimum.
@@ -260,9 +257,7 @@ impl Config {
260257
with_proc_macro_server: self.proc_macro_server_choice(dir),
261258
prefill_caches: false,
262259
num_worker_threads: 1,
263-
// each process serves one expansion at a time and loads every proc-macro dylib,
264-
// so a few processes are shared by all extraction threads to bound memory
265-
proc_macro_processes: threads.clamp(1, MAX_PROC_MACRO_PROCESSES),
260+
proc_macro_processes: 1,
266261
},
267262
)
268263
}
@@ -309,26 +304,3 @@ fn join_path_buf(lhs: &AbsPath, rhs: &Path) -> AbsPathBuf {
309304
};
310305
lhs.join(path)
311306
}
312-
313-
#[cfg(test)]
314-
mod tests {
315-
use super::*;
316-
317-
#[test]
318-
fn proc_macro_processes_follow_threads_up_to_cap() {
319-
let dir = AbsPathBuf::assert_utf8(std::env::temp_dir());
320-
// explicit sysroot paths avoid toolchain discovery
321-
let config = Config {
322-
sysroot: Some(dir.clone().into()),
323-
sysroot_src: Some(dir.clone().into()),
324-
..Default::default()
325-
};
326-
for (threads, expected) in [(0, 1), (1, 1), (2, 2), (4, 4), (32, 4)] {
327-
let (_, load_config) = config.to_cargo_config(&dir, threads);
328-
assert_eq!(
329-
load_config.proc_macro_processes, expected,
330-
"{threads} threads"
331-
);
332-
}
333-
}
334-
}

‎rust/extractor/src/main.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@ fn main() -> anyhow::Result<()> {
390390
let (workspaces, unmanaged) = Extractor::group_files(&manifests, &files);
391391
let cwd = cwd()?;
392392
log_project_toolchain();
393+
let (cargo_config, load_cargo_config) = config.to_cargo_config(&cwd);
393394
let library_mode = if config.extract_dependencies_as_source {
394395
SourceKind::Source
395396
} else {
@@ -409,7 +410,6 @@ fn main() -> anyhow::Result<()> {
409410
SourceKind::Source,
410411
&RustAnalyzerNoSemantics::warning("no manifest found"),
411412
);
412-
let (cargo_config, load_cargo_config) = config.to_cargo_config(&cwd, threads);
413413

414414
let mut dependencies = DependencyFiles::default();
415415

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "proc_macro_state"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
macros = { path = "macros" }
8+
9+
[workspace]
10+
members = ["macros", "busy0", "busy1", "busy2", "busy3", "busy4", "busy5"]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "busy0"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
macros = { path = "../macros" }
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#[macros::slow]
2+
pub fn f0() {}
3+
4+
#[macros::slow]
5+
pub fn f1() {}
6+
7+
#[macros::slow]
8+
pub fn f2() {}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "busy1"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
macros = { path = "../macros" }
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#[macros::slow]
2+
pub fn f0() {}
3+
4+
#[macros::slow]
5+
pub fn f1() {}
6+
7+
#[macros::slow]
8+
pub fn f2() {}
9+
10+
#[macros::slow]
11+
pub fn f3() {}
12+
13+
#[macros::slow]
14+
pub fn f4() {}
15+
16+
#[macros::slow]
17+
pub fn f5() {}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "busy2"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
macros = { path = "../macros" }
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#[macros::slow]
2+
pub fn f0() {}
3+
4+
#[macros::slow]
5+
pub fn f1() {}
6+
7+
#[macros::slow]
8+
pub fn f2() {}
9+
10+
#[macros::slow]
11+
pub fn f3() {}
12+
13+
#[macros::slow]
14+
pub fn f4() {}
15+
16+
#[macros::slow]
17+
pub fn f5() {}
18+
19+
#[macros::slow]
20+
pub fn f6() {}
21+
22+
#[macros::slow]
23+
pub fn f7() {}
24+
25+
#[macros::slow]
26+
pub fn f8() {}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "busy3"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
macros = { path = "../macros" }

0 commit comments

Comments
 (0)