Skip to content

Commit 5b0c56b

Browse files
committed
Introduce UsageMap::user_map.
`UsageMap` contains `used_map`, which maps from an item to the item it uses. This commit add `user_map`, which is the inverse. We already compute this inverse, but later on, and it is only held as a local variable. Its simpler and nicer to put it next to `used_map`.
1 parent de2911f commit 5b0c56b

File tree

2 files changed

+14
-21
lines changed

2 files changed

+14
-21
lines changed

compiler/rustc_monomorphize/src/collector.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,9 @@ pub struct UsageMap<'tcx> {
213213
// are represented as a range, which indexes into `used_items`.
214214
used_map: FxHashMap<MonoItem<'tcx>, Range<usize>>,
215215

216+
// Maps every mono item to the mono items that use it.
217+
user_map: FxHashMap<MonoItem<'tcx>, Vec<MonoItem<'tcx>>>,
218+
216219
// A mono item that is used by N different other mono items will appear
217220
// here N times. Indexed into by the ranges in `used_map`.
218221
used_items: Vec<MonoItem<'tcx>>,
@@ -222,7 +225,11 @@ type MonoItems<'tcx> = Vec<Spanned<MonoItem<'tcx>>>;
222225

223226
impl<'tcx> UsageMap<'tcx> {
224227
fn new() -> UsageMap<'tcx> {
225-
UsageMap { used_map: FxHashMap::default(), used_items: Vec::new() }
228+
UsageMap {
229+
used_map: FxHashMap::default(),
230+
user_map: FxHashMap::default(),
231+
used_items: Vec::new(),
232+
}
226233
}
227234

228235
fn record_used<'a>(
@@ -240,11 +247,16 @@ impl<'tcx> UsageMap<'tcx> {
240247

241248
for Spanned { node: used_item, .. } in used_items.into_iter() {
242249
self.used_items.push(*used_item);
250+
self.user_map.entry(*used_item).or_default().push(user_item);
243251
}
244252

245253
assert!(self.used_map.insert(user_item, new_items_range).is_none());
246254
}
247255

256+
pub fn get_user_items(&self, item: MonoItem<'tcx>) -> Option<&[MonoItem<'tcx>]> {
257+
self.user_map.get(&item).map(|items| items.as_slice())
258+
}
259+
248260
/// Internally iterate over all inlined items used by `item`.
249261
pub fn for_each_inlined_used_item<F>(&self, tcx: TyCtxt<'tcx>, item: MonoItem<'tcx>, mut f: F)
250262
where
@@ -259,16 +271,6 @@ impl<'tcx> UsageMap<'tcx> {
259271
}
260272
}
261273
}
262-
263-
/// Internally iterate over each item and the items used by it.
264-
pub fn for_each_item_and_its_used_items<F>(&self, mut f: F)
265-
where
266-
F: FnMut(MonoItem<'tcx>, &[MonoItem<'tcx>]),
267-
{
268-
for (&item, range) in &self.used_map {
269-
f(item, &self.used_items[range.clone()])
270-
}
271-
}
272274
}
273275

274276
#[instrument(skip(tcx, mode), level = "debug")]

compiler/rustc_monomorphize/src/partitioning.rs

+1-10
Original file line numberDiff line numberDiff line change
@@ -514,15 +514,6 @@ fn internalize_symbols<'tcx>(
514514
return;
515515
}
516516

517-
// Build a map from every monomorphization to all the monomorphizations that
518-
// reference it.
519-
let mut user_map: FxHashMap<MonoItem<'tcx>, Vec<MonoItem<'tcx>>> = Default::default();
520-
cx.usage_map.for_each_item_and_its_used_items(|user_item, used_items| {
521-
for used_item in used_items {
522-
user_map.entry(*used_item).or_default().push(user_item);
523-
}
524-
});
525-
526517
// For each internalization candidates in each codegen unit, check if it is
527518
// used from outside its defining codegen unit.
528519
for cgu in codegen_units {
@@ -535,7 +526,7 @@ fn internalize_symbols<'tcx>(
535526
}
536527
debug_assert_eq!(mono_item_placements[item], home_cgu);
537528

538-
if let Some(user_items) = user_map.get(item) {
529+
if let Some(user_items) = cx.usage_map.get_user_items(*item) {
539530
if user_items
540531
.iter()
541532
.filter_map(|user_item| {

0 commit comments

Comments
 (0)