Skip to content

Commit 60f5679

Browse files
committed
Add capacity to PlaceholderExpander
1 parent f0b76d5 commit 60f5679

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

compiler/rustc_expand/src/expand.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
508508
// Unresolved macros produce dummy outputs as a recovery measure.
509509
invocations.reverse();
510510
let mut expanded_fragments = Vec::new();
511+
let mut expanded_fragments_len = 0;
511512
let mut undetermined_invocations = Vec::new();
512513
let (mut progress, mut force) = (false, !self.monotonic);
513514
loop {
@@ -602,6 +603,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
602603
expanded_fragments.push(Vec::new());
603604
}
604605
expanded_fragments[depth - 1].push((expn_id, expanded_fragment));
606+
expanded_fragments_len += 1;
605607
invocations.extend(derive_invocations.into_iter().rev());
606608
}
607609
ExpandResult::Retry(invoc) => {
@@ -622,7 +624,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
622624
self.cx.force_mode = orig_force_mode;
623625

624626
// Finally incorporate all the expanded macros into the input AST fragment.
625-
let mut placeholder_expander = PlaceholderExpander::default();
627+
let mut placeholder_expander = PlaceholderExpander::with_capacity(expanded_fragments_len);
626628
while let Some(expanded_fragments) = expanded_fragments.pop() {
627629
for (expn_id, expanded_fragment) in expanded_fragments.into_iter().rev() {
628630
placeholder_expander

compiler/rustc_expand/src/placeholders.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,17 @@ pub(crate) fn placeholder(
218218
}
219219
}
220220

221-
#[derive(Default)]
222221
pub(crate) struct PlaceholderExpander {
223222
expanded_fragments: FxHashMap<ast::NodeId, AstFragment>,
224223
}
225224

226225
impl PlaceholderExpander {
226+
pub(crate) fn with_capacity(capacity: usize) -> Self {
227+
PlaceholderExpander {
228+
expanded_fragments: FxHashMap::with_capacity_and_hasher(capacity, Default::default()),
229+
}
230+
}
231+
227232
pub(crate) fn add(&mut self, id: ast::NodeId, mut fragment: AstFragment) {
228233
fragment.mut_visit_with(self);
229234
self.expanded_fragments.insert(id, fragment);

0 commit comments

Comments
 (0)