1
Fork 0

Split NamedMatch::MatchNonterminal in two.

The `Lrc` is only relevant within `transcribe()`. There, the `Lrc` is
helpful for the non-`NtTT` cases, because the entire nonterminal is
cloned. But for the `NtTT` cases the inner token tree is cloned (a full
clone) and so the `Lrc` is of no help.

This commit splits the `NtTT` and non-`NtTT` cases, avoiding the useless
`Lrc` in the former case, for the following effect on macro-heavy
crates.
- It reduces the total number of allocations a lot.
- It increases the size of some of the remaining allocations.
- It doesn't affect *peak* memory usage, because the larger allocations
  are short-lived.

This overall gives a speed win.
This commit is contained in:
Nicholas Nethercote 2022-03-23 11:46:22 +11:00
parent 904e70a7b0
commit 6817442ec7
3 changed files with 81 additions and 71 deletions

View file

@ -105,7 +105,7 @@ type NamedMatchVec = SmallVec<[NamedMatch; 4]>;
// This type is used a lot. Make sure it doesn't unintentionally get bigger. // This type is used a lot. Make sure it doesn't unintentionally get bigger.
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
rustc_data_structures::static_assert_size!(NamedMatchVec, 72); rustc_data_structures::static_assert_size!(NamedMatchVec, 168);
/// Represents a single "position" (aka "matcher position", aka "item"), as /// Represents a single "position" (aka "matcher position", aka "item"), as
/// described in the module documentation. /// described in the module documentation.
@ -278,22 +278,20 @@ pub(super) fn count_names(ms: &[TokenTree]) -> usize {
}) })
} }
/// `NamedMatch` is a pattern-match result for a single `token::MATCH_NONTERMINAL`: /// `NamedMatch` is a pattern-match result for a single metavar. All
/// so it is associated with a single ident in a parse, and all /// `MatchedNtNonTt`s in the `NamedMatch` have the same non-terminal type
/// `MatchedNonterminal`s in the `NamedMatch` have the same non-terminal type /// (expr, item, etc).
/// (expr, item, etc). Each leaf in a single `NamedMatch` corresponds to a
/// single `token::MATCH_NONTERMINAL` in the `TokenTree` that produced it.
/// ///
/// The in-memory structure of a particular `NamedMatch` represents the match /// The in-memory structure of a particular `NamedMatch` represents the match
/// that occurred when a particular subset of a matcher was applied to a /// that occurred when a particular subset of a matcher was applied to a
/// particular token tree. /// particular token tree.
/// ///
/// The width of each `MatchedSeq` in the `NamedMatch`, and the identity of /// The width of each `MatchedSeq` in the `NamedMatch`, and the identity of
/// the `MatchedNonterminal`s, will depend on the token tree it was applied /// the `MatchedNtNonTts`s, will depend on the token tree it was applied
/// to: each `MatchedSeq` corresponds to a single `TTSeq` in the originating /// to: each `MatchedSeq` corresponds to a single repetition in the originating
/// token tree. The depth of the `NamedMatch` structure will therefore depend /// token tree. The depth of the `NamedMatch` structure will therefore depend
/// only on the nesting depth of `ast::TTSeq`s in the originating /// only on the nesting depth of repetitions in the originating token tree it
/// token tree it was derived from. /// was derived from.
/// ///
/// In layman's terms: `NamedMatch` will form a tree representing nested matches of a particular /// In layman's terms: `NamedMatch` will form a tree representing nested matches of a particular
/// meta variable. For example, if we are matching the following macro against the following /// meta variable. For example, if we are matching the following macro against the following
@ -312,24 +310,32 @@ pub(super) fn count_names(ms: &[TokenTree]) -> usize {
/// ```rust /// ```rust
/// MatchedSeq([ /// MatchedSeq([
/// MatchedSeq([ /// MatchedSeq([
/// MatchedNonterminal(a), /// MatchedNtNonTt(a),
/// MatchedNonterminal(b), /// MatchedNtNonTt(b),
/// MatchedNonterminal(c), /// MatchedNtNonTt(c),
/// MatchedNonterminal(d), /// MatchedNtNonTt(d),
/// ]), /// ]),
/// MatchedSeq([ /// MatchedSeq([
/// MatchedNonterminal(a), /// MatchedNtNonTt(a),
/// MatchedNonterminal(b), /// MatchedNtNonTt(b),
/// MatchedNonterminal(c), /// MatchedNtNonTt(c),
/// MatchedNonterminal(d), /// MatchedNtNonTt(d),
/// MatchedNonterminal(e), /// MatchedNtNonTt(e),
/// ]) /// ])
/// ]) /// ])
/// ``` /// ```
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
crate enum NamedMatch { crate enum NamedMatch {
MatchedSeq(Lrc<NamedMatchVec>), MatchedSeq(Lrc<NamedMatchVec>),
MatchedNonterminal(Lrc<Nonterminal>),
// This variant should never hold an `NtTT`. `MatchedNtTt` should be used
// for that case.
MatchedNtNonTt(Lrc<Nonterminal>),
// `NtTT` is handled without any cloning when transcribing, unlike other
// nonterminals. Therefore, an `Lrc` isn't helpful and causes unnecessary
// allocations. Hence this separate variant.
MatchedNtTt(rustc_ast::tokenstream::TokenTree),
} }
/// Takes a slice of token trees `ms` representing a matcher which successfully matched input /// Takes a slice of token trees `ms` representing a matcher which successfully matched input
@ -669,7 +675,11 @@ impl<'tt> TtParser<'tt> {
} }
Ok(nt) => nt, Ok(nt) => nt,
}; };
item.push_match(match_cur, MatchedNonterminal(Lrc::new(nt))); let m = match nt {
Nonterminal::NtTT(tt) => MatchedNtTt(tt),
_ => MatchedNtNonTt(Lrc::new(nt)),
};
item.push_match(match_cur, m);
item.idx += 1; item.idx += 1;
item.match_cur += 1; item.match_cur += 1;
} else { } else {

View file

@ -4,11 +4,11 @@ use crate::expand::{ensure_complete_parse, parse_ast_fragment, AstFragment, AstF
use crate::mbe; use crate::mbe;
use crate::mbe::macro_check; use crate::mbe::macro_check;
use crate::mbe::macro_parser::{Error, ErrorReported, Failure, Success, TtParser}; use crate::mbe::macro_parser::{Error, ErrorReported, Failure, Success, TtParser};
use crate::mbe::macro_parser::{MatchedNonterminal, MatchedSeq}; use crate::mbe::macro_parser::{MatchedNtTt, MatchedSeq};
use crate::mbe::transcribe::transcribe; use crate::mbe::transcribe::transcribe;
use rustc_ast as ast; use rustc_ast as ast;
use rustc_ast::token::{self, NonterminalKind, NtTT, Token, TokenKind::*}; use rustc_ast::token::{self, NonterminalKind, Token, TokenKind::*};
use rustc_ast::tokenstream::{DelimSpan, TokenStream}; use rustc_ast::tokenstream::{DelimSpan, TokenStream};
use rustc_ast::{NodeId, DUMMY_NODE_ID}; use rustc_ast::{NodeId, DUMMY_NODE_ID};
use rustc_ast_pretty::pprust; use rustc_ast_pretty::pprust;
@ -470,22 +470,20 @@ pub fn compile_declarative_macro(
MatchedSeq(ref s) => s MatchedSeq(ref s) => s
.iter() .iter()
.map(|m| { .map(|m| {
if let MatchedNonterminal(ref nt) = *m { if let MatchedNtTt(ref tt) = *m {
if let NtTT(ref tt) = **nt { let mut tts = vec![];
let mut tts = vec![]; mbe::quoted::parse(
mbe::quoted::parse( tt.clone().into(),
tt.clone().into(), true,
true, &sess.parse_sess,
&sess.parse_sess, def.id,
def.id, features,
features, edition,
edition, &mut tts,
&mut tts, );
); let tt = tts.pop().unwrap();
let tt = tts.pop().unwrap(); valid &= check_lhs_nt_follows(&sess.parse_sess, features, &def, &tt);
valid &= check_lhs_nt_follows(&sess.parse_sess, features, &def, &tt); return tt;
return tt;
}
} }
sess.parse_sess.span_diagnostic.span_bug(def.span, "wrong-structured lhs") sess.parse_sess.span_diagnostic.span_bug(def.span, "wrong-structured lhs")
}) })
@ -497,20 +495,18 @@ pub fn compile_declarative_macro(
MatchedSeq(ref s) => s MatchedSeq(ref s) => s
.iter() .iter()
.map(|m| { .map(|m| {
if let MatchedNonterminal(ref nt) = *m { if let MatchedNtTt(ref tt) = *m {
if let NtTT(ref tt) = **nt { let mut tts = vec![];
let mut tts = vec![]; mbe::quoted::parse(
mbe::quoted::parse( tt.clone().into(),
tt.clone().into(), false,
false, &sess.parse_sess,
&sess.parse_sess, def.id,
def.id, features,
features, edition,
edition, &mut tts,
&mut tts, );
); return tts.pop().unwrap();
return tts.pop().unwrap();
}
} }
sess.parse_sess.span_diagnostic.span_bug(def.span, "wrong-structured lhs") sess.parse_sess.span_diagnostic.span_bug(def.span, "wrong-structured lhs")
}) })

View file

@ -1,8 +1,8 @@
use crate::base::ExtCtxt; use crate::base::ExtCtxt;
use crate::mbe::macro_parser::{MatchedNonterminal, MatchedSeq, NamedMatch}; use crate::mbe::macro_parser::{MatchedNtNonTt, MatchedNtTt, MatchedSeq, NamedMatch};
use crate::mbe::{self, MetaVarExpr}; use crate::mbe::{self, MetaVarExpr};
use rustc_ast::mut_visit::{self, MutVisitor}; use rustc_ast::mut_visit::{self, MutVisitor};
use rustc_ast::token::{self, NtTT, Token, TokenKind}; use rustc_ast::token::{self, Nonterminal, Token, TokenKind};
use rustc_ast::tokenstream::{DelimSpan, TokenStream, TokenTree, TreeAndSpacing}; use rustc_ast::tokenstream::{DelimSpan, TokenStream, TokenTree, TreeAndSpacing};
use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::sync::Lrc; use rustc_data_structures::sync::Lrc;
@ -233,25 +233,29 @@ pub(super) fn transcribe<'a>(
// the meta-var. // the meta-var.
let ident = MacroRulesNormalizedIdent::new(orignal_ident); let ident = MacroRulesNormalizedIdent::new(orignal_ident);
if let Some(cur_matched) = lookup_cur_matched(ident, interp, &repeats) { if let Some(cur_matched) = lookup_cur_matched(ident, interp, &repeats) {
if let MatchedNonterminal(nt) = cur_matched { match cur_matched {
let token = if let NtTT(tt) = &**nt { MatchedNtTt(ref tt) => {
// `tt`s are emitted into the output stream directly as "raw tokens", // `tt`s are emitted into the output stream directly as "raw tokens",
// without wrapping them into groups. // without wrapping them into groups.
tt.clone() let token = tt.clone();
} else { result.push(token.into());
}
MatchedNtNonTt(ref nt) => {
// Other variables are emitted into the output stream as groups with // Other variables are emitted into the output stream as groups with
// `Delimiter::None` to maintain parsing priorities. // `Delimiter::None` to maintain parsing priorities.
// `Interpolated` is currently used for such groups in rustc parser. // `Interpolated` is currently used for such groups in rustc parser.
debug_assert!(!matches!(**nt, Nonterminal::NtTT(_)));
marker.visit_span(&mut sp); marker.visit_span(&mut sp);
TokenTree::token(token::Interpolated(nt.clone()), sp) let token = TokenTree::token(token::Interpolated(nt.clone()), sp);
}; result.push(token.into());
result.push(token.into()); }
} else { MatchedSeq(..) => {
// We were unable to descend far enough. This is an error. // We were unable to descend far enough. This is an error.
return Err(cx.struct_span_err( return Err(cx.struct_span_err(
sp, /* blame the macro writer */ sp, /* blame the macro writer */
&format!("variable '{}' is still repeating at this depth", ident), &format!("variable '{}' is still repeating at this depth", ident),
)); ));
}
} }
} else { } else {
// If we aren't able to match the meta-var, we push it back into the result but // If we aren't able to match the meta-var, we push it back into the result but
@ -308,7 +312,7 @@ fn lookup_cur_matched<'a>(
let mut matched = matched; let mut matched = matched;
for &(idx, _) in repeats { for &(idx, _) in repeats {
match matched { match matched {
MatchedNonterminal(_) => break, MatchedNtTt(_) | MatchedNtNonTt(_) => break,
MatchedSeq(ref ads) => matched = ads.get(idx).unwrap(), MatchedSeq(ref ads) => matched = ads.get(idx).unwrap(),
} }
} }
@ -398,7 +402,7 @@ fn lockstep_iter_size(
let name = MacroRulesNormalizedIdent::new(name); let name = MacroRulesNormalizedIdent::new(name);
match lookup_cur_matched(name, interpolations, repeats) { match lookup_cur_matched(name, interpolations, repeats) {
Some(matched) => match matched { Some(matched) => match matched {
MatchedNonterminal(_) => LockstepIterSize::Unconstrained, MatchedNtTt(_) | MatchedNtNonTt(_) => LockstepIterSize::Unconstrained,
MatchedSeq(ref ads) => LockstepIterSize::Constraint(ads.len(), name), MatchedSeq(ref ads) => LockstepIterSize::Constraint(ads.len(), name),
}, },
_ => LockstepIterSize::Unconstrained, _ => LockstepIterSize::Unconstrained,
@ -445,7 +449,7 @@ fn count_repetitions<'a>(
sp: &DelimSpan, sp: &DelimSpan,
) -> PResult<'a, usize> { ) -> PResult<'a, usize> {
match matched { match matched {
MatchedNonterminal(_) => { MatchedNtTt(_) | MatchedNtNonTt(_) => {
if declared_lhs_depth == 0 { if declared_lhs_depth == 0 {
return Err(cx.struct_span_err( return Err(cx.struct_span_err(
sp.entire(), sp.entire(),