Faster env snapshotting in proc-macro-srv

This commit is contained in:
Lukas Wirth 2024-06-30 16:05:46 +02:00
parent 678420e66a
commit 2fb38ceb66
6 changed files with 147 additions and 117 deletions

View file

@ -156,12 +156,11 @@ impl ProcMacro {
let call_site = span_data_table.insert_full(call_site).0; let call_site = span_data_table.insert_full(call_site).0;
let mixed_site = span_data_table.insert_full(mixed_site).0; let mixed_site = span_data_table.insert_full(mixed_site).0;
let task = ExpandMacro { let task = ExpandMacro {
data: msg::ExpandMacroData {
macro_body: FlatTree::new(subtree, version, &mut span_data_table), macro_body: FlatTree::new(subtree, version, &mut span_data_table),
macro_name: self.name.to_string(), macro_name: self.name.to_string(),
attributes: attr.map(|subtree| FlatTree::new(subtree, version, &mut span_data_table)), attributes: attr
lib: self.dylib_path.to_path_buf().into(), .map(|subtree| FlatTree::new(subtree, version, &mut span_data_table)),
env: env.into(),
current_dir,
has_global_spans: ExpnGlobals { has_global_spans: ExpnGlobals {
serialize: version >= HAS_GLOBAL_SPANS, serialize: version >= HAS_GLOBAL_SPANS,
def_site, def_site,
@ -173,6 +172,10 @@ impl ProcMacro {
} else { } else {
Vec::new() Vec::new()
}, },
},
lib: self.dylib_path.to_path_buf().into(),
env: env.into(),
current_dir,
}; };
let response = self.process.send_task(msg::Request::ExpandMacro(Box::new(task)))?; let response = self.process.send_task(msg::Request::ExpandMacro(Box::new(task)))?;

View file

@ -72,6 +72,16 @@ pub struct PanicMessage(pub String);
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct ExpandMacro { pub struct ExpandMacro {
pub lib: Utf8PathBuf,
/// Environment variables to set during macro expansion.
pub env: Vec<(String, String)>,
pub current_dir: Option<String>,
#[serde(flatten)]
pub data: ExpandMacroData,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct ExpandMacroData {
/// Argument of macro call. /// Argument of macro call.
/// ///
/// In custom derive this will be a struct or enum; in attribute-like macro - underlying /// In custom derive this will be a struct or enum; in attribute-like macro - underlying
@ -86,13 +96,6 @@ pub struct ExpandMacro {
/// Possible attributes for the attribute-like macros. /// Possible attributes for the attribute-like macros.
pub attributes: Option<FlatTree>, pub attributes: Option<FlatTree>,
pub lib: Utf8PathBuf,
/// Environment variables to set during macro expansion.
pub env: Vec<(String, String)>,
pub current_dir: Option<String>,
/// marker for serde skip stuff /// marker for serde skip stuff
#[serde(skip_serializing_if = "ExpnGlobals::skip_serializing_if")] #[serde(skip_serializing_if = "ExpnGlobals::skip_serializing_if")]
#[serde(default)] #[serde(default)]
@ -268,12 +271,10 @@ mod tests {
let tt = fixture_token_tree(); let tt = fixture_token_tree();
let mut span_data_table = Default::default(); let mut span_data_table = Default::default();
let task = ExpandMacro { let task = ExpandMacro {
data: ExpandMacroData {
macro_body: FlatTree::new(&tt, CURRENT_API_VERSION, &mut span_data_table), macro_body: FlatTree::new(&tt, CURRENT_API_VERSION, &mut span_data_table),
macro_name: Default::default(), macro_name: Default::default(),
attributes: None, attributes: None,
lib: Utf8PathBuf::from_path_buf(std::env::current_dir().unwrap()).unwrap(),
env: Default::default(),
current_dir: Default::default(),
has_global_spans: ExpnGlobals { has_global_spans: ExpnGlobals {
serialize: true, serialize: true,
def_site: 0, def_site: 0,
@ -281,12 +282,19 @@ mod tests {
mixed_site: 0, mixed_site: 0,
}, },
span_data_table: Vec::new(), span_data_table: Vec::new(),
},
lib: Utf8PathBuf::from_path_buf(std::env::current_dir().unwrap()).unwrap(),
env: Default::default(),
current_dir: Default::default(),
}; };
let json = serde_json::to_string(&task).unwrap(); let json = serde_json::to_string(&task).unwrap();
// println!("{}", json); // println!("{}", json);
let back: ExpandMacro = serde_json::from_str(&json).unwrap(); let back: ExpandMacro = serde_json::from_str(&json).unwrap();
assert_eq!(tt, back.macro_body.to_subtree_resolved(CURRENT_API_VERSION, &span_data_table)); assert_eq!(
tt,
back.data.macro_body.to_subtree_resolved(CURRENT_API_VERSION, &span_data_table)
);
} }
} }

View file

@ -33,12 +33,14 @@ fn run() -> io::Result<()> {
#[cfg(any(feature = "sysroot-abi", rust_analyzer))] #[cfg(any(feature = "sysroot-abi", rust_analyzer))]
fn run() -> io::Result<()> { fn run() -> io::Result<()> {
use proc_macro_api::msg::{self, Message}; use proc_macro_api::msg::{self, Message};
use proc_macro_srv::EnvSnapshot;
let read_request = |buf: &mut String| msg::Request::read(&mut io::stdin().lock(), buf); let read_request = |buf: &mut String| msg::Request::read(&mut io::stdin().lock(), buf);
let write_response = |msg: msg::Response| msg.write(&mut io::stdout().lock()); let write_response = |msg: msg::Response| msg.write(&mut io::stdout().lock());
let mut srv = proc_macro_srv::ProcMacroSrv::default(); let env = EnvSnapshot::new();
let mut srv = proc_macro_srv::ProcMacroSrv::new(&env);
let mut buf = String::new(); let mut buf = String::new();
while let Some(req) = read_request(&mut buf)? { while let Some(req) = read_request(&mut buf)? {

View file

@ -32,7 +32,9 @@ use std::{
collections::{hash_map::Entry, HashMap}, collections::{hash_map::Entry, HashMap},
env, env,
ffi::OsString, ffi::OsString,
fs, thread, fs,
path::{Path, PathBuf},
thread,
time::SystemTime, time::SystemTime,
}; };
@ -50,15 +52,21 @@ use crate::server_impl::TokenStream;
pub const RUSTC_VERSION_STRING: &str = env!("RUSTC_VERSION"); pub const RUSTC_VERSION_STRING: &str = env!("RUSTC_VERSION");
#[derive(Default)] pub struct ProcMacroSrv<'env> {
pub struct ProcMacroSrv {
expanders: HashMap<(Utf8PathBuf, SystemTime), dylib::Expander>, expanders: HashMap<(Utf8PathBuf, SystemTime), dylib::Expander>,
span_mode: SpanMode, span_mode: SpanMode,
env: &'env EnvSnapshot,
}
impl<'env> ProcMacroSrv<'env> {
pub fn new(env: &'env EnvSnapshot) -> Self {
Self { expanders: Default::default(), span_mode: Default::default(), env }
}
} }
const EXPANDER_STACK_SIZE: usize = 8 * 1024 * 1024; const EXPANDER_STACK_SIZE: usize = 8 * 1024 * 1024;
impl ProcMacroSrv { impl<'env> ProcMacroSrv<'env> {
pub fn set_span_mode(&mut self, span_mode: SpanMode) { pub fn set_span_mode(&mut self, span_mode: SpanMode) {
self.span_mode = span_mode; self.span_mode = span_mode;
} }
@ -69,52 +77,24 @@ impl ProcMacroSrv {
pub fn expand( pub fn expand(
&mut self, &mut self,
task: msg::ExpandMacro, msg::ExpandMacro { lib, env, current_dir, data }: msg::ExpandMacro,
) -> Result<(msg::FlatTree, Vec<u32>), msg::PanicMessage> { ) -> Result<(msg::FlatTree, Vec<u32>), msg::PanicMessage> {
let span_mode = self.span_mode; let span_mode = self.span_mode;
let expander = self.expander(task.lib.as_ref()).map_err(|err| { let snapped_env = self.env;
let expander = self.expander(lib.as_ref()).map_err(|err| {
debug_assert!(false, "should list macros before asking to expand"); debug_assert!(false, "should list macros before asking to expand");
msg::PanicMessage(format!("failed to load macro: {err}")) msg::PanicMessage(format!("failed to load macro: {err}"))
})?; })?;
let prev_env = EnvSnapshot::new(); let prev_env = EnvChange::apply(snapped_env, env, current_dir.as_ref().map(<_>::as_ref));
for (k, v) in &task.env {
env::set_var(k, v);
}
let prev_working_dir = match &task.current_dir {
Some(dir) => {
let prev_working_dir = std::env::current_dir().ok();
if let Err(err) = std::env::set_current_dir(dir) {
eprintln!("Failed to set the current working dir to {dir}. Error: {err:?}")
}
prev_working_dir
}
None => None,
};
let ExpnGlobals { def_site, call_site, mixed_site, .. } = task.has_global_spans;
let result = match span_mode { let result = match span_mode {
SpanMode::Id => { SpanMode::Id => expand_id(data, expander).map(|it| (it, vec![])),
expand_id(task, expander, def_site, call_site, mixed_site).map(|it| (it, vec![])) SpanMode::RustAnalyzer => expand_ra_span(data, expander),
}
SpanMode::RustAnalyzer => {
expand_ra_span(task, expander, def_site, call_site, mixed_site)
}
}; };
prev_env.rollback(); prev_env.rollback();
if let Some(dir) = prev_working_dir {
if let Err(err) = std::env::set_current_dir(&dir) {
eprintln!(
"Failed to set the current working dir to {}. Error: {:?}",
dir.display(),
err
)
}
}
result.map_err(msg::PanicMessage) result.map_err(msg::PanicMessage)
} }
@ -168,32 +148,28 @@ impl ProcMacroSrvSpan for Span {
} }
fn expand_id( fn expand_id(
task: msg::ExpandMacro, msg::ExpandMacroData {
macro_body,
macro_name,
attributes,
has_global_spans: ExpnGlobals { serialize: _, def_site, call_site, mixed_site },
span_data_table: _,
}: msg::ExpandMacroData,
expander: &dylib::Expander, expander: &dylib::Expander,
def_site: usize,
call_site: usize,
mixed_site: usize,
) -> Result<msg::FlatTree, String> { ) -> Result<msg::FlatTree, String> {
let def_site = TokenId(def_site as u32); let def_site = TokenId(def_site as u32);
let call_site = TokenId(call_site as u32); let call_site = TokenId(call_site as u32);
let mixed_site = TokenId(mixed_site as u32); let mixed_site = TokenId(mixed_site as u32);
let macro_body = task.macro_body.to_subtree_unresolved(CURRENT_API_VERSION); let macro_body = macro_body.to_subtree_unresolved(CURRENT_API_VERSION);
let attributes = task.attributes.map(|it| it.to_subtree_unresolved(CURRENT_API_VERSION)); let attributes = attributes.map(|it| it.to_subtree_unresolved(CURRENT_API_VERSION));
let result = thread::scope(|s| { let result = thread::scope(|s| {
let thread = thread::Builder::new() let thread = thread::Builder::new()
.stack_size(EXPANDER_STACK_SIZE) .stack_size(EXPANDER_STACK_SIZE)
.name(task.macro_name.clone()) .name(macro_name.clone())
.spawn_scoped(s, || { .spawn_scoped(s, || {
expander expander
.expand( .expand(&macro_name, macro_body, attributes, def_site, call_site, mixed_site)
&task.macro_name,
macro_body,
attributes,
def_site,
call_site,
mixed_site,
)
.map(|it| msg::FlatTree::new_raw(&it, CURRENT_API_VERSION)) .map(|it| msg::FlatTree::new_raw(&it, CURRENT_API_VERSION))
}); });
let res = match thread { let res = match thread {
@ -210,35 +186,31 @@ fn expand_id(
} }
fn expand_ra_span( fn expand_ra_span(
task: msg::ExpandMacro, msg::ExpandMacroData {
macro_body,
macro_name,
attributes,
has_global_spans: ExpnGlobals { serialize: _, def_site, call_site, mixed_site },
span_data_table,
}: msg::ExpandMacroData,
expander: &dylib::Expander, expander: &dylib::Expander,
def_site: usize,
call_site: usize,
mixed_site: usize,
) -> Result<(msg::FlatTree, Vec<u32>), String> { ) -> Result<(msg::FlatTree, Vec<u32>), String> {
let mut span_data_table = deserialize_span_data_index_map(&task.span_data_table); let mut span_data_table = deserialize_span_data_index_map(&span_data_table);
let def_site = span_data_table[def_site]; let def_site = span_data_table[def_site];
let call_site = span_data_table[call_site]; let call_site = span_data_table[call_site];
let mixed_site = span_data_table[mixed_site]; let mixed_site = span_data_table[mixed_site];
let macro_body = task.macro_body.to_subtree_resolved(CURRENT_API_VERSION, &span_data_table); let macro_body = macro_body.to_subtree_resolved(CURRENT_API_VERSION, &span_data_table);
let attributes = let attributes =
task.attributes.map(|it| it.to_subtree_resolved(CURRENT_API_VERSION, &span_data_table)); attributes.map(|it| it.to_subtree_resolved(CURRENT_API_VERSION, &span_data_table));
let result = thread::scope(|s| { let result = thread::scope(|s| {
let thread = thread::Builder::new() let thread = thread::Builder::new()
.stack_size(EXPANDER_STACK_SIZE) .stack_size(EXPANDER_STACK_SIZE)
.name(task.macro_name.clone()) .name(macro_name.clone())
.spawn_scoped(s, || { .spawn_scoped(s, || {
expander expander
.expand( .expand(&macro_name, macro_body, attributes, def_site, call_site, mixed_site)
&task.macro_name,
macro_body,
attributes,
def_site,
call_site,
mixed_site,
)
.map(|it| { .map(|it| {
( (
msg::FlatTree::new(&it, CURRENT_API_VERSION, &mut span_data_table), msg::FlatTree::new(&it, CURRENT_API_VERSION, &mut span_data_table),
@ -269,31 +241,74 @@ impl PanicMessage {
} }
} }
struct EnvSnapshot { pub struct EnvSnapshot {
vars: HashMap<OsString, OsString>, vars: HashMap<OsString, OsString>,
} }
impl EnvSnapshot { impl EnvSnapshot {
fn new() -> EnvSnapshot { pub fn new() -> EnvSnapshot {
EnvSnapshot { vars: env::vars_os().collect() } EnvSnapshot { vars: env::vars_os().collect() }
} }
}
struct EnvChange<'snap> {
changed_vars: Vec<String>,
prev_working_dir: Option<PathBuf>,
snap: &'snap EnvSnapshot,
}
impl<'snap> EnvChange<'snap> {
fn apply(
snap: &'snap EnvSnapshot,
new_vars: Vec<(String, String)>,
current_dir: Option<&Path>,
) -> EnvChange<'snap> {
let prev_working_dir = match current_dir {
Some(dir) => {
let prev_working_dir = std::env::current_dir().ok();
if let Err(err) = std::env::set_current_dir(dir) {
eprintln!(
"Failed to set the current working dir to {}. Error: {err:?}",
dir.display()
)
}
prev_working_dir
}
None => None,
};
EnvChange {
snap,
changed_vars: new_vars
.into_iter()
.map(|(k, v)| {
env::set_var(&k, v);
k
})
.collect(),
prev_working_dir,
}
}
fn rollback(self) {} fn rollback(self) {}
} }
impl Drop for EnvSnapshot { impl Drop for EnvChange<'_> {
fn drop(&mut self) { fn drop(&mut self) {
for (name, value) in env::vars_os() { for name in self.changed_vars.drain(..) {
let old_value = self.vars.remove(&name); match self.snap.vars.get::<std::ffi::OsStr>(name.as_ref()) {
if old_value != Some(value) { Some(prev_val) => env::set_var(name, prev_val),
match old_value {
None => env::remove_var(name), None => env::remove_var(name),
Some(old_value) => env::set_var(name, old_value),
} }
} }
if let Some(dir) = &self.prev_working_dir {
if let Err(err) = std::env::set_current_dir(&dir) {
eprintln!(
"Failed to set the current working dir to {}. Error: {:?}",
dir.display(),
err
)
} }
for (name, old_value) in self.vars.drain() {
env::set_var(name, old_value)
} }
} }
} }

View file

@ -1,9 +1,10 @@
//! Proc macro ABI //! Proc macro ABI
use libloading::Library;
use proc_macro::bridge; use proc_macro::bridge;
use proc_macro_api::ProcMacroKind; use proc_macro_api::ProcMacroKind;
use libloading::Library;
use crate::{dylib::LoadProcMacroDylibError, ProcMacroSrvSpan}; use crate::{dylib::LoadProcMacroDylibError, ProcMacroSrvSpan};
pub(crate) struct ProcMacros { pub(crate) struct ProcMacros {

View file

@ -5,7 +5,7 @@ use proc_macro_api::msg::TokenId;
use span::{ErasedFileAstId, FileId, Span, SpanAnchor, SyntaxContextId}; use span::{ErasedFileAstId, FileId, Span, SpanAnchor, SyntaxContextId};
use tt::TextRange; use tt::TextRange;
use crate::{dylib, proc_macro_test_dylib_path, ProcMacroSrv}; use crate::{dylib, proc_macro_test_dylib_path, EnvSnapshot, ProcMacroSrv};
fn parse_string(call_site: TokenId, src: &str) -> crate::server_impl::TokenStream<TokenId> { fn parse_string(call_site: TokenId, src: &str) -> crate::server_impl::TokenStream<TokenId> {
crate::server_impl::TokenStream::with_subtree( crate::server_impl::TokenStream::with_subtree(
@ -96,7 +96,8 @@ fn assert_expand_impl(
pub(crate) fn list() -> Vec<String> { pub(crate) fn list() -> Vec<String> {
let dylib_path = proc_macro_test_dylib_path(); let dylib_path = proc_macro_test_dylib_path();
let mut srv = ProcMacroSrv::default(); let env = EnvSnapshot::new();
let mut srv = ProcMacroSrv::new(&env);
let res = srv.list_macros(&dylib_path).unwrap(); let res = srv.list_macros(&dylib_path).unwrap();
res.into_iter().map(|(name, kind)| format!("{name} [{kind:?}]")).collect() res.into_iter().map(|(name, kind)| format!("{name} [{kind:?}]")).collect()
} }