| author | bors <bors@rust-lang.org> 2026-02-03 22:20:57 UTC |
| committer | bors <bors@rust-lang.org> 2026-02-03 22:20:57 UTC |
| log | 0c40f5be0c8eb04d206e928c5d8c133a9142143a |
| tree | 81e34dfa77bcde25261446ca4487fc5aee556ff6 |
| parent | 366a1b93e7f466ebe559477add05f064873d0c71 |
| parent | 60f567916cc39add8a9b6571c1865577204ca729 |
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 |
| 359 | 359 | } |
| 360 | 360 | } |
| 361 | 361 | } |
| 362 | ||
| 363 | pub fn len(&self) -> usize { | |
| 364 | self.len.load(Ordering::Acquire) | |
| 365 | } | |
| 362 | 366 | } |
| 363 | 367 | |
| 364 | 368 | #[cfg(test)] |
compiler/rustc_expand/src/expand.rs+3-1| ... | ... | @@ -508,6 +508,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> { |
| 508 | 508 | // Unresolved macros produce dummy outputs as a recovery measure. |
| 509 | 509 | invocations.reverse(); |
| 510 | 510 | let mut expanded_fragments = Vec::new(); |
| 511 | let mut expanded_fragments_len = 0; | |
| 511 | 512 | let mut undetermined_invocations = Vec::new(); |
| 512 | 513 | let (mut progress, mut force) = (false, !self.monotonic); |
| 513 | 514 | loop { |
| ... | ... | @@ -602,6 +603,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> { |
| 602 | 603 | expanded_fragments.push(Vec::new()); |
| 603 | 604 | } |
| 604 | 605 | expanded_fragments[depth - 1].push((expn_id, expanded_fragment)); |
| 606 | expanded_fragments_len += 1; | |
| 605 | 607 | invocations.extend(derive_invocations.into_iter().rev()); |
| 606 | 608 | } |
| 607 | 609 | ExpandResult::Retry(invoc) => { |
| ... | ... | @@ -622,7 +624,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> { |
| 622 | 624 | self.cx.force_mode = orig_force_mode; |
| 623 | 625 | |
| 624 | 626 | // 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); | |
| 626 | 628 | while let Some(expanded_fragments) = expanded_fragments.pop() { |
| 627 | 629 | for (expn_id, expanded_fragment) in expanded_fragments.into_iter().rev() { |
| 628 | 630 | placeholder_expander |
compiler/rustc_expand/src/placeholders.rs+6-1| ... | ... | @@ -218,12 +218,17 @@ pub(crate) fn placeholder( |
| 218 | 218 | } |
| 219 | 219 | } |
| 220 | 220 | |
| 221 | #[derive(Default)] | |
| 222 | 221 | pub(crate) struct PlaceholderExpander { |
| 223 | 222 | expanded_fragments: FxHashMap<ast::NodeId, AstFragment>, |
| 224 | 223 | } |
| 225 | 224 | |
| 226 | 225 | 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 | ||
| 227 | 232 | pub(crate) fn add(&mut self, id: ast::NodeId, mut fragment: AstFragment) { |
| 228 | 233 | fragment.mut_visit_with(self); |
| 229 | 234 | 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>( |
| 410 | 410 | ) { |
| 411 | 411 | let _timer = qcx.tcx.prof.generic_activity_with_arg("query_key_hash_verify_for", query.name()); |
| 412 | 412 | |
| 413 | let mut map = UnordMap::default(); | |
| 414 | ||
| 415 | 413 | let cache = query.query_cache(qcx); |
| 414 | let mut map = UnordMap::with_capacity(cache.len()); | |
| 416 | 415 | cache.iter(&mut |key, _, _| { |
| 417 | 416 | let node = DepNode::construct(qcx.tcx, query.dep_kind(), key); |
| 418 | 417 | 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 { |
| 30 | 30 | fn complete(&self, key: Self::Key, value: Self::Value, index: DepNodeIndex); |
| 31 | 31 | |
| 32 | 32 | fn iter(&self, f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex)); |
| 33 | ||
| 34 | fn len(&self) -> usize; | |
| 33 | 35 | } |
| 34 | 36 | |
| 35 | 37 | /// In-memory cache for queries whose keys aren't suitable for any of the |
| ... | ... | @@ -71,6 +73,10 @@ where |
| 71 | 73 | } |
| 72 | 74 | } |
| 73 | 75 | } |
| 76 | ||
| 77 | fn len(&self) -> usize { | |
| 78 | self.cache.len() | |
| 79 | } | |
| 74 | 80 | } |
| 75 | 81 | |
| 76 | 82 | /// In-memory cache for queries whose key type only has one value (e.g. `()`). |
| ... | ... | @@ -107,6 +113,10 @@ where |
| 107 | 113 | f(&(), &value.0, value.1) |
| 108 | 114 | } |
| 109 | 115 | } |
| 116 | ||
| 117 | fn len(&self) -> usize { | |
| 118 | self.cache.get().is_some().into() | |
| 119 | } | |
| 110 | 120 | } |
| 111 | 121 | |
| 112 | 122 | /// In-memory cache for queries whose key is a [`DefId`]. |
| ... | ... | @@ -157,6 +167,10 @@ where |
| 157 | 167 | }); |
| 158 | 168 | self.foreign.iter(f); |
| 159 | 169 | } |
| 170 | ||
| 171 | fn len(&self) -> usize { | |
| 172 | self.local.len() + self.foreign.len() | |
| 173 | } | |
| 160 | 174 | } |
| 161 | 175 | |
| 162 | 176 | impl<K, V> QueryCache for VecCache<K, V, DepNodeIndex> |
| ... | ... | @@ -180,4 +194,8 @@ where |
| 180 | 194 | fn iter(&self, f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex)) { |
| 181 | 195 | self.iter(f) |
| 182 | 196 | } |
| 197 | ||
| 198 | fn len(&self) -> usize { | |
| 199 | self.len() | |
| 200 | } | |
| 183 | 201 | } |