rust-analyzer/crates/proc-macro-srv/src/proc_macros.rs
Chayim Refael Friedman ceba289f80 Store token trees in contiguous Vec instead of as a tree
I expected this to be faster (due to less allocations and better cache locality), but benchmarked it is not (neither it is slower). Memory usage, however, drops by ~50mb (of `analysis-stats .`). I guess tt construction is just not hot.

This also simplifies using even less memory for token trees by compressing equal span, which I plan to do right after.

Some workflows are more easily expressed with a flat tt, while some are better expressed with a tree. With the right helpers, though (which was mostly a matter of trial and error), even the worst workflows become very easy indeed.
2025-01-02 19:21:46 +02:00

120 lines
4.4 KiB
Rust

//! Proc macro ABI
use proc_macro::bridge;
use libloading::Library;
use crate::{
dylib::LoadProcMacroDylibError, server_impl::TopSubtree, ProcMacroKind, ProcMacroSrvSpan,
};
#[repr(transparent)]
pub(crate) struct ProcMacros([bridge::client::ProcMacro]);
impl From<bridge::PanicMessage> for crate::PanicMessage {
fn from(p: bridge::PanicMessage) -> Self {
Self { message: p.as_str().map(|s| s.to_owned()) }
}
}
impl ProcMacros {
/// Load a new ABI.
///
/// # Arguments
///
/// *`lib` - The dynamic library containing the macro implementations
/// *`symbol_name` - The symbol name the macros can be found attributes
/// *`info` - RustCInfo about the compiler that was used to compile the
/// macro crate. This is the information we use to figure out
/// which ABI to return
pub(crate) fn from_lib<'l>(
lib: &'l Library,
symbol_name: String,
version_string: &str,
) -> Result<&'l ProcMacros, LoadProcMacroDylibError> {
if version_string != crate::RUSTC_VERSION_STRING {
return Err(LoadProcMacroDylibError::AbiMismatch(version_string.to_owned()));
}
unsafe { lib.get::<&'l &'l ProcMacros>(symbol_name.as_bytes()) }
.map(|it| **it)
.map_err(Into::into)
}
pub(crate) fn expand<S: ProcMacroSrvSpan>(
&self,
macro_name: &str,
macro_body: TopSubtree<S>,
attributes: Option<TopSubtree<S>>,
def_site: S,
call_site: S,
mixed_site: S,
) -> Result<TopSubtree<S>, crate::PanicMessage> {
let parsed_body = crate::server_impl::TokenStream::with_subtree(macro_body);
let parsed_attributes = attributes
.map_or_else(crate::server_impl::TokenStream::new, |attr| {
crate::server_impl::TokenStream::with_subtree(attr)
});
for proc_macro in &self.0 {
match proc_macro {
bridge::client::ProcMacro::CustomDerive { trait_name, client, .. }
if *trait_name == macro_name =>
{
let res = client.run(
&bridge::server::SameThread,
S::make_server(call_site, def_site, mixed_site),
parsed_body,
cfg!(debug_assertions),
);
return res
.map(|it| it.into_subtree(call_site))
.map_err(crate::PanicMessage::from);
}
bridge::client::ProcMacro::Bang { name, client } if *name == macro_name => {
let res = client.run(
&bridge::server::SameThread,
S::make_server(call_site, def_site, mixed_site),
parsed_body,
cfg!(debug_assertions),
);
return res
.map(|it| it.into_subtree(call_site))
.map_err(crate::PanicMessage::from);
}
bridge::client::ProcMacro::Attr { name, client } if *name == macro_name => {
let res = client.run(
&bridge::server::SameThread,
S::make_server(call_site, def_site, mixed_site),
parsed_attributes,
parsed_body,
cfg!(debug_assertions),
);
return res
.map(|it| it.into_subtree(call_site))
.map_err(crate::PanicMessage::from);
}
_ => continue,
}
}
Err(bridge::PanicMessage::String(format!("proc-macro `{macro_name}` is missing")).into())
}
pub(crate) fn list_macros(&self) -> Vec<(String, ProcMacroKind)> {
self.0
.iter()
.map(|proc_macro| match proc_macro {
bridge::client::ProcMacro::CustomDerive { trait_name, .. } => {
(trait_name.to_string(), ProcMacroKind::CustomDerive)
}
bridge::client::ProcMacro::Bang { name, .. } => {
(name.to_string(), ProcMacroKind::Bang)
}
bridge::client::ProcMacro::Attr { name, .. } => {
(name.to_string(), ProcMacroKind::Attr)
}
})
.collect()
}
}