Skip to content

Commit 8db4f28

Browse files
committed
proc_macro: use crossbeam-channel for the CrossThread execution strategy
Compared to mpsc::channel, crossbeam-channel has significantly lower overhead.
1 parent 6ce595a commit 8db4f28

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3840,6 +3840,7 @@ dependencies = [
38403840
name = "rustc_expand"
38413841
version = "0.0.0"
38423842
dependencies = [
3843+
"crossbeam-channel",
38433844
"rustc_ast",
38443845
"rustc_ast_passes",
38453846
"rustc_ast_pretty",

compiler/rustc_expand/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ rustc_parse = { path = "../rustc_parse" }
2525
rustc_session = { path = "../rustc_session" }
2626
smallvec = { version = "1.6.1", features = ["union", "may_dangle"] }
2727
rustc_ast = { path = "../rustc_ast" }
28+
crossbeam-channel = "0.5.0"

compiler/rustc_expand/src/proc_macro.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,29 @@ use rustc_parse::parser::ForceCollect;
1212
use rustc_span::def_id::CrateNum;
1313
use rustc_span::{Span, DUMMY_SP};
1414

15+
struct CrossbeamMessagePipe<T> {
16+
tx: crossbeam_channel::Sender<T>,
17+
rx: crossbeam_channel::Receiver<T>,
18+
}
19+
20+
impl<T> pm::bridge::server::MessagePipe<T> for CrossbeamMessagePipe<T> {
21+
fn new() -> (Self, Self) {
22+
let (tx1, rx1) = crossbeam_channel::unbounded();
23+
let (tx2, rx2) = crossbeam_channel::unbounded();
24+
(CrossbeamMessagePipe { tx: tx1, rx: rx2 }, CrossbeamMessagePipe { tx: tx2, rx: rx1 })
25+
}
26+
27+
fn send(&mut self, value: T) {
28+
self.tx.send(value).unwrap();
29+
}
30+
31+
fn recv(&mut self) -> Option<T> {
32+
self.rx.recv().ok()
33+
}
34+
}
35+
1536
fn exec_strategy(ecx: &ExtCtxt<'_>) -> impl pm::bridge::server::ExecutionStrategy {
16-
<pm::bridge::server::MaybeCrossThread<pm::bridge::server::StdMessagePipe<_>>>::new(
37+
<pm::bridge::server::MaybeCrossThread<CrossbeamMessagePipe<_>>>::new(
1738
ecx.sess.opts.debugging_opts.proc_macro_cross_thread,
1839
)
1940
}

src/tools/tidy/src/deps.rs

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ const PERMITTED_DEPENDENCIES: &[&str] = &[
9797
"compiler_builtins",
9898
"cpuid-bool",
9999
"crc32fast",
100+
"crossbeam-channel",
100101
"crossbeam-deque",
101102
"crossbeam-epoch",
102103
"crossbeam-queue",

0 commit comments

Comments
 (0)