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 9a2530a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 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
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)
10 changes: 0 additions & 10 deletions tests/ui/not-repeatable.stderr
Expand Up @@ -23,16 +23,6 @@ error[E0599]: the method `quote_into_iter` exists for struct `Ipv4Addr`, but its
`&mut Ipv4Addr: Iterator`
which is required by `&mut Ipv4Addr: quote::__private::ext::RepIteratorExt`
note: the following traits must be implemented
--> $RUST/core/src/iter/traits/iterator.rs
|
| / pub trait Iterator {
| | /// The type of the elements being iterated over.
| | #[stable(feature = "rust1", since = "1.0.0")]
| | type Item;
... |
| | }
| | }
| |__^
|
::: src/to_tokens.rs
|
Expand Down

0 comments on commit 9a2530a

Please sign in to comment.