From 8db4f28c53176ef190b7cba1fdc2b95d66af030d Mon Sep 17 00:00:00 2001 From: Nika Layzell Date: Thu, 1 Jul 2021 12:31:36 -0400 Subject: [PATCH] proc_macro: use crossbeam-channel for the CrossThread execution strategy Compared to mpsc::channel, crossbeam-channel has significantly lower overhead. --- Cargo.lock | 1 + compiler/rustc_expand/Cargo.toml | 1 + compiler/rustc_expand/src/proc_macro.rs | 23 ++++++++++++++++++++++- src/tools/tidy/src/deps.rs | 1 + 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 8db8a56eaa8ec..060d9b033c223 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3840,6 +3840,7 @@ dependencies = [ name = "rustc_expand" version = "0.0.0" dependencies = [ + "crossbeam-channel", "rustc_ast", "rustc_ast_passes", "rustc_ast_pretty", diff --git a/compiler/rustc_expand/Cargo.toml b/compiler/rustc_expand/Cargo.toml index 59c1604e8444c..e217d8ad23aa7 100644 --- a/compiler/rustc_expand/Cargo.toml +++ b/compiler/rustc_expand/Cargo.toml @@ -25,3 +25,4 @@ rustc_parse = { path = "../rustc_parse" } rustc_session = { path = "../rustc_session" } smallvec = { version = "1.6.1", features = ["union", "may_dangle"] } rustc_ast = { path = "../rustc_ast" } +crossbeam-channel = "0.5.0" diff --git a/compiler/rustc_expand/src/proc_macro.rs b/compiler/rustc_expand/src/proc_macro.rs index a23d28bf81f7c..e597e9a58b6af 100644 --- a/compiler/rustc_expand/src/proc_macro.rs +++ b/compiler/rustc_expand/src/proc_macro.rs @@ -12,8 +12,29 @@ use rustc_parse::parser::ForceCollect; use rustc_span::def_id::CrateNum; use rustc_span::{Span, DUMMY_SP}; +struct CrossbeamMessagePipe { + tx: crossbeam_channel::Sender, + rx: crossbeam_channel::Receiver, +} + +impl pm::bridge::server::MessagePipe for CrossbeamMessagePipe { + fn new() -> (Self, Self) { + let (tx1, rx1) = crossbeam_channel::unbounded(); + let (tx2, rx2) = crossbeam_channel::unbounded(); + (CrossbeamMessagePipe { tx: tx1, rx: rx2 }, CrossbeamMessagePipe { tx: tx2, rx: rx1 }) + } + + fn send(&mut self, value: T) { + self.tx.send(value).unwrap(); + } + + fn recv(&mut self) -> Option { + self.rx.recv().ok() + } +} + fn exec_strategy(ecx: &ExtCtxt<'_>) -> impl pm::bridge::server::ExecutionStrategy { - >>::new( + >>::new( ecx.sess.opts.debugging_opts.proc_macro_cross_thread, ) } diff --git a/src/tools/tidy/src/deps.rs b/src/tools/tidy/src/deps.rs index ea587210b4f4e..578f56a7a8630 100644 --- a/src/tools/tidy/src/deps.rs +++ b/src/tools/tidy/src/deps.rs @@ -97,6 +97,7 @@ const PERMITTED_DEPENDENCIES: &[&str] = &[ "compiler_builtins", "cpuid-bool", "crc32fast", + "crossbeam-channel", "crossbeam-deque", "crossbeam-epoch", "crossbeam-queue",