From c6d1849411f2c054cffa3bafa4c3d685e8080fb9 Mon Sep 17 00:00:00 2001 From: Jonathan Brouwer Date: Tue, 8 Sep 2026 20:41:43 +0200 Subject: [PATCH 1/2] Reserve items in `Extend` implementation of `MonoItems` --- compiler/rustc_monomorphize/src/collector.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/compiler/rustc_monomorphize/src/collector.rs b/compiler/rustc_monomorphize/src/collector.rs index 4ee1abe4a1ff4..622666a12ef40 100644 --- a/compiler/rustc_monomorphize/src/collector.rs +++ b/compiler/rustc_monomorphize/src/collector.rs @@ -345,6 +345,8 @@ impl<'tcx> Extend>> for MonoItems<'tcx> { where I: IntoIterator>>, { + let iter = iter.into_iter(); + self.items.reserve(iter.size_hint().0); for item in iter { self.push(item) } From 5bd43f35e5ec90ac97e035a70683412a5c35e9f5 Mon Sep 17 00:00:00 2001 From: Jonathan Brouwer Date: Tue, 8 Sep 2026 20:46:38 +0200 Subject: [PATCH 2/2] Do the same optimization in more places --- compiler/rustc_data_structures/src/sso/map.rs | 4 +++- compiler/rustc_data_structures/src/sso/set.rs | 4 +++- compiler/rustc_infer/src/traits/util.rs | 2 ++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_data_structures/src/sso/map.rs b/compiler/rustc_data_structures/src/sso/map.rs index 827c82fa46a11..018cbd96317db 100644 --- a/compiler/rustc_data_structures/src/sso/map.rs +++ b/compiler/rustc_data_structures/src/sso/map.rs @@ -356,7 +356,9 @@ impl Extend<(K, V)> for SsoHashMap { where I: IntoIterator, { - for (key, value) in iter.into_iter() { + let iter = iter.into_iter(); + self.reserve(iter.size_hint().0); + for (key, value) in iter { self.insert(key, value); } } diff --git a/compiler/rustc_data_structures/src/sso/set.rs b/compiler/rustc_data_structures/src/sso/set.rs index e3fa1cbf4cc57..cf75ea4537012 100644 --- a/compiler/rustc_data_structures/src/sso/set.rs +++ b/compiler/rustc_data_structures/src/sso/set.rs @@ -168,7 +168,9 @@ impl Extend for SsoHashSet { where I: IntoIterator, { - for val in iter.into_iter() { + let iter = iter.into_iter(); + self.reserve(iter.size_hint().0); + for val in iter { self.insert(val); } } diff --git a/compiler/rustc_infer/src/traits/util.rs b/compiler/rustc_infer/src/traits/util.rs index cc29546adb880..0380488caf96f 100644 --- a/compiler/rustc_infer/src/traits/util.rs +++ b/compiler/rustc_infer/src/traits/util.rs @@ -47,6 +47,8 @@ impl<'tcx> PredicateSet<'tcx> { impl<'tcx> Extend> for PredicateSet<'tcx> { fn extend>>(&mut self, iter: I) { + let iter = iter.into_iter(); + self.set.reserve(iter.size_hint().0); for pred in iter { self.insert(pred); }