Skip to content

Commit f506b04

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 a17780d + 39bb3c7 commit f506b04

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

compiler/rustc_errors/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,7 @@ impl DiagCtxt {
833833
*fulfilled_expectations = Default::default();
834834
}
835835

836+
#[inline]
836837
pub fn handle<'a>(&'a self) -> DiagCtxtHandle<'a> {
837838
DiagCtxtHandle { dcx: self, tainted_with_errors: None }
838839
}

compiler/rustc_expand/src/mbe/transcribe.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ struct TranscrCtx<'psess, 'itp> {
7070

7171
impl<'psess> TranscrCtx<'psess, '_> {
7272
/// Span marked with the correct expansion and transparency.
73+
#[inline(always)]
7374
fn visited_dspan(&mut self, dspan: DelimSpan) -> Span {
7475
let mut span = dspan.entire();
7576
self.marker.mark_span(&mut span);
@@ -86,6 +87,7 @@ struct Marker {
8687

8788
impl Marker {
8889
/// Mark a span with the stored expansion ID and transparency.
90+
#[inline(always)]
8991
fn mark_span(&mut self, span: &mut Span) {
9092
// `apply_mark` is a relatively expensive operation, both due to taking hygiene lock, and
9193
// by itself. All tokens in a macro body typically have the same syntactic context, unless
@@ -113,6 +115,7 @@ enum FrameKind {
113115
}
114116

115117
impl<'a> Frame<'a> {
118+
#[inline(always)]
116119
fn new_delimited(src: &'a mbe::Delimited, span: DelimSpan, spacing: DelimSpacing) -> Frame<'a> {
117120
Frame {
118121
tts: &src.tts,
@@ -121,6 +124,7 @@ impl<'a> Frame<'a> {
121124
}
122125
}
123126

127+
#[inline(always)]
124128
fn new_sequence(
125129
src: &'a mbe::SequenceRepetition,
126130
sep: Option<Token>,
@@ -133,6 +137,7 @@ impl<'a> Frame<'a> {
133137
impl<'a> Iterator for Frame<'a> {
134138
type Item = &'a mbe::TokenTree;
135139

140+
#[inline(always)]
136141
fn next(&mut self) -> Option<&'a mbe::TokenTree> {
137142
let res = self.tts.get(self.idx);
138143
self.idx += 1;
@@ -289,6 +294,7 @@ pub(super) fn transcribe<'a>(
289294
}
290295

291296
/// Turn `$(...)*` sequences into tokens.
297+
#[inline(always)] // called once
292298
fn transcribe_sequence<'tx, 'itp>(
293299
tscx: &mut TranscrCtx<'tx, 'itp>,
294300
seq: &mbe::TokenTree,
@@ -357,6 +363,7 @@ fn transcribe_sequence<'tx, 'itp>(
357363
/// producing "xyz", which is bad because it effectively merges tokens.
358364
/// `Spacing::Alone` is the safer option. Fortunately, `space_between` will avoid
359365
/// some of the unnecessary whitespace.
366+
#[inline(always)] // called once
360367
fn transcribe_metavar<'tx>(
361368
tscx: &mut TranscrCtx<'tx, '_>,
362369
mut sp: Span,
@@ -492,6 +499,7 @@ fn transcribe_metavar<'tx>(
492499
}
493500

494501
/// Turn `${expr(...)}` metavariable expressionss into tokens.
502+
#[inline(always)] // called once
495503
fn transcribe_metavar_expr<'tx>(
496504
tscx: &mut TranscrCtx<'tx, '_>,
497505
dspan: DelimSpan,
@@ -502,7 +510,7 @@ fn transcribe_metavar_expr<'tx>(
502510
MetaVarExpr::Concat(ref elements) => metavar_expr_concat(tscx, dspan, elements)?,
503511
MetaVarExpr::Count(original_ident, depth) => {
504512
let matched = matched_from_ident(dcx, original_ident, tscx.interp)?;
505-
let count = count_repetitions(dcx, depth, matched, &tscx.repeats, &dspan)?;
513+
let count = count_repetitions(dcx, depth, matched, &tscx.repeats, dspan)?;
506514
TokenTree::token_alone(
507515
TokenKind::lit(token::Integer, sym::integer(count), None),
508516
tscx.visited_dspan(dspan),
@@ -537,6 +545,7 @@ fn transcribe_metavar_expr<'tx>(
537545
}
538546

539547
/// Handle the `${concat(...)}` metavariable expression.
548+
#[inline(always)] // called once
540549
fn metavar_expr_concat<'tx>(
541550
tscx: &mut TranscrCtx<'tx, '_>,
542551
dspan: DelimSpan,
@@ -617,6 +626,7 @@ fn metavar_expr_concat<'tx>(
617626
/// These are typically used for passing larger amounts of code, and tokens in that code usually
618627
/// combine with each other and not with tokens outside of the sequence.
619628
/// - The metavariable span comes from a different crate, then we prefer the more local span.
629+
#[inline(always)] // called once
620630
fn maybe_use_metavar_location(
621631
psess: &ParseSess,
622632
stack: &[Frame<'_>],
@@ -682,6 +692,7 @@ fn maybe_use_metavar_location(
682692
/// See the definition of `repeats` in the `transcribe` function. `repeats` is used to descend
683693
/// into the right place in nested matchers. If we attempt to descend too far, the macro writer has
684694
/// made a mistake, and we return `None`.
695+
#[inline(always)]
685696
fn lookup_cur_matched<'a>(
686697
ident: MacroRulesNormalizedIdent,
687698
interpolations: &'a FxHashMap<MacroRulesNormalizedIdent, NamedMatch>,
@@ -722,6 +733,7 @@ impl LockstepIterSize {
722733
/// - `Unconstrained` is compatible with everything.
723734
/// - `Contradiction` is incompatible with everything.
724735
/// - `Constraint(len)` is only compatible with other constraints of the same length.
736+
#[inline(always)]
725737
fn with(self, other: LockstepIterSize) -> LockstepIterSize {
726738
match self {
727739
LockstepIterSize::Unconstrained => other,
@@ -759,6 +771,7 @@ impl LockstepIterSize {
759771
/// declared at depths which weren't equal or there was a compiler bug. For example, if we have 3 repetitions of
760772
/// the outer sequence and 4 repetitions of the inner sequence for `x`, we should have the same for
761773
/// `y`; otherwise, we can't transcribe them both at the given depth.
774+
#[inline(always)]
762775
fn lockstep_iter_size(
763776
tree: &mbe::TokenTree,
764777
interpolations: &FxHashMap<MacroRulesNormalizedIdent, NamedMatch>,
@@ -808,12 +821,13 @@ fn lockstep_iter_size(
808821
/// * `[ $( ${count(foo, 0)} ),* ]` will be the same as `[ $( ${count(foo)} ),* ]`
809822
/// * `[ $( ${count(foo, 1)} ),* ]` will return an error because `${count(foo, 1)}` is
810823
/// declared inside a single repetition and the index `1` implies two nested repetitions.
824+
#[inline(always)] // called once
811825
fn count_repetitions<'dx>(
812826
dcx: DiagCtxtHandle<'dx>,
813827
depth_user: usize,
814828
mut matched: &NamedMatch,
815829
repeats: &[(usize, usize)],
816-
sp: &DelimSpan,
830+
sp: DelimSpan,
817831
) -> PResult<'dx, usize> {
818832
// Recursively count the number of matches in `matched` at given depth
819833
// (or at the top-level of `matched` if no depth is given).
@@ -869,6 +883,7 @@ fn count_repetitions<'dx>(
869883
}
870884

871885
/// Returns a `NamedMatch` item declared on the LHS given an arbitrary [Ident]
886+
#[inline(always)]
872887
fn matched_from_ident<'ctx, 'interp, 'rslt>(
873888
dcx: DiagCtxtHandle<'ctx>,
874889
ident: Ident,
@@ -900,6 +915,7 @@ fn out_of_bounds_err<'a>(dcx: DiagCtxtHandle<'a>, max: usize, span: Span, ty: &s
900915
}
901916

902917
/// Extracts an metavariable symbol that can be an identifier, a token tree or a literal.
918+
#[inline(always)]
903919
fn extract_symbol_from_pnr<'a>(
904920
dcx: DiagCtxtHandle<'a>,
905921
pnr: &ParseNtResult,

compiler/rustc_session/src/parse.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ impl ParseSess {
334334
self.proc_macro_quoted_spans.iter_enumerated()
335335
}
336336

337+
#[inline]
337338
pub fn dcx(&self) -> DiagCtxtHandle<'_> {
338339
self.dcx.handle()
339340
}

0 commit comments

Comments
 (0)