From f1fe797ee2b4f1e9d47eb468212b742960dafb87 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Thu, 1 Jun 2023 13:25:26 +1000 Subject: [PATCH] Change representation of `UsageMap::used_map`. It currently uses ranges, which index into `UsageMap::used_items`. This commit changes it to just use `Vec`, which is much simpler to construct and use. This change does result in more allocations, but it is few enough that the perf impact is negligible. --- compiler/rustc_monomorphize/src/collector.rs | 41 ++++++-------------- 1 file changed, 12 insertions(+), 29 deletions(-) diff --git a/compiler/rustc_monomorphize/src/collector.rs b/compiler/rustc_monomorphize/src/collector.rs index 51ccdca69c8..f4ee7b75875 100644 --- a/compiler/rustc_monomorphize/src/collector.rs +++ b/compiler/rustc_monomorphize/src/collector.rs @@ -195,7 +195,6 @@ use rustc_session::lint::builtin::LARGE_ASSIGNMENTS; use rustc_session::Limit; use rustc_span::source_map::{dummy_spanned, respan, Span, Spanned, DUMMY_SP}; use rustc_target::abi::Size; -use std::ops::Range; use std::path::PathBuf; use crate::errors::{ @@ -209,27 +208,18 @@ pub enum MonoItemCollectionMode { } pub struct UsageMap<'tcx> { - // Maps every mono item to the mono items used by it. Those mono items - // are represented as a range, which indexes into `used_items`. - used_map: FxHashMap, Range>, + // Maps every mono item to the mono items used by it. + used_map: FxHashMap, Vec>>, // 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>, } type MonoItems<'tcx> = Vec>>; impl<'tcx> UsageMap<'tcx> { fn new() -> UsageMap<'tcx> { - UsageMap { - used_map: FxHashMap::default(), - user_map: FxHashMap::default(), - used_items: Vec::new(), - } + UsageMap { used_map: FxHashMap::default(), user_map: FxHashMap::default() } } fn record_used<'a>( @@ -239,18 +229,12 @@ impl<'tcx> UsageMap<'tcx> { ) where 'tcx: 'a, { - let old_len = self.used_items.len(); - let new_len = old_len + used_items.len(); - let new_items_range = old_len..new_len; - - self.used_items.reserve(used_items.len()); - - 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); + let used_items: Vec<_> = used_items.iter().map(|item| item.node).collect(); + for &used_item in used_items.iter() { + self.user_map.entry(used_item).or_default().push(user_item); } - assert!(self.used_map.insert(user_item, new_items_range).is_none()); + assert!(self.used_map.insert(user_item, used_items).is_none()); } pub fn get_user_items(&self, item: MonoItem<'tcx>) -> Option<&[MonoItem<'tcx>]> { @@ -262,12 +246,11 @@ impl<'tcx> UsageMap<'tcx> { where F: FnMut(MonoItem<'tcx>), { - if let Some(range) = self.used_map.get(&item) { - for used_item in self.used_items[range.clone()].iter() { - let is_inlined = used_item.instantiation_mode(tcx) == InstantiationMode::LocalCopy; - if is_inlined { - f(*used_item); - } + let used_items = self.used_map.get(&item).unwrap(); + for used_item in used_items.iter() { + let is_inlined = used_item.instantiation_mode(tcx) == InstantiationMode::LocalCopy; + if is_inlined { + f(*used_item); } } }