Skip to content

Commit 6872377

Browse files
committed
Change TokenTreeOrTokenTreeVec to TokenTreeOrTokenTreeSlice.
This avoids a `to_owned` call that can be hot, speeding up the various runs of html5ever by 1--5%, and some runs of crates.io by 2--3%.
1 parent 6fc409e commit 6872377

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

src/libsyntax/ext/tt/macro_parser.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
8383
pub use self::NamedMatch::*;
8484
pub use self::ParseResult::*;
85-
use self::TokenTreeOrTokenTreeVec::*;
85+
use self::TokenTreeOrTokenTreeSlice::*;
8686

8787
use ast::Ident;
8888
use syntax_pos::{self, BytePos, Span};
@@ -106,12 +106,12 @@ use std::collections::hash_map::Entry::{Occupied, Vacant};
106106
/// Either a sequence of token trees or a single one. This is used as the representation of the
107107
/// sequence of tokens that make up a matcher.
108108
#[derive(Clone)]
109-
enum TokenTreeOrTokenTreeVec {
109+
enum TokenTreeOrTokenTreeSlice<'a> {
110110
Tt(TokenTree),
111-
TtSeq(Vec<TokenTree>),
111+
TtSeq(&'a [TokenTree]),
112112
}
113113

114-
impl TokenTreeOrTokenTreeVec {
114+
impl<'a> TokenTreeOrTokenTreeSlice<'a> {
115115
/// Returns the number of constituent top-level token trees of `self` (top-level in that it
116116
/// will not recursively descend into subtrees).
117117
fn len(&self) -> usize {
@@ -135,19 +135,19 @@ impl TokenTreeOrTokenTreeVec {
135135
/// This is used by `inner_parse_loop` to keep track of delimited submatchers that we have
136136
/// descended into.
137137
#[derive(Clone)]
138-
struct MatcherTtFrame {
138+
struct MatcherTtFrame<'a> {
139139
/// The "parent" matcher that we are descending into.
140-
elts: TokenTreeOrTokenTreeVec,
140+
elts: TokenTreeOrTokenTreeSlice<'a>,
141141
/// The position of the "dot" in `elts` at the time we descended.
142142
idx: usize,
143143
}
144144

145145
/// Represents a single "position" (aka "matcher position", aka "item"), as described in the module
146146
/// documentation.
147147
#[derive(Clone)]
148-
struct MatcherPos {
148+
struct MatcherPos<'a> {
149149
/// The token or sequence of tokens that make up the matcher
150-
top_elts: TokenTreeOrTokenTreeVec,
150+
top_elts: TokenTreeOrTokenTreeSlice<'a>,
151151
/// The position of the "dot" in this matcher
152152
idx: usize,
153153
/// The beginning position in the source that the beginning of this matcher corresponds to. In
@@ -186,7 +186,7 @@ struct MatcherPos {
186186
sep: Option<Token>,
187187
/// The "parent" matcher position if we are in a repetition. That is, the matcher position just
188188
/// before we enter the sequence.
189-
up: Option<Box<MatcherPos>>,
189+
up: Option<Box<MatcherPos<'a>>>,
190190

191191
// Specifically used to "unzip" token trees. By "unzip", we mean to unwrap the delimiters from
192192
// a delimited token tree (e.g. something wrapped in `(` `)`) or to get the contents of a doc
@@ -195,10 +195,10 @@ struct MatcherPos {
195195
/// pat ) pat`), we need to keep track of the matchers we are descending into. This stack does
196196
/// that where the bottom of the stack is the outermost matcher.
197197
// Also, throughout the comments, this "descent" is often referred to as "unzipping"...
198-
stack: Vec<MatcherTtFrame>,
198+
stack: Vec<MatcherTtFrame<'a>>,
199199
}
200200

201-
impl MatcherPos {
201+
impl<'a> MatcherPos<'a> {
202202
/// Add `m` as a named match for the `idx`-th metavar.
203203
fn push_match(&mut self, idx: usize, m: NamedMatch) {
204204
let matches = Rc::make_mut(&mut self.matches[idx]);
@@ -241,8 +241,8 @@ fn create_matches(len: usize) -> Vec<Rc<Vec<NamedMatch>>> {
241241

242242
/// Generate the top-level matcher position in which the "dot" is before the first token of the
243243
/// matcher `ms` and we are going to start matching at position `lo` in the source.
244-
fn initial_matcher_pos(ms: Vec<TokenTree>, lo: BytePos) -> Box<MatcherPos> {
245-
let match_idx_hi = count_names(&ms[..]);
244+
fn initial_matcher_pos(ms: &[TokenTree], lo: BytePos) -> Box<MatcherPos> {
245+
let match_idx_hi = count_names(ms);
246246
let matches = create_matches(match_idx_hi);
247247
Box::new(MatcherPos {
248248
// Start with the top level matcher given to us
@@ -394,12 +394,12 @@ fn token_name_eq(t1: &Token, t2: &Token) -> bool {
394394
/// # Returns
395395
///
396396
/// A `ParseResult`. Note that matches are kept track of through the items generated.
397-
fn inner_parse_loop(
397+
fn inner_parse_loop<'a>(
398398
sess: &ParseSess,
399-
cur_items: &mut SmallVector<Box<MatcherPos>>,
400-
next_items: &mut Vec<Box<MatcherPos>>,
401-
eof_items: &mut SmallVector<Box<MatcherPos>>,
402-
bb_items: &mut SmallVector<Box<MatcherPos>>,
399+
cur_items: &mut SmallVector<Box<MatcherPos<'a>>>,
400+
next_items: &mut Vec<Box<MatcherPos<'a>>>,
401+
eof_items: &mut SmallVector<Box<MatcherPos<'a>>>,
402+
bb_items: &mut SmallVector<Box<MatcherPos<'a>>>,
403403
token: &Token,
404404
span: syntax_pos::Span,
405405
) -> ParseResult<()> {
@@ -596,7 +596,7 @@ pub fn parse(
596596
// processes all of these possible matcher positions and produces posible next positions into
597597
// `next_items`. After some post-processing, the contents of `next_items` replenish `cur_items`
598598
// and we start over again.
599-
let mut cur_items = SmallVector::one(initial_matcher_pos(ms.to_owned(), parser.span.lo()));
599+
let mut cur_items = SmallVector::one(initial_matcher_pos(ms, parser.span.lo()));
600600
let mut next_items = Vec::new();
601601

602602
loop {

0 commit comments

Comments
 (0)