Rollup merge of #67250 - nnethercote:rm-DelimSpan-from-NamedMatch-MatchedSeq, r=Centril

Remove the `DelimSpan` from `NamedMatch::MatchedSeq`.

Because it's unused. This then allows the removal of
`MatcherPos::sp_open`. It's a tiny perf win, reducing instruction counts
by 0.1% - 0.2% on a few benchmarks.

r? @Centril
This commit is contained in:
Mazdak Farrokhzad 2019-12-13 04:21:31 +01:00 committed by GitHub
commit ac0bd42a4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 20 deletions

View File

@ -83,7 +83,7 @@ use syntax::print::pprust;
use syntax::sess::ParseSess; use syntax::sess::ParseSess;
use syntax::symbol::{kw, sym, Symbol}; use syntax::symbol::{kw, sym, Symbol};
use syntax::token::{self, DocComment, Nonterminal, Token}; use syntax::token::{self, DocComment, Nonterminal, Token};
use syntax::tokenstream::{DelimSpan, TokenStream}; use syntax::tokenstream::TokenStream;
use errors::{PResult, FatalError}; use errors::{PResult, FatalError};
use smallvec::{smallvec, SmallVec}; use smallvec::{smallvec, SmallVec};
@ -164,11 +164,6 @@ struct MatcherPos<'root, 'tt> {
/// The position of the "dot" in this matcher /// The position of the "dot" in this matcher
idx: usize, idx: usize,
/// The first span of source that the beginning of this matcher corresponds to. In other
/// words, the token in the source whose span is `sp_open` is matched against the first token of
/// the matcher.
sp_open: Span,
/// For each named metavar in the matcher, we keep track of token trees matched against the /// For each named metavar in the matcher, we keep track of token trees matched against the
/// metavar by the black box parser. In particular, there may be more than one match per /// metavar by the black box parser. In particular, there may be more than one match per
/// metavar if we are in a repetition (each repetition matches each of the variables). /// metavar if we are in a repetition (each repetition matches each of the variables).
@ -307,8 +302,8 @@ fn create_matches(len: usize) -> Box<[Lrc<NamedMatchVec>]> {
} }
/// Generates the top-level matcher position in which the "dot" is before the first token of the /// Generates the top-level matcher position in which the "dot" is before the first token of the
/// matcher `ms` and we are going to start matching at the span `open` in the source. /// matcher `ms`.
fn initial_matcher_pos<'root, 'tt>(ms: &'tt [TokenTree], open: Span) -> MatcherPos<'root, 'tt> { fn initial_matcher_pos<'root, 'tt>(ms: &'tt [TokenTree]) -> MatcherPos<'root, 'tt> {
let match_idx_hi = count_names(ms); let match_idx_hi = count_names(ms);
let matches = create_matches(match_idx_hi); let matches = create_matches(match_idx_hi);
MatcherPos { MatcherPos {
@ -316,8 +311,6 @@ fn initial_matcher_pos<'root, 'tt>(ms: &'tt [TokenTree], open: Span) -> MatcherP
top_elts: TtSeq(ms), // "elts" is an abbr. for "elements" top_elts: TtSeq(ms), // "elts" is an abbr. for "elements"
// The "dot" is before the first token of the matcher // The "dot" is before the first token of the matcher
idx: 0, idx: 0,
// We start matching at the span `open` in the source code
sp_open: open,
// Initialize `matches` to a bunch of empty `Vec`s -- one for each metavar in `top_elts`. // Initialize `matches` to a bunch of empty `Vec`s -- one for each metavar in `top_elts`.
// `match_lo` for `top_elts` is 0 and `match_hi` is `matches.len()`. `match_cur` is 0 since // `match_lo` for `top_elts` is 0 and `match_hi` is `matches.len()`. `match_cur` is 0 since
@ -355,7 +348,7 @@ fn initial_matcher_pos<'root, 'tt>(ms: &'tt [TokenTree], open: Span) -> MatcherP
/// token tree it was derived from. /// token tree it was derived from.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
crate enum NamedMatch { crate enum NamedMatch {
MatchedSeq(Lrc<NamedMatchVec>, DelimSpan), MatchedSeq(Lrc<NamedMatchVec>),
MatchedNonterminal(Lrc<Nonterminal>), MatchedNonterminal(Lrc<Nonterminal>),
} }
@ -497,8 +490,7 @@ fn inner_parse_loop<'root, 'tt>(
// Add matches from this repetition to the `matches` of `up` // Add matches from this repetition to the `matches` of `up`
for idx in item.match_lo..item.match_hi { for idx in item.match_lo..item.match_hi {
let sub = item.matches[idx].clone(); let sub = item.matches[idx].clone();
let span = DelimSpan::from_pair(item.sp_open, token.span); new_pos.push_match(idx, MatchedSeq(sub));
new_pos.push_match(idx, MatchedSeq(sub, span));
} }
// Move the "dot" past the repetition in `up` // Move the "dot" past the repetition in `up`
@ -552,7 +544,7 @@ fn inner_parse_loop<'root, 'tt>(
new_item.match_cur += seq.num_captures; new_item.match_cur += seq.num_captures;
new_item.idx += 1; new_item.idx += 1;
for idx in item.match_cur..item.match_cur + seq.num_captures { for idx in item.match_cur..item.match_cur + seq.num_captures {
new_item.push_match(idx, MatchedSeq(Lrc::new(smallvec![]), sp)); new_item.push_match(idx, MatchedSeq(Lrc::new(smallvec![])));
} }
cur_items.push(new_item); cur_items.push(new_item);
} }
@ -568,7 +560,6 @@ fn inner_parse_loop<'root, 'tt>(
match_cur: item.match_cur, match_cur: item.match_cur,
match_hi: item.match_cur + seq.num_captures, match_hi: item.match_cur + seq.num_captures,
up: Some(item), up: Some(item),
sp_open: sp.open,
top_elts: Tt(TokenTree::Sequence(sp, seq)), top_elts: Tt(TokenTree::Sequence(sp, seq)),
}))); })));
} }
@ -663,7 +654,7 @@ pub(super) fn parse(
// //
// This MatcherPos instance is allocated on the stack. All others -- and // This MatcherPos instance is allocated on the stack. All others -- and
// there are frequently *no* others! -- are allocated on the heap. // there are frequently *no* others! -- are allocated on the heap.
let mut initial = initial_matcher_pos(ms, parser.token.span); let mut initial = initial_matcher_pos(ms);
let mut cur_items = smallvec![MatcherPosHandle::Ref(&mut initial)]; let mut cur_items = smallvec![MatcherPosHandle::Ref(&mut initial)];
let mut next_items = Vec::new(); let mut next_items = Vec::new();

View File

@ -379,7 +379,7 @@ pub fn compile_declarative_macro(
// Extract the arguments: // Extract the arguments:
let lhses = match argument_map[&lhs_nm] { let lhses = match argument_map[&lhs_nm] {
MatchedSeq(ref s, _) => s MatchedSeq(ref s) => s
.iter() .iter()
.map(|m| { .map(|m| {
if let MatchedNonterminal(ref nt) = *m { if let MatchedNonterminal(ref nt) = *m {
@ -402,7 +402,7 @@ pub fn compile_declarative_macro(
}; };
let rhses = match argument_map[&rhs_nm] { let rhses = match argument_map[&rhs_nm] {
MatchedSeq(ref s, _) => s MatchedSeq(ref s) => s
.iter() .iter()
.map(|m| { .map(|m| {
if let MatchedNonterminal(ref nt) = *m { if let MatchedNonterminal(ref nt) = *m {

View File

@ -299,7 +299,7 @@ fn lookup_cur_matched<'a>(
for &(idx, _) in repeats { for &(idx, _) in repeats {
match matched { match matched {
MatchedNonterminal(_) => break, MatchedNonterminal(_) => break,
MatchedSeq(ref ads, _) => matched = ads.get(idx).unwrap(), MatchedSeq(ref ads) => matched = ads.get(idx).unwrap(),
} }
} }
@ -382,7 +382,7 @@ fn lockstep_iter_size(
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, MatchedNonterminal(_) => LockstepIterSize::Unconstrained,
MatchedSeq(ref ads, _) => LockstepIterSize::Constraint(ads.len(), name), MatchedSeq(ref ads) => LockstepIterSize::Constraint(ads.len(), name),
}, },
_ => LockstepIterSize::Unconstrained, _ => LockstepIterSize::Unconstrained,
} }