1
Fork 0

Auto merge of #119192 - michaelwoerister:mcp533-push, r=cjgillot

Replace a number of FxHashMaps/Sets with stable-iteration-order alternatives

This PR replaces almost all of the remaining `FxHashMap`s in query results with either `FxIndexMap` or `UnordMap`. The only case that is missing is the `EffectiveVisibilities` struct which turned out to not be straightforward to transform. Once that is done too, we can remove the `HashStable` implementation from `HashMap`.

The first commit adds the `StableCompare` trait which is a companion trait to `StableOrd`. Some types like `Symbol` can be compared in a cross-session stable way, but their `Ord` implementation is not stable. In such cases, a `StableCompare` implementation can be provided to offer a lightweight way for stable sorting. The more heavyweight option is to sort via `ToStableHashKey`, but then sorting needs to have access to a stable hashing context and `ToStableHashKey` can also be expensive as in the case of `Symbol` where it has to allocate a `String`.

The rest of the commits are rather mechanical and don't overlap, so they are best reviewed individually.

Part of [MCP 533](https://github.com/rust-lang/compiler-team/issues/533).
This commit is contained in:
bors 2024-01-05 19:38:27 +00:00
commit b8c207435c
30 changed files with 256 additions and 147 deletions

View file

@ -1,6 +1,8 @@
use crate::{HashStableContext, Symbol};
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::stable_hasher::{Hash64, HashStable, StableHasher, ToStableHashKey};
use rustc_data_structures::stable_hasher::{
Hash64, HashStable, StableHasher, StableOrd, ToStableHashKey,
};
use rustc_data_structures::unhash::Unhasher;
use rustc_data_structures::AtomicRef;
use rustc_index::Idx;
@ -132,6 +134,11 @@ impl Default for DefPathHash {
}
}
// Safety: `DefPathHash` sort order is not affected (de)serialization.
unsafe impl StableOrd for DefPathHash {
const CAN_USE_UNSTABLE_SORT: bool = true;
}
/// A [`StableCrateId`] is a 64-bit hash of a crate name, together with all
/// `-Cmetadata` arguments, and some other data. It is to [`CrateNum`] what [`DefPathHash`] is to
/// [`DefId`]. It is stable across compilation sessions.
@ -490,6 +497,15 @@ impl<CTX: HashStableContext> ToStableHashKey<CTX> for CrateNum {
}
}
impl<CTX: HashStableContext> ToStableHashKey<CTX> for DefPathHash {
type KeyType = DefPathHash;
#[inline]
fn to_stable_hash_key(&self, _: &CTX) -> DefPathHash {
*self
}
}
macro_rules! typed_def_id {
($Name:ident, $LocalName:ident) => {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Encodable, Decodable, HashStable_Generic)]

View file

@ -4,7 +4,9 @@
use rustc_arena::DroplessArena;
use rustc_data_structures::fx::FxIndexSet;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
use rustc_data_structures::stable_hasher::{
HashStable, StableCompare, StableHasher, ToStableHashKey,
};
use rustc_data_structures::sync::Lock;
use rustc_macros::HashStable_Generic;
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
@ -2104,6 +2106,14 @@ impl<CTX> ToStableHashKey<CTX> for Symbol {
}
}
impl StableCompare for Symbol {
const CAN_USE_UNSTABLE_SORT: bool = true;
fn stable_cmp(&self, other: &Self) -> std::cmp::Ordering {
self.as_str().cmp(other.as_str())
}
}
pub(crate) struct Interner(Lock<InternerInner>);
// The `&'static str`s in this type actually point into the arena.