authorbors <bors@rust-lang.org> 2026-02-03 22:20:57 UTC
committerbors <bors@rust-lang.org> 2026-02-03 22:20:57 UTC
log0c40f5be0c8eb04d206e928c5d8c133a9142143a
tree81e34dfa77bcde25261446ca4487fc5aee556ff6
parent366a1b93e7f466ebe559477add05f064873d0c71
parent60f567916cc39add8a9b6571c1865577204ca729

Auto merge of #151929 - camsteffen:lengg, r=BoxyUwU

Use with_capacity in query_key_hash_verify and PlaceholderExpander Addresses the first two items from https://github.com/rust-lang/rust/issues/137005#issuecomment-2687803558.

5 files changed, 32 insertions(+), 4 deletions(-)

compiler/rustc_data_structures/src/vec_cache.rs+4
......@@ -359,6 +359,10 @@ where
359359 }
360360 }
361361 }
362
363 pub fn len(&self) -> usize {
364 self.len.load(Ordering::Acquire)
365 }
362366}
363367
364368#[cfg(test)]
compiler/rustc_expand/src/expand.rs+3-1
......@@ -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+6-1
......@@ -218,12 +218,17 @@ pub(crate) fn placeholder(
218218 }
219219}
220220
221#[derive(Default)]
222221pub(crate) struct PlaceholderExpander {
223222 expanded_fragments: FxHashMap<ast::NodeId, AstFragment>,
224223}
225224
226225impl 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);
compiler/rustc_query_impl/src/plumbing.rs+1-2
......@@ -410,9 +410,8 @@ pub(crate) fn query_key_hash_verify<'tcx>(
410410) {
411411 let _timer = qcx.tcx.prof.generic_activity_with_arg("query_key_hash_verify_for", query.name());
412412
413 let mut map = UnordMap::default();
414
415413 let cache = query.query_cache(qcx);
414 let mut map = UnordMap::with_capacity(cache.len());
416415 cache.iter(&mut |key, _, _| {
417416 let node = DepNode::construct(qcx.tcx, query.dep_kind(), key);
418417 if let Some(other_key) = map.insert(node, *key) {
compiler/rustc_query_system/src/query/caches.rs+18
......@@ -30,6 +30,8 @@ pub trait QueryCache: Sized {
3030 fn complete(&self, key: Self::Key, value: Self::Value, index: DepNodeIndex);
3131
3232 fn iter(&self, f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex));
33
34 fn len(&self) -> usize;
3335}
3436
3537/// In-memory cache for queries whose keys aren't suitable for any of the
......@@ -71,6 +73,10 @@ where
7173 }
7274 }
7375 }
76
77 fn len(&self) -> usize {
78 self.cache.len()
79 }
7480}
7581
7682/// In-memory cache for queries whose key type only has one value (e.g. `()`).
......@@ -107,6 +113,10 @@ where
107113 f(&(), &value.0, value.1)
108114 }
109115 }
116
117 fn len(&self) -> usize {
118 self.cache.get().is_some().into()
119 }
110120}
111121
112122/// In-memory cache for queries whose key is a [`DefId`].
......@@ -157,6 +167,10 @@ where
157167 });
158168 self.foreign.iter(f);
159169 }
170
171 fn len(&self) -> usize {
172 self.local.len() + self.foreign.len()
173 }
160174}
161175
162176impl<K, V> QueryCache for VecCache<K, V, DepNodeIndex>
......@@ -180,4 +194,8 @@ where
180194 fn iter(&self, f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex)) {
181195 self.iter(f)
182196 }
197
198 fn len(&self) -> usize {
199 self.len()
200 }
183201}