Skip to content

Commit 0ee9cfd

Browse files
committed
Auto merge of #119693 - petrochenkov:cachemark, r=cjgillot
macro_rules: Add an expansion-local cache to span marker Most tokens in a macro body typically have the same syntax context. So the cache should usually be hit. This change can either be combined with #119689, or serve as its alternative, depending on perf results.
2 parents 76101ee + edec91d commit 0ee9cfd

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

compiler/rustc_expand/src/mbe/transcribe.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,28 @@ use rustc_errors::DiagnosticBuilder;
1313
use rustc_errors::{pluralize, PResult};
1414
use rustc_span::hygiene::{LocalExpnId, Transparency};
1515
use rustc_span::symbol::{sym, Ident, MacroRulesNormalizedIdent};
16-
use rustc_span::Span;
16+
use rustc_span::{Span, SyntaxContext};
1717

1818
use smallvec::{smallvec, SmallVec};
1919
use std::mem;
2020

2121
// A Marker adds the given mark to the syntax context.
22-
struct Marker(LocalExpnId, Transparency);
22+
struct Marker(LocalExpnId, Transparency, FxHashMap<SyntaxContext, SyntaxContext>);
2323

2424
impl MutVisitor for Marker {
2525
const VISIT_TOKENS: bool = true;
2626

2727
fn visit_span(&mut self, span: &mut Span) {
28-
*span = span.apply_mark(self.0.to_expn_id(), self.1)
28+
// `apply_mark` is a relatively expensive operation, both due to taking hygiene lock, and
29+
// by itself. All tokens in a macro body typically have the same syntactic context, unless
30+
// it's some advanced case with macro-generated macros. So if we cache the marked version
31+
// of that context once, we'll typically have a 100% cache hit rate after that.
32+
let Marker(expn_id, transparency, ref mut cache) = *self;
33+
let data = span.data();
34+
let marked_ctxt = *cache
35+
.entry(data.ctxt)
36+
.or_insert_with(|| data.ctxt.apply_mark(expn_id.to_expn_id(), transparency));
37+
*span = data.with_ctxt(marked_ctxt);
2938
}
3039
}
3140

@@ -123,7 +132,7 @@ pub(super) fn transcribe<'a>(
123132
// again, and we are done transcribing.
124133
let mut result: Vec<TokenTree> = Vec::new();
125134
let mut result_stack = Vec::new();
126-
let mut marker = Marker(cx.current_expansion.id, transparency);
135+
let mut marker = Marker(cx.current_expansion.id, transparency, Default::default());
127136

128137
loop {
129138
// Look at the last frame on the stack.

compiler/rustc_span/src/hygiene.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ impl SyntaxContext {
658658
}
659659

660660
/// Extend a syntax context with a given expansion and transparency.
661-
pub(crate) fn apply_mark(self, expn_id: ExpnId, transparency: Transparency) -> SyntaxContext {
661+
pub fn apply_mark(self, expn_id: ExpnId, transparency: Transparency) -> SyntaxContext {
662662
HygieneData::with(|data| data.apply_mark(self, expn_id, transparency))
663663
}
664664

0 commit comments

Comments
 (0)