Skip to content

Commit 1bd42be

Browse files
committed
Auto merge of rust-lang#120076 - Mark-Simulacrum:unhash, r=cjgillot
Use UnhashMap for a few more maps This avoids a few cases of hashing data that's already hashed. cc rust-lang#56308
2 parents d3c9082 + 510fcd3 commit 1bd42be

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed

compiler/rustc_data_structures/src/hashes.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,21 @@ impl fmt::LowerHex for Hash64 {
7575
}
7676
}
7777

78-
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
78+
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default)]
7979
pub struct Hash128 {
8080
inner: u128,
8181
}
8282

83+
// We expect Hash128 to be well mixed. So there's no point in hashing both parts.
84+
//
85+
// This also allows using Hash128-containing types in UnHash-based hashmaps, which would otherwise
86+
// debug_assert! that we're hashing more than a single u64.
87+
impl std::hash::Hash for Hash128 {
88+
fn hash<H: std::hash::Hasher>(&self, h: &mut H) {
89+
h.write_u64(self.truncate().as_u64());
90+
}
91+
}
92+
8393
impl Hash128 {
8494
#[inline]
8595
pub fn truncate(self) -> Hash64 {

compiler/rustc_data_structures/src/stable_hasher.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,19 @@ impl_stable_traits_for_trivial_type!(char);
314314
impl_stable_traits_for_trivial_type!(());
315315

316316
impl_stable_traits_for_trivial_type!(Hash64);
317-
impl_stable_traits_for_trivial_type!(Hash128);
317+
318+
// We need a custom impl as the default hash function will only hash half the bits. For stable
319+
// hashing we want to hash the full 128-bit hash.
320+
impl<CTX> HashStable<CTX> for Hash128 {
321+
#[inline]
322+
fn hash_stable(&self, _: &mut CTX, hasher: &mut StableHasher) {
323+
self.as_u128().hash(hasher);
324+
}
325+
}
326+
327+
unsafe impl StableOrd for Hash128 {
328+
const CAN_USE_UNSTABLE_SORT: bool = true;
329+
}
318330

319331
impl<CTX> HashStable<CTX> for ! {
320332
fn hash_stable(&self, _ctx: &mut CTX, _hasher: &mut StableHasher) {

compiler/rustc_span/src/hygiene.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ pub(crate) struct HygieneData {
330330
/// would have collisions without a disambiguator.
331331
/// The keys of this map are always computed with `ExpnData.disambiguator`
332332
/// set to 0.
333-
expn_data_disambiguators: FxHashMap<Hash64, u32>,
333+
expn_data_disambiguators: UnhashMap<Hash64, u32>,
334334
}
335335

336336
impl HygieneData {
@@ -359,7 +359,7 @@ impl HygieneData {
359359
dollar_crate_name: kw::DollarCrate,
360360
}],
361361
syntax_context_map: FxHashMap::default(),
362-
expn_data_disambiguators: FxHashMap::default(),
362+
expn_data_disambiguators: UnhashMap::default(),
363363
}
364364
}
365365

compiler/rustc_span/src/source_map.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
//! information, source code snippets, etc.
1111
1212
use crate::*;
13-
use rustc_data_structures::fx::FxHashMap;
1413
use rustc_data_structures::sync::{IntoDynSyncSend, MappedReadGuard, ReadGuard, RwLock};
14+
use rustc_data_structures::unhash::UnhashMap;
1515
use std::fs;
1616
use std::io::{self, BorrowedBuf, Read};
1717
use std::path::{self};
@@ -164,7 +164,7 @@ impl FileLoader for RealFileLoader {
164164
#[derive(Default)]
165165
struct SourceMapFiles {
166166
source_files: monotonic::MonotonicVec<Lrc<SourceFile>>,
167-
stable_id_to_source_file: FxHashMap<StableSourceFileId, Lrc<SourceFile>>,
167+
stable_id_to_source_file: UnhashMap<StableSourceFileId, Lrc<SourceFile>>,
168168
}
169169

170170
pub struct SourceMap {

0 commit comments

Comments
 (0)