Skip to content

Commit 1a70064

Browse files
committed
Avoid use of Lrc in mbe::Frame.
This is a nice performance win on some crates.
1 parent 769e2ed commit 1a70064

File tree

1 file changed

+38
-29
lines changed

1 file changed

+38
-29
lines changed

compiler/rustc_expand/src/mbe/transcribe.rs

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use rustc_ast::mut_visit::{self, MutVisitor};
55
use rustc_ast::token::{self, Token, TokenKind};
66
use rustc_ast::tokenstream::{DelimSpan, TokenStream, TokenTree, TreeAndSpacing};
77
use rustc_data_structures::fx::FxHashMap;
8-
use rustc_data_structures::sync::Lrc;
98
use rustc_errors::{pluralize, PResult};
109
use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed};
1110
use rustc_span::hygiene::{LocalExpnId, Transparency};
@@ -27,31 +26,35 @@ impl MutVisitor for Marker {
2726
}
2827

2928
/// An iterator over the token trees in a delimited token tree (`{ ... }`) or a sequence (`$(...)`).
30-
enum Frame {
31-
Delimited { forest: Lrc<mbe::Delimited>, idx: usize, span: DelimSpan },
32-
Sequence { forest: Lrc<mbe::SequenceRepetition>, idx: usize, sep: Option<Token> },
29+
enum Frame<'a> {
30+
Delimited {
31+
tts: &'a [mbe::TokenTree],
32+
delim_token: token::DelimToken,
33+
idx: usize,
34+
span: DelimSpan,
35+
},
36+
Sequence {
37+
tts: &'a [mbe::TokenTree],
38+
idx: usize,
39+
sep: Option<Token>,
40+
},
3341
}
3442

35-
impl Frame {
43+
impl<'a> Frame<'a> {
3644
/// Construct a new frame around the delimited set of tokens.
37-
fn new(tts: Vec<mbe::TokenTree>) -> Frame {
38-
let forest = Lrc::new(mbe::Delimited { delim: token::NoDelim, tts });
39-
Frame::Delimited { forest, idx: 0, span: DelimSpan::dummy() }
45+
fn new(tts: &'a [mbe::TokenTree]) -> Frame<'a> {
46+
Frame::Delimited { tts, delim_token: token::NoDelim, idx: 0, span: DelimSpan::dummy() }
4047
}
4148
}
4249

43-
impl Iterator for Frame {
44-
type Item = mbe::TokenTree;
50+
impl<'a> Iterator for Frame<'a> {
51+
type Item = &'a mbe::TokenTree;
4552

46-
fn next(&mut self) -> Option<mbe::TokenTree> {
47-
match *self {
48-
Frame::Delimited { ref forest, ref mut idx, .. } => {
49-
let res = forest.tts.get(*idx).cloned();
50-
*idx += 1;
51-
res
52-
}
53-
Frame::Sequence { ref forest, ref mut idx, .. } => {
54-
let res = forest.tts.get(*idx).cloned();
53+
fn next(&mut self) -> Option<&'a mbe::TokenTree> {
54+
match self {
55+
Frame::Delimited { tts, ref mut idx, .. }
56+
| Frame::Sequence { tts, ref mut idx, .. } => {
57+
let res = tts.get(*idx);
5558
*idx += 1;
5659
res
5760
}
@@ -92,7 +95,7 @@ pub(super) fn transcribe<'a>(
9295

9396
// We descend into the RHS (`src`), expanding things as we go. This stack contains the things
9497
// we have yet to expand/are still expanding. We start the stack off with the whole RHS.
95-
let mut stack: SmallVec<[Frame; 1]> = smallvec![Frame::new(src)];
98+
let mut stack: SmallVec<[Frame<'_>; 1]> = smallvec![Frame::new(&src)];
9699

97100
// As we descend in the RHS, we will need to be able to match nested sequences of matchers.
98101
// `repeats` keeps track of where we are in matching at each level, with the last element being
@@ -146,14 +149,14 @@ pub(super) fn transcribe<'a>(
146149
// We are done processing a Delimited. If this is the top-level delimited, we are
147150
// done. Otherwise, we unwind the result_stack to append what we have produced to
148151
// any previous results.
149-
Frame::Delimited { forest, span, .. } => {
152+
Frame::Delimited { delim_token, span, .. } => {
150153
if result_stack.is_empty() {
151154
// No results left to compute! We are back at the top-level.
152155
return Ok(TokenStream::new(result));
153156
}
154157

155158
// Step back into the parent Delimited.
156-
let tree = TokenTree::Delimited(span, forest.delim, TokenStream::new(result));
159+
let tree = TokenTree::Delimited(span, delim_token, TokenStream::new(result));
157160
result = result_stack.pop().unwrap();
158161
result.push(tree.into());
159162
}
@@ -167,7 +170,7 @@ pub(super) fn transcribe<'a>(
167170
// We are descending into a sequence. We first make sure that the matchers in the RHS
168171
// and the matches in `interp` have the same shape. Otherwise, either the caller or the
169172
// macro writer has made a mistake.
170-
seq @ mbe::TokenTree::Sequence(..) => {
173+
seq @ mbe::TokenTree::Sequence(_, delimited) => {
171174
match lockstep_iter_size(&seq, interp, &repeats) {
172175
LockstepIterSize::Unconstrained => {
173176
return Err(cx.struct_span_err(
@@ -214,7 +217,7 @@ pub(super) fn transcribe<'a>(
214217
stack.push(Frame::Sequence {
215218
idx: 0,
216219
sep: seq.separator.clone(),
217-
forest: seq,
220+
tts: &delimited.tts,
218221
});
219222
}
220223
}
@@ -272,15 +275,21 @@ pub(super) fn transcribe<'a>(
272275
// the previous results (from outside the Delimited).
273276
mbe::TokenTree::Delimited(mut span, delimited) => {
274277
mut_visit::visit_delim_span(&mut span, &mut marker);
275-
stack.push(Frame::Delimited { forest: delimited, idx: 0, span });
278+
stack.push(Frame::Delimited {
279+
tts: &delimited.tts,
280+
delim_token: delimited.delim,
281+
idx: 0,
282+
span,
283+
});
276284
result_stack.push(mem::take(&mut result));
277285
}
278286

279287
// Nothing much to do here. Just push the token to the result, being careful to
280288
// preserve syntax context.
281289
mbe::TokenTree::Token(token) => {
282-
let mut tt = TokenTree::Token(token);
283-
mut_visit::visit_tt(&mut tt, &mut marker);
290+
let mut token = token.clone();
291+
mut_visit::visit_token(&mut token, &mut marker);
292+
let tt = TokenTree::Token(token);
284293
result.push(tt.into());
285294
}
286295

@@ -516,7 +525,7 @@ fn out_of_bounds_err<'a>(
516525

517526
fn transcribe_metavar_expr<'a>(
518527
cx: &ExtCtxt<'a>,
519-
expr: MetaVarExpr,
528+
expr: &MetaVarExpr,
520529
interp: &FxHashMap<MacroRulesNormalizedIdent, NamedMatch>,
521530
marker: &mut Marker,
522531
repeats: &[(usize, usize)],
@@ -528,7 +537,7 @@ fn transcribe_metavar_expr<'a>(
528537
marker.visit_span(&mut span);
529538
span
530539
};
531-
match expr {
540+
match *expr {
532541
MetaVarExpr::Count(original_ident, depth_opt) => {
533542
let matched = matched_from_ident(cx, original_ident, interp)?;
534543
let count = count_repetitions(cx, depth_opt, matched, &repeats, sp)?;

0 commit comments

Comments
 (0)