Skip to content

Commit

Permalink
Special case quote!/quote_spanned! for 1 and 2 tts.
Browse files Browse the repository at this point in the history
These make crates that use `quote` compile faster.

I also tried specializing for 3 tts, but the rules are more complex and
the additional perf wins are much smaller than they are for 1 and 2 tts.
  • Loading branch information
nnethercote committed Apr 11, 2022
1 parent 31c3be4 commit 42fa703
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/lib.rs
Expand Up @@ -473,6 +473,28 @@ macro_rules! quote {
() => {
$crate::__private::TokenStream::new()
};

// Special case rule for a single tt, for performance.
($tt:tt) => {{
let mut _s = $crate::__private::TokenStream::new();
$crate::quote_token!($tt _s);
_s
}};

// Special case rules for two tts, for performance.
(# $var:ident) => {{
let mut _s = $crate::__private::TokenStream::new();
$crate::ToTokens::to_tokens(&$var, &mut _s);
_s
}};
($tt1:tt $tt2:tt) => {{
let mut _s = $crate::__private::TokenStream::new();
$crate::quote_token!($tt1 _s);
$crate::quote_token!($tt2 _s);
_s
}};

// Catch-all rule for all remaining inputs.
($($tt:tt)*) => {{
let mut _s = $crate::__private::TokenStream::new();
$crate::quote_each_token!(_s $($tt)*);
Expand Down Expand Up @@ -582,6 +604,31 @@ macro_rules! quote_spanned {
let _: $crate::__private::Span = $span;
$crate::__private::TokenStream::new()
}};

// Special case rule for a single tt, for performance.
($span:expr=> $tt:tt) => {{
let mut _s = $crate::__private::TokenStream::new();
let _span: $crate::__private::Span = $span;
$crate::quote_token_spanned!($tt _s _span);
_s
}};

// Special case rules for two tts, for performance.
($span:expr=> # $var:ident) => {{
let mut _s = $crate::__private::TokenStream::new();
let _span: $crate::__private::Span = $span;
$crate::ToTokens::to_tokens(&$var, &mut _s);
_s
}};
($span:expr=> $tt1:tt $tt2:tt) => {{
let mut _s = $crate::__private::TokenStream::new();
let _span: $crate::__private::Span = $span;
$crate::quote_token_spanned!($tt1 _s _span);
$crate::quote_token_spanned!($tt2 _s _span);
_s
}};

// Catch-all rule for all remaining inputs.
($span:expr=> $($tt:tt)*) => {{
let mut _s = $crate::__private::TokenStream::new();
let _span: $crate::__private::Span = $span;
Expand Down

0 comments on commit 42fa703

Please sign in to comment.