Skip to content

Commit 725536d

Browse files
Auto merge of #142815 - tgross35:transcribe-perf, r=<try>
mbe: Inline functions in `transcribe` that are only called once [#142713] did some refactoring of macro expansion, which seems to have had an effect on performance. The only change was to split large functions into smaller ones, so mark some functions that are only called once `#[inline]` to make codegen more similar. [#142713]: https://www.github.com/rust-lang/rust/pull/142713
2 parents 36b2163 + 0bd2f7a commit 725536d

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

compiler/rustc_expand/src/mbe/transcribe.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,6 @@ use crate::mbe::{self, KleeneOp, MetaVarExpr};
2727

2828
/// Context needed to perform transcription of metavariable expressions.
2929
struct TranscrCtx<'psess, 'itp> {
30-
psess: &'psess ParseSess,
31-
32-
/// Map from metavars to matched tokens
33-
interp: &'itp FxHashMap<MacroRulesNormalizedIdent, NamedMatch>,
34-
35-
/// Allow marking spans.
36-
marker: Marker,
37-
3830
/// The stack of things yet to be completely expanded.
3931
///
4032
/// We descend into the RHS (`src`), expanding things as we go. This stack contains the things
@@ -66,6 +58,14 @@ struct TranscrCtx<'psess, 'itp> {
6658
/// The in-progress `result` lives at the top of this stack. Each entered `TokenTree` adds a
6759
/// new entry.
6860
result_stack: Vec<Vec<TokenTree>>,
61+
62+
/// Allow marking spans.
63+
marker: Marker,
64+
65+
psess: &'psess ParseSess,
66+
67+
/// Map from metavars to matched tokens
68+
interp: &'itp FxHashMap<MacroRulesNormalizedIdent, NamedMatch>,
6969
}
7070

7171
impl<'psess> TranscrCtx<'psess, '_> {
@@ -174,17 +174,17 @@ pub(super) fn transcribe<'a>(
174174
}
175175

176176
let mut tscx = TranscrCtx {
177-
psess,
178-
interp,
179-
marker: Marker { expand_id, transparency, cache: Default::default() },
180-
repeats: Vec::new(),
181177
stack: smallvec![Frame::new_delimited(
182178
src,
183179
src_span,
184180
DelimSpacing::new(Spacing::Alone, Spacing::Alone)
185181
)],
182+
repeats: Vec::new(),
186183
result: Vec::new(),
187184
result_stack: Vec::new(),
185+
marker: Marker { expand_id, transparency, cache: Default::default() },
186+
psess,
187+
interp,
188188
};
189189

190190
loop {
@@ -289,6 +289,7 @@ pub(super) fn transcribe<'a>(
289289
}
290290

291291
/// Turn `$(...)*` sequences into tokens.
292+
#[inline(always)] // called once
292293
fn transcribe_sequence<'tx, 'itp>(
293294
tscx: &mut TranscrCtx<'tx, 'itp>,
294295
seq: &mbe::TokenTree,
@@ -357,6 +358,7 @@ fn transcribe_sequence<'tx, 'itp>(
357358
/// producing "xyz", which is bad because it effectively merges tokens.
358359
/// `Spacing::Alone` is the safer option. Fortunately, `space_between` will avoid
359360
/// some of the unnecessary whitespace.
361+
#[inline(always)] // called once
360362
fn transcribe_metavar<'tx>(
361363
tscx: &mut TranscrCtx<'tx, '_>,
362364
mut sp: Span,
@@ -492,6 +494,7 @@ fn transcribe_metavar<'tx>(
492494
}
493495

494496
/// Turn `${expr(...)}` metavariable expressionss into tokens.
497+
#[inline(always)] // called once
495498
fn transcribe_metavar_expr<'tx>(
496499
tscx: &mut TranscrCtx<'tx, '_>,
497500
dspan: DelimSpan,
@@ -502,7 +505,7 @@ fn transcribe_metavar_expr<'tx>(
502505
MetaVarExpr::Concat(ref elements) => metavar_expr_concat(tscx, dspan, elements)?,
503506
MetaVarExpr::Count(original_ident, depth) => {
504507
let matched = matched_from_ident(dcx, original_ident, tscx.interp)?;
505-
let count = count_repetitions(dcx, depth, matched, &tscx.repeats, &dspan)?;
508+
let count = count_repetitions(dcx, depth, matched, &tscx.repeats, dspan)?;
506509
TokenTree::token_alone(
507510
TokenKind::lit(token::Integer, sym::integer(count), None),
508511
tscx.visited_dspan(dspan),
@@ -537,6 +540,7 @@ fn transcribe_metavar_expr<'tx>(
537540
}
538541

539542
/// Handle the `${concat(...)}` metavariable expression.
543+
#[inline(always)] // called once
540544
fn metavar_expr_concat<'tx>(
541545
tscx: &mut TranscrCtx<'tx, '_>,
542546
dspan: DelimSpan,
@@ -617,6 +621,7 @@ fn metavar_expr_concat<'tx>(
617621
/// These are typically used for passing larger amounts of code, and tokens in that code usually
618622
/// combine with each other and not with tokens outside of the sequence.
619623
/// - The metavariable span comes from a different crate, then we prefer the more local span.
624+
#[inline(always)] // called once
620625
fn maybe_use_metavar_location(
621626
psess: &ParseSess,
622627
stack: &[Frame<'_>],
@@ -808,12 +813,13 @@ fn lockstep_iter_size(
808813
/// * `[ $( ${count(foo, 0)} ),* ]` will be the same as `[ $( ${count(foo)} ),* ]`
809814
/// * `[ $( ${count(foo, 1)} ),* ]` will return an error because `${count(foo, 1)}` is
810815
/// declared inside a single repetition and the index `1` implies two nested repetitions.
816+
#[inline(always)] // called once
811817
fn count_repetitions<'dx>(
812818
dcx: DiagCtxtHandle<'dx>,
813819
depth_user: usize,
814820
mut matched: &NamedMatch,
815821
repeats: &[(usize, usize)],
816-
sp: &DelimSpan,
822+
sp: DelimSpan,
817823
) -> PResult<'dx, usize> {
818824
// Recursively count the number of matches in `matched` at given depth
819825
// (or at the top-level of `matched` if no depth is given).

0 commit comments

Comments
 (0)