diff --git a/compiler/rustc_monomorphize/src/collector.rs b/compiler/rustc_monomorphize/src/collector.rs index 345d2e3d906..51ccdca69c8 100644 --- a/compiler/rustc_monomorphize/src/collector.rs +++ b/compiler/rustc_monomorphize/src/collector.rs @@ -213,6 +213,9 @@ pub struct UsageMap<'tcx> { // are represented as a range, which indexes into `used_items`. used_map: FxHashMap, Range>, + // Maps every mono item to the mono items that use it. + user_map: FxHashMap, Vec>>, + // A mono item that is used by N different other mono items will appear // here N times. Indexed into by the ranges in `used_map`. used_items: Vec>, @@ -222,7 +225,11 @@ pub struct UsageMap<'tcx> { impl<'tcx> UsageMap<'tcx> { fn new() -> UsageMap<'tcx> { - UsageMap { used_map: FxHashMap::default(), used_items: Vec::new() } + UsageMap { + used_map: FxHashMap::default(), + user_map: FxHashMap::default(), + used_items: Vec::new(), + } } fn record_used<'a>( @@ -240,11 +247,16 @@ fn record_used<'a>( for Spanned { node: used_item, .. } in used_items.into_iter() { self.used_items.push(*used_item); + self.user_map.entry(*used_item).or_default().push(user_item); } assert!(self.used_map.insert(user_item, new_items_range).is_none()); } + pub fn get_user_items(&self, item: MonoItem<'tcx>) -> Option<&[MonoItem<'tcx>]> { + self.user_map.get(&item).map(|items| items.as_slice()) + } + /// Internally iterate over all inlined items used by `item`. pub fn for_each_inlined_used_item(&self, tcx: TyCtxt<'tcx>, item: MonoItem<'tcx>, mut f: F) where @@ -259,16 +271,6 @@ pub fn for_each_inlined_used_item(&self, tcx: TyCtxt<'tcx>, item: MonoItem<'t } } } - - /// Internally iterate over each item and the items used by it. - pub fn for_each_item_and_its_used_items(&self, mut f: F) - where - F: FnMut(MonoItem<'tcx>, &[MonoItem<'tcx>]), - { - for (&item, range) in &self.used_map { - f(item, &self.used_items[range.clone()]) - } - } } #[instrument(skip(tcx, mode), level = "debug")] diff --git a/compiler/rustc_monomorphize/src/partitioning.rs b/compiler/rustc_monomorphize/src/partitioning.rs index 5d707e62430..8932288a161 100644 --- a/compiler/rustc_monomorphize/src/partitioning.rs +++ b/compiler/rustc_monomorphize/src/partitioning.rs @@ -514,15 +514,6 @@ fn internalize_symbols<'tcx>( return; } - // Build a map from every monomorphization to all the monomorphizations that - // reference it. - let mut user_map: FxHashMap, Vec>> = Default::default(); - cx.usage_map.for_each_item_and_its_used_items(|user_item, used_items| { - for used_item in used_items { - user_map.entry(*used_item).or_default().push(user_item); - } - }); - // For each internalization candidates in each codegen unit, check if it is // used from outside its defining codegen unit. for cgu in codegen_units { @@ -535,7 +526,7 @@ fn internalize_symbols<'tcx>( } debug_assert_eq!(mono_item_placements[item], home_cgu); - if let Some(user_items) = user_map.get(item) { + if let Some(user_items) = cx.usage_map.get_user_items(*item) { if user_items .iter() .filter_map(|user_item| {