From 25b34de8c9b3cbda6f6de0d62f369ef3bb1d883c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Tue, 8 Sep 2026 16:32:22 +0200 Subject: [PATCH 1/2] Reuse `reachable_inlined_items` --- compiler/rustc_monomorphize/src/partitioning.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_monomorphize/src/partitioning.rs b/compiler/rustc_monomorphize/src/partitioning.rs index 29b624afd0614..24fac7db1f773 100644 --- a/compiler/rustc_monomorphize/src/partitioning.rs +++ b/compiler/rustc_monomorphize/src/partitioning.rs @@ -216,6 +216,7 @@ where let cgu_name_builder = &mut CodegenUnitNameBuilder::new(cx.tcx); let cgu_name_cache = &mut UnordMap::default(); + let mut reachable_inlined_items = FxIndexSet::default(); for mono_item in mono_items { // Handle only root (GloballyShared) items directly here. Inlined (LocalCopy) items @@ -264,15 +265,15 @@ where // going via another root item. This includes drop-glue, functions from // external crates, and local functions the definition of which is // marked with `#[inline]`. - let mut reachable_inlined_items = FxIndexSet::default(); + reachable_inlined_items.clear(); get_reachable_inlined_items(cx.tcx, mono_item, cx.usage_map, &mut reachable_inlined_items); // Add those inlined items. It's possible an inlined item is reachable // from multiple root items within a CGU, which is fine, it just means // the `insert` will be a no-op. - for inlined_item in reachable_inlined_items { + for inlined_item in &reachable_inlined_items { // This is a CGU-private copy. - cgu.items_mut().entry(inlined_item).or_insert_with(|| MonoItemData { + cgu.items_mut().entry(*inlined_item).or_insert_with(|| MonoItemData { inlined: true, linkage: Linkage::Internal, visibility: Visibility::Default, From 430c6f4c4464a9f76b3e0098b3eca1156ea18a02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Tue, 8 Sep 2026 15:03:10 +0200 Subject: [PATCH 2/2] Reserve capacity for used and mentioned items --- compiler/rustc_monomorphize/src/collector.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/compiler/rustc_monomorphize/src/collector.rs b/compiler/rustc_monomorphize/src/collector.rs index 4ee1abe4a1ff4..70b6bda25be5e 100644 --- a/compiler/rustc_monomorphize/src/collector.rs +++ b/compiler/rustc_monomorphize/src/collector.rs @@ -326,6 +326,10 @@ impl<'tcx> MonoItems<'tcx> { self.items.entry(item.node).or_insert(item.span); } + fn reserve(&mut self, additional: usize) { + self.items.reserve(additional); + } + fn items(&self) -> impl Iterator> { self.items.keys().cloned() } @@ -487,7 +491,9 @@ fn collect_items_rec<'tcx>( def_path_str, }); }; + used_items.reserve(used.len()); used_items.extend(used.into_iter().copied()); + mentioned_items.reserve(mentioned.len()); mentioned_items.extend(mentioned.into_iter().copied()); } MonoItem::GlobalAsm(item_id) => {