Skip to content

Commit

Permalink
Order rules in quote_token! and quote_token_spanned! by frequency.
Browse files Browse the repository at this point in the history
This reduces `cargo check` times by 4-9% on a few crates that use
`quote!` heavily.
  • Loading branch information
nnethercote committed Mar 17, 2022
1 parent 278057d commit fad6995
Showing 1 changed file with 61 additions and 56 deletions.
117 changes: 61 additions & 56 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -859,9 +859,21 @@ macro_rules! quote_token_with_context_spanned {
};
}

// These rules are ordered by approximate token frequency, at least for the
// first 10 or so, to improve compile times. Having `ident` first is by far the
// most important, because it's typically 2-3x more common than the next most
// common token.
#[macro_export]
#[doc(hidden)]
macro_rules! quote_token {
($tokens:ident $ident:ident) => {
$crate::__private::push_ident(&mut $tokens, stringify!($ident));
};

($tokens:ident ::) => {
$crate::__private::push_colon2(&mut $tokens);
};

($tokens:ident ( $($inner:tt)* )) => {
$crate::__private::push_group(
&mut $tokens,
Expand All @@ -886,6 +898,26 @@ macro_rules! quote_token {
);
};

($tokens:ident #) => {
$crate::__private::push_pound(&mut $tokens);
};

($tokens:ident ,) => {
$crate::__private::push_comma(&mut $tokens);
};

($tokens:ident .) => {
$crate::__private::push_dot(&mut $tokens);
};

($tokens:ident ;) => {
$crate::__private::push_semi(&mut $tokens);
};

($tokens:ident :) => {
$crate::__private::push_colon(&mut $tokens);
};

($tokens:ident +) => {
$crate::__private::push_add(&mut $tokens);
};
Expand Down Expand Up @@ -922,18 +954,6 @@ macro_rules! quote_token {
$crate::__private::push_caret_eq(&mut $tokens);
};

($tokens:ident :) => {
$crate::__private::push_colon(&mut $tokens);
};

($tokens:ident ::) => {
$crate::__private::push_colon2(&mut $tokens);
};

($tokens:ident ,) => {
$crate::__private::push_comma(&mut $tokens);
};

($tokens:ident /) => {
$crate::__private::push_div(&mut $tokens);
};
Expand All @@ -942,10 +962,6 @@ macro_rules! quote_token {
$crate::__private::push_div_eq(&mut $tokens);
};

($tokens:ident .) => {
$crate::__private::push_dot(&mut $tokens);
};

($tokens:ident ..) => {
$crate::__private::push_dot2(&mut $tokens);
};
Expand Down Expand Up @@ -1002,10 +1018,6 @@ macro_rules! quote_token {
$crate::__private::push_or_or(&mut $tokens);
};

($tokens:ident #) => {
$crate::__private::push_pound(&mut $tokens);
};

($tokens:ident ?) => {
$crate::__private::push_question(&mut $tokens);
};
Expand All @@ -1030,10 +1042,6 @@ macro_rules! quote_token {
$crate::__private::push_fat_arrow(&mut $tokens);
};

($tokens:ident ;) => {
$crate::__private::push_semi(&mut $tokens);
};

($tokens:ident <<) => {
$crate::__private::push_shl(&mut $tokens);
};
Expand Down Expand Up @@ -1062,10 +1070,6 @@ macro_rules! quote_token {
$crate::__private::push_sub_eq(&mut $tokens);
};

($tokens:ident $ident:ident) => {
$crate::__private::push_ident(&mut $tokens, stringify!($ident));
};

($tokens:ident $lifetime:lifetime) => {
$crate::__private::push_lifetime(&mut $tokens, stringify!($lifetime));
};
Expand All @@ -1079,9 +1083,18 @@ macro_rules! quote_token {
};
}

// See the comment above `quote_token!` about the rule ordering.
#[macro_export]
#[doc(hidden)]
macro_rules! quote_token_spanned {
($tokens:ident $span:ident $ident:ident) => {
$crate::__private::push_ident_spanned(&mut $tokens, $span, stringify!($ident));
};

($tokens:ident $span:ident ::) => {
$crate::__private::push_colon2_spanned(&mut $tokens, $span);
};

($tokens:ident $span:ident ( $($inner:tt)* )) => {
$crate::__private::push_group_spanned(
&mut $tokens,
Expand Down Expand Up @@ -1109,6 +1122,26 @@ macro_rules! quote_token_spanned {
);
};

($tokens:ident $span:ident #) => {
$crate::__private::push_pound_spanned(&mut $tokens, $span);
};

($tokens:ident $span:ident ,) => {
$crate::__private::push_comma_spanned(&mut $tokens, $span);
};

($tokens:ident $span:ident .) => {
$crate::__private::push_dot_spanned(&mut $tokens, $span);
};

($tokens:ident $span:ident ;) => {
$crate::__private::push_semi_spanned(&mut $tokens, $span);
};

($tokens:ident $span:ident :) => {
$crate::__private::push_colon_spanned(&mut $tokens, $span);
};

($tokens:ident $span:ident +) => {
$crate::__private::push_add_spanned(&mut $tokens, $span);
};
Expand Down Expand Up @@ -1145,18 +1178,6 @@ macro_rules! quote_token_spanned {
$crate::__private::push_caret_eq_spanned(&mut $tokens, $span);
};

($tokens:ident $span:ident :) => {
$crate::__private::push_colon_spanned(&mut $tokens, $span);
};

($tokens:ident $span:ident ::) => {
$crate::__private::push_colon2_spanned(&mut $tokens, $span);
};

($tokens:ident $span:ident ,) => {
$crate::__private::push_comma_spanned(&mut $tokens, $span);
};

($tokens:ident $span:ident /) => {
$crate::__private::push_div_spanned(&mut $tokens, $span);
};
Expand All @@ -1165,10 +1186,6 @@ macro_rules! quote_token_spanned {
$crate::__private::push_div_eq_spanned(&mut $tokens, $span);
};

($tokens:ident $span:ident .) => {
$crate::__private::push_dot_spanned(&mut $tokens, $span);
};

($tokens:ident $span:ident ..) => {
$crate::__private::push_dot2_spanned(&mut $tokens, $span);
};
Expand Down Expand Up @@ -1225,10 +1242,6 @@ macro_rules! quote_token_spanned {
$crate::__private::push_or_or_spanned(&mut $tokens, $span);
};

($tokens:ident $span:ident #) => {
$crate::__private::push_pound_spanned(&mut $tokens, $span);
};

($tokens:ident $span:ident ?) => {
$crate::__private::push_question_spanned(&mut $tokens, $span);
};
Expand All @@ -1253,10 +1266,6 @@ macro_rules! quote_token_spanned {
$crate::__private::push_fat_arrow_spanned(&mut $tokens, $span);
};

($tokens:ident $span:ident ;) => {
$crate::__private::push_semi_spanned(&mut $tokens, $span);
};

($tokens:ident $span:ident <<) => {
$crate::__private::push_shl_spanned(&mut $tokens, $span);
};
Expand Down Expand Up @@ -1285,10 +1294,6 @@ macro_rules! quote_token_spanned {
$crate::__private::push_sub_eq_spanned(&mut $tokens, $span);
};

($tokens:ident $span:ident $ident:ident) => {
$crate::__private::push_ident_spanned(&mut $tokens, $span, stringify!($ident));
};

($tokens:ident $span:ident $lifetime:lifetime) => {
$crate::__private::push_lifetime_spanned(&mut $tokens, $span, stringify!($lifetime));
};
Expand Down

0 comments on commit fad6995

Please sign in to comment.