From 6141e639022d2f21f9cf553e5cb7a1725c498a02 Mon Sep 17 00:00:00 2001 From: xmakro Date: Tue, 8 Sep 2026 13:47:11 -0700 Subject: [PATCH] Avoid small macro matching and transcription allocations [skip ci] --- compiler/rustc_expand/src/mbe/macro_parser.rs | 7 +++++-- compiler/rustc_expand/src/mbe/transcribe.rs | 5 +++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_expand/src/mbe/macro_parser.rs b/compiler/rustc_expand/src/mbe/macro_parser.rs index 3e94e0ca34773..d096ccf1e5933 100644 --- a/compiler/rustc_expand/src/mbe/macro_parser.rs +++ b/compiler/rustc_expand/src/mbe/macro_parser.rs @@ -722,14 +722,17 @@ impl TtParser { ErrorReported(guarantee) } - fn nameize>( + fn nameize>( &self, matcher: &[MatcherLoc], mut res: I, ) -> NamedMatches { // Make that each metavar has _exactly one_ binding. If so, insert the binding into the // `NamedParseResult`. Otherwise, it's an error. - let mut ret_val = FxHashMap::default(); + if res.len() == 0 { + return FxHashMap::default(); + } + let mut ret_val = FxHashMap::with_capacity_and_hasher(res.len(), Default::default()); for loc in matcher { if let &MatcherLoc::MetaVarDecl { bind, .. } = loc && ret_val diff --git a/compiler/rustc_expand/src/mbe/transcribe.rs b/compiler/rustc_expand/src/mbe/transcribe.rs index eabec05cd66c6..fc6adbf1458f6 100644 --- a/compiler/rustc_expand/src/mbe/transcribe.rs +++ b/compiler/rustc_expand/src/mbe/transcribe.rs @@ -186,7 +186,7 @@ pub(super) fn transcribe<'a>( src_span, DelimSpacing::new(Spacing::Alone, Spacing::Alone) )], - result: Vec::new(), + result: Vec::with_capacity(src.tts.len()), result_stack: Vec::new(), }; @@ -271,7 +271,8 @@ pub(super) fn transcribe<'a>( tscx.marker.mark_span(&mut span.open); tscx.marker.mark_span(&mut span.close); tscx.stack.push(Frame::new_delimited(delimited, span, *spacing)); - tscx.result_stack.push(mem::take(&mut tscx.result)); + tscx.result_stack + .push(mem::replace(&mut tscx.result, Vec::with_capacity(delimited.tts.len()))); } // Nothing much to do here. Just push the token to the result, being careful to