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 4d75f2e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
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
2 changes: 1 addition & 1 deletion tests/ui/not-quotable.stderr
Expand Up @@ -14,4 +14,4 @@ error[E0277]: the trait bound `Ipv4Addr: ToTokens` is not satisfied
RepInterp<T>
String
and 23 others
= note: this error originates in the macro `$crate::quote_token_with_context` (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in the macro `quote` (in Nightly builds, run with -Z macro-backtrace for more info)

0 comments on commit 4d75f2e

Please sign in to comment.