Skip to content

Commit 15958ca

Browse files
pietroalbinikennytm
authored andcommitted
Rollup merge of rust-lang#55558 - nnethercote:tweak-MatcherPos-matches, r=petrochenkov
Tweak `MatcherPos::matches` These changes reduce instruction counts on `sentry-cli-check` incremental builds by up to 2%.
2 parents 133ba2d + 0d2abe4 commit 15958ca

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/libsyntax/ext/tt/macro_parser.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ struct MatcherTtFrame<'a> {
143143
idx: usize,
144144
}
145145

146+
type NamedMatchVec = SmallVec<[NamedMatch; 4]>;
147+
146148
/// Represents a single "position" (aka "matcher position", aka "item"), as described in the module
147149
/// documentation.
148150
#[derive(Clone)]
@@ -168,7 +170,7 @@ struct MatcherPos<'a> {
168170
/// all bound matches from the submatcher into the shared top-level `matches` vector. If `sep`
169171
/// and `up` are `Some`, then `matches` is _not_ the shared top-level list. Instead, if one
170172
/// wants the shared `matches`, one should use `up.matches`.
171-
matches: Vec<Rc<Vec<NamedMatch>>>,
173+
matches: Box<[Rc<NamedMatchVec>]>,
172174
/// The position in `matches` corresponding to the first metavar in this matcher's sequence of
173175
/// token trees. In other words, the first metavar in the first token of `top_elts` corresponds
174176
/// to `matches[match_lo]`.
@@ -278,9 +280,14 @@ pub fn count_names(ms: &[TokenTree]) -> usize {
278280
})
279281
}
280282

281-
/// Initialize `len` empty shared `Vec`s to be used to store matches of metavars.
282-
fn create_matches(len: usize) -> Vec<Rc<Vec<NamedMatch>>> {
283-
(0..len).into_iter().map(|_| Rc::new(Vec::new())).collect()
283+
/// `len` `Vec`s (initially shared and empty) that will store matches of metavars.
284+
fn create_matches(len: usize) -> Box<[Rc<NamedMatchVec>]> {
285+
if len == 0 {
286+
vec![]
287+
} else {
288+
let empty_matches = Rc::new(SmallVec::new());
289+
vec![empty_matches.clone(); len]
290+
}.into_boxed_slice()
284291
}
285292

286293
/// Generate the top-level matcher position in which the "dot" is before the first token of the
@@ -332,7 +339,7 @@ fn initial_matcher_pos(ms: &[TokenTree], open: Span) -> MatcherPos {
332339
/// token tree it was derived from.
333340
#[derive(Debug, Clone)]
334341
pub enum NamedMatch {
335-
MatchedSeq(Rc<Vec<NamedMatch>>, DelimSpan),
342+
MatchedSeq(Rc<NamedMatchVec>, DelimSpan),
336343
MatchedNonterminal(Rc<Nonterminal>),
337344
}
338345

@@ -540,7 +547,7 @@ fn inner_parse_loop<'a>(
540547
new_item.match_cur += seq.num_captures;
541548
new_item.idx += 1;
542549
for idx in item.match_cur..item.match_cur + seq.num_captures {
543-
new_item.push_match(idx, MatchedSeq(Rc::new(vec![]), sp));
550+
new_item.push_match(idx, MatchedSeq(Rc::new(smallvec![]), sp));
544551
}
545552
cur_items.push(new_item);
546553
}

0 commit comments

Comments
 (0)