Skip to content

Commit

Permalink
Rollup merge of rust-lang#67250 - nnethercote:rm-DelimSpan-from-Named…
Browse files Browse the repository at this point in the history
…Match-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
  • Loading branch information
Centril authored Dec 12, 2019
2 parents 7bfd45a + ffd2142 commit b533592
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 20 deletions.
23 changes: 7 additions & 16 deletions src/libsyntax_expand/mbe/macro_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ use syntax::print::pprust;
use syntax::sess::ParseSess;
use syntax::symbol::{kw, sym, Symbol};
use syntax::token::{self, DocComment, Nonterminal, Token};
use syntax::tokenstream::{DelimSpan, TokenStream};
use syntax::tokenstream::TokenStream;

use errors::{PResult, FatalError};
use smallvec::{smallvec, SmallVec};
Expand Down Expand Up @@ -164,11 +164,6 @@ struct MatcherPos<'root, 'tt> {
/// The position of the "dot" in this matcher
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
/// 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).
Expand Down Expand Up @@ -307,17 +302,15 @@ 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
/// matcher `ms` and we are going to start matching at the span `open` in the source.
fn initial_matcher_pos<'root, 'tt>(ms: &'tt [TokenTree], open: Span) -> MatcherPos<'root, 'tt> {
/// matcher `ms`.
fn initial_matcher_pos<'root, 'tt>(ms: &'tt [TokenTree]) -> MatcherPos<'root, 'tt> {
let match_idx_hi = count_names(ms);
let matches = create_matches(match_idx_hi);
MatcherPos {
// Start with the top level matcher given to us
top_elts: TtSeq(ms), // "elts" is an abbr. for "elements"
// The "dot" is before the first token of the matcher
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`.
// `match_lo` for `top_elts` is 0 and `match_hi` is `matches.len()`. `match_cur` is 0 since
Expand Down Expand Up @@ -355,7 +348,7 @@ fn initial_matcher_pos<'root, 'tt>(ms: &'tt [TokenTree], open: Span) -> MatcherP
/// token tree it was derived from.
#[derive(Debug, Clone)]
crate enum NamedMatch {
MatchedSeq(Lrc<NamedMatchVec>, DelimSpan),
MatchedSeq(Lrc<NamedMatchVec>),
MatchedNonterminal(Lrc<Nonterminal>),
}

Expand Down Expand Up @@ -497,8 +490,7 @@ fn inner_parse_loop<'root, 'tt>(
// Add matches from this repetition to the `matches` of `up`
for idx in item.match_lo..item.match_hi {
let sub = item.matches[idx].clone();
let span = DelimSpan::from_pair(item.sp_open, token.span);
new_pos.push_match(idx, MatchedSeq(sub, span));
new_pos.push_match(idx, MatchedSeq(sub));
}

// Move the "dot" past the repetition in `up`
Expand Down Expand Up @@ -552,7 +544,7 @@ fn inner_parse_loop<'root, 'tt>(
new_item.match_cur += seq.num_captures;
new_item.idx += 1;
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);
}
Expand All @@ -568,7 +560,6 @@ fn inner_parse_loop<'root, 'tt>(
match_cur: item.match_cur,
match_hi: item.match_cur + seq.num_captures,
up: Some(item),
sp_open: sp.open,
top_elts: Tt(TokenTree::Sequence(sp, seq)),
})));
}
Expand Down Expand Up @@ -663,7 +654,7 @@ pub(super) fn parse(
//
// This MatcherPos instance is allocated on the stack. All others -- and
// 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 next_items = Vec::new();

Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax_expand/mbe/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ pub fn compile_declarative_macro(

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

let rhses = match argument_map[&rhs_nm] {
MatchedSeq(ref s, _) => s
MatchedSeq(ref s) => s
.iter()
.map(|m| {
if let MatchedNonterminal(ref nt) = *m {
Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax_expand/mbe/transcribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ fn lookup_cur_matched<'a>(
for &(idx, _) in repeats {
match matched {
MatchedNonterminal(_) => break,
MatchedSeq(ref ads, _) => matched = ads.get(idx).unwrap(),
MatchedSeq(ref ads) => matched = ads.get(idx).unwrap(),
}
}

Expand Down Expand Up @@ -382,7 +382,7 @@ fn lockstep_iter_size(
match lookup_cur_matched(name, interpolations, repeats) {
Some(matched) => match matched {
MatchedNonterminal(_) => LockstepIterSize::Unconstrained,
MatchedSeq(ref ads, _) => LockstepIterSize::Constraint(ads.len(), name),
MatchedSeq(ref ads) => LockstepIterSize::Constraint(ads.len(), name),
},
_ => LockstepIterSize::Unconstrained,
}
Expand Down

0 comments on commit b533592

Please sign in to comment.