Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions compiler/rustc_monomorphize/src/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Item = MonoItem<'tcx>> {
self.items.keys().cloned()
}
Expand Down Expand Up @@ -487,7 +491,9 @@ fn collect_items_rec<'tcx>(
def_path_str,
});
};
used_items.reserve(used.len());

@JonathanBrouwer JonathanBrouwer Sep 8, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of reserving outside of the extend call which is easily forgotten, how about this implementation:
#162495

View changes since the review

used_items.extend(used.into_iter().copied());
mentioned_items.reserve(mentioned.len());
mentioned_items.extend(mentioned.into_iter().copied());
}
MonoItem::GlobalAsm(item_id) => {
Expand Down
7 changes: 4 additions & 3 deletions compiler/rustc_monomorphize/src/partitioning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {

@JonathanBrouwer JonathanBrouwer Sep 8, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative which might be slightly nicer as it doesn't need a copy:
reachable_inlined_items.drain(..)
and remove the .clear() above

The current implementation is also fine by me tho, if you prefer it. I think both of our options should be identical for performance

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that iter + clear is generally faster than drain, because the implementation of drain tends to be complicated.

// 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,
Expand Down
Loading