1
Fork 0

Use UnhashMap whenever we have a key of DefPathHash

This commit is contained in:
Aaron Hill 2021-01-01 23:51:07 -05:00
parent 0876f59b97
commit 0dc9b26523
No known key found for this signature in database
GPG key ID: B4087E510E98B164
2 changed files with 12 additions and 10 deletions

View file

@ -11,6 +11,7 @@ use rustc_data_structures::fingerprint::{Fingerprint, FingerprintDecoder};
use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::svh::Svh; use rustc_data_structures::svh::Svh;
use rustc_data_structures::sync::{AtomicCell, Lock, LockGuard, Lrc, OnceCell}; use rustc_data_structures::sync::{AtomicCell, Lock, LockGuard, Lrc, OnceCell};
use rustc_data_structures::unhash::UnhashMap;
use rustc_errors::ErrorReported; use rustc_errors::ErrorReported;
use rustc_expand::base::{SyntaxExtension, SyntaxExtensionKind}; use rustc_expand::base::{SyntaxExtension, SyntaxExtensionKind};
use rustc_expand::proc_macro::{AttrProcMacro, BangProcMacro, ProcMacroDerive}; use rustc_expand::proc_macro::{AttrProcMacro, BangProcMacro, ProcMacroDerive};
@ -80,7 +81,7 @@ crate struct CrateMetadata {
/// For every definition in this crate, maps its `DefPathHash` to its /// For every definition in this crate, maps its `DefPathHash` to its
/// `DefIndex`. See `raw_def_id_to_def_id` for more details about how /// `DefIndex`. See `raw_def_id_to_def_id` for more details about how
/// this is used. /// this is used.
def_path_hash_map: OnceCell<FxHashMap<DefPathHash, DefIndex>>, def_path_hash_map: OnceCell<UnhashMap<DefPathHash, DefIndex>>,
/// Used for decoding interpret::AllocIds in a cached & thread-safe manner. /// Used for decoding interpret::AllocIds in a cached & thread-safe manner.
alloc_decoding_state: AllocDecodingState, alloc_decoding_state: AllocDecodingState,
/// The `DepNodeIndex` of the `DepNode` representing this upstream crate. /// The `DepNodeIndex` of the `DepNode` representing this upstream crate.
@ -1560,7 +1561,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
// stored in this crate. // stored in this crate.
let map = self.cdata.def_path_hash_map.get_or_init(|| { let map = self.cdata.def_path_hash_map.get_or_init(|| {
let end_id = self.root.tables.def_path_hashes.size() as u32; let end_id = self.root.tables.def_path_hashes.size() as u32;
let mut map = FxHashMap::with_capacity_and_hasher(end_id as usize, Default::default()); let mut map = UnhashMap::with_capacity_and_hasher(end_id as usize, Default::default());
for i in 0..end_id { for i in 0..end_id {
let def_index = DefIndex::from_u32(i); let def_index = DefIndex::from_u32(i);
// There may be gaps in the encoded table if we're decoding a proc-macro crate // There may be gaps in the encoded table if we're decoding a proc-macro crate

View file

@ -8,6 +8,7 @@ use rustc_data_structures::fingerprint::{Fingerprint, FingerprintDecoder, Finger
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet}; use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet};
use rustc_data_structures::sync::{HashMapExt, Lock, Lrc, OnceCell}; use rustc_data_structures::sync::{HashMapExt, Lock, Lrc, OnceCell};
use rustc_data_structures::thin_vec::ThinVec; use rustc_data_structures::thin_vec::ThinVec;
use rustc_data_structures::unhash::UnhashMap;
use rustc_errors::Diagnostic; use rustc_errors::Diagnostic;
use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, LOCAL_CRATE}; use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, LOCAL_CRATE};
use rustc_hir::definitions::DefPathHash; use rustc_hir::definitions::DefPathHash;
@ -87,7 +88,7 @@ pub struct OnDiskCache<'sess> {
// compilation session. This is used as an initial 'guess' when // compilation session. This is used as an initial 'guess' when
// we try to map a `DefPathHash` to its `DefId` in the current compilation // we try to map a `DefPathHash` to its `DefId` in the current compilation
// session. // session.
foreign_def_path_hashes: FxHashMap<DefPathHash, RawDefId>, foreign_def_path_hashes: UnhashMap<DefPathHash, RawDefId>,
// The *next* compilation sessison's `foreign_def_path_hashes` - at // The *next* compilation sessison's `foreign_def_path_hashes` - at
// the end of our current compilation session, this will get written // the end of our current compilation session, this will get written
@ -95,19 +96,19 @@ pub struct OnDiskCache<'sess> {
// will become `foreign_def_path_hashes` of the next compilation session. // will become `foreign_def_path_hashes` of the next compilation session.
// This stores any `DefPathHash` that we may need to map to a `DefId` // This stores any `DefPathHash` that we may need to map to a `DefId`
// during the next compilation session. // during the next compilation session.
latest_foreign_def_path_hashes: Lock<FxHashMap<DefPathHash, RawDefId>>, latest_foreign_def_path_hashes: Lock<UnhashMap<DefPathHash, RawDefId>>,
// Maps `DefPathHashes` to their corresponding `LocalDefId`s for all // Maps `DefPathHashes` to their corresponding `LocalDefId`s for all
// local items in the current compilation session. This is only populated // local items in the current compilation session. This is only populated
// when we are in incremental mode and have loaded a pre-existing cache // when we are in incremental mode and have loaded a pre-existing cache
// from disk, since this map is only used when deserializing a `DefPathHash` // from disk, since this map is only used when deserializing a `DefPathHash`
// from the incremental cache. // from the incremental cache.
local_def_path_hash_to_def_id: FxHashMap<DefPathHash, LocalDefId>, local_def_path_hash_to_def_id: UnhashMap<DefPathHash, LocalDefId>,
// Caches all lookups of `DefPathHashes`, both for local and foreign // Caches all lookups of `DefPathHashes`, both for local and foreign
// definitions. A definition from the previous compilation session // definitions. A definition from the previous compilation session
// may no longer exist in the current compilation session, so // may no longer exist in the current compilation session, so
// we use `Option<DefId>` so that we can cache a lookup failure. // we use `Option<DefId>` so that we can cache a lookup failure.
def_path_hash_to_def_id_cache: Lock<FxHashMap<DefPathHash, Option<DefId>>>, def_path_hash_to_def_id_cache: Lock<UnhashMap<DefPathHash, Option<DefId>>>,
} }
// This type is used only for serialization and deserialization. // This type is used only for serialization and deserialization.
@ -123,7 +124,7 @@ struct Footer {
syntax_contexts: FxHashMap<u32, AbsoluteBytePos>, syntax_contexts: FxHashMap<u32, AbsoluteBytePos>,
// See `OnDiskCache.expn_data` // See `OnDiskCache.expn_data`
expn_data: FxHashMap<u32, AbsoluteBytePos>, expn_data: FxHashMap<u32, AbsoluteBytePos>,
foreign_def_path_hashes: FxHashMap<DefPathHash, RawDefId>, foreign_def_path_hashes: UnhashMap<DefPathHash, RawDefId>,
} }
type EncodedQueryResultIndex = Vec<(SerializedDepNodeIndex, AbsoluteBytePos)>; type EncodedQueryResultIndex = Vec<(SerializedDepNodeIndex, AbsoluteBytePos)>;
@ -160,8 +161,8 @@ crate struct RawDefId {
pub index: u32, pub index: u32,
} }
fn make_local_def_path_hash_map(definitions: &Definitions) -> FxHashMap<DefPathHash, LocalDefId> { fn make_local_def_path_hash_map(definitions: &Definitions) -> UnhashMap<DefPathHash, LocalDefId> {
FxHashMap::from_iter( UnhashMap::from_iter(
definitions definitions
.def_path_table() .def_path_table()
.all_def_path_hashes_and_def_ids(LOCAL_CRATE) .all_def_path_hashes_and_def_ids(LOCAL_CRATE)
@ -964,7 +965,7 @@ struct CacheEncoder<'a, 'tcx, E: OpaqueEncoder> {
source_map: CachingSourceMapView<'tcx>, source_map: CachingSourceMapView<'tcx>,
file_to_file_index: FxHashMap<*const SourceFile, SourceFileIndex>, file_to_file_index: FxHashMap<*const SourceFile, SourceFileIndex>,
hygiene_context: &'a HygieneEncodeContext, hygiene_context: &'a HygieneEncodeContext,
latest_foreign_def_path_hashes: FxHashMap<DefPathHash, RawDefId>, latest_foreign_def_path_hashes: UnhashMap<DefPathHash, RawDefId>,
} }
impl<'a, 'tcx, E> CacheEncoder<'a, 'tcx, E> impl<'a, 'tcx, E> CacheEncoder<'a, 'tcx, E>