Skip to content

Commit c2fdbf6

Browse files
committed
Auto merge of rust-lang#128597 - Urgau:blake2-type-id, r=<try>
[perf] Use Blake2 for the type-id hash and only for the `TypeId` hash, not as a general stable hasher. cc `@michaelwoerister`
2 parents 8fbdc04 + 41990be commit c2fdbf6

File tree

51 files changed

+348
-164
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+348
-164
lines changed

Cargo.lock

+20-2
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,15 @@ version = "2.5.0"
275275
source = "registry+https://github.com/rust-lang/crates.io-index"
276276
checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1"
277277

278+
[[package]]
279+
name = "blake2"
280+
version = "0.10.6"
281+
source = "registry+https://github.com/rust-lang/crates.io-index"
282+
checksum = "46502ad458c9a52b69d4d4d32775c788b7a1b85e8bc9d482d92250fc0e3f8efe"
283+
dependencies = [
284+
"digest",
285+
]
286+
278287
[[package]]
279288
name = "block-buffer"
280289
version = "0.10.4"
@@ -1017,6 +1026,7 @@ checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292"
10171026
dependencies = [
10181027
"block-buffer",
10191028
"crypto-common",
1029+
"subtle",
10201030
]
10211031

10221032
[[package]]
@@ -3227,8 +3237,10 @@ checksum = "5be1bdc7edf596692617627bbfeaba522131b18e06ca4df2b6b689e3c5d5ce84"
32273237
[[package]]
32283238
name = "rustc-stable-hash"
32293239
version = "0.1.0"
3230-
source = "registry+https://github.com/rust-lang/crates.io-index"
3231-
checksum = "e5c9f15eec8235d7cb775ee6f81891db79b98fd54ba1ad8fae565b88ef1ae4e2"
3240+
source = "git+https://github.com/Urgau/rustc-stable-hash.git?rev=543696a#543696a0b6299c4cc8a8c9c30651e5cc0056655a"
3241+
dependencies = [
3242+
"blake2",
3243+
]
32323244

32333245
[[package]]
32343246
name = "rustc_abi"
@@ -5067,6 +5079,12 @@ dependencies = [
50675079
"syn 1.0.109",
50685080
]
50695081

5082+
[[package]]
5083+
name = "subtle"
5084+
version = "2.6.1"
5085+
source = "registry+https://github.com/rust-lang/crates.io-index"
5086+
checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
5087+
50705088
[[package]]
50715089
name = "suggest-tests"
50725090
version = "0.1.0"

compiler/rustc_ast/src/ast.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use std::{cmp, fmt, mem};
2323

2424
pub use rustc_ast_ir::{Movability, Mutability};
2525
use rustc_data_structures::packed::Pu128;
26-
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
26+
use rustc_data_structures::stable_hasher::{ExtendedHasher, GenericStableHasher, HashStable};
2727
use rustc_data_structures::stack::ensure_sufficient_stack;
2828
use rustc_data_structures::sync::Lrc;
2929
use rustc_macros::{Decodable, Encodable, HashStable_Generic};
@@ -105,7 +105,7 @@ impl PartialEq<Symbol> for Path {
105105
}
106106

107107
impl<CTX: rustc_span::HashStableContext> HashStable<CTX> for Path {
108-
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
108+
fn hash_stable<H: ExtendedHasher>(&self, hcx: &mut CTX, hasher: &mut GenericStableHasher<H>) {
109109
self.segments.len().hash_stable(hcx, hasher);
110110
for segment in &self.segments {
111111
segment.ident.hash_stable(hcx, hasher);
@@ -1723,7 +1723,7 @@ impl<CTX> HashStable<CTX> for AttrArgs
17231723
where
17241724
CTX: crate::HashStableContext,
17251725
{
1726-
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
1726+
fn hash_stable<H: ExtendedHasher>(&self, ctx: &mut CTX, hasher: &mut GenericStableHasher<H>) {
17271727
mem::discriminant(self).hash_stable(ctx, hasher);
17281728
match self {
17291729
AttrArgs::Empty => {}
@@ -1759,7 +1759,7 @@ impl<CTX> HashStable<CTX> for DelimArgs
17591759
where
17601760
CTX: crate::HashStableContext,
17611761
{
1762-
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
1762+
fn hash_stable<H: ExtendedHasher>(&self, ctx: &mut CTX, hasher: &mut GenericStableHasher<H>) {
17631763
let DelimArgs { dspan, delim, tokens } = self;
17641764
dspan.hash_stable(ctx, hasher);
17651765
delim.hash_stable(ctx, hasher);

compiler/rustc_ast/src/lib.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub mod token;
4343
pub mod tokenstream;
4444
pub mod visit;
4545

46-
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
46+
use rustc_data_structures::stable_hasher::{ExtendedHasher, GenericStableHasher, HashStable};
4747

4848
pub use self::ast::*;
4949
pub use self::ast_traits::{AstDeref, AstNodeWrapper, HasAttrs, HasNodeId, HasTokens};
@@ -52,11 +52,19 @@ pub use self::ast_traits::{AstDeref, AstNodeWrapper, HasAttrs, HasNodeId, HasTok
5252
/// This is a hack to allow using the `HashStable_Generic` derive macro
5353
/// instead of implementing everything in `rustc_middle`.
5454
pub trait HashStableContext: rustc_span::HashStableContext {
55-
fn hash_attr(&mut self, _: &ast::Attribute, hasher: &mut StableHasher);
55+
fn hash_attr<H: ExtendedHasher>(
56+
&mut self,
57+
_: &ast::Attribute,
58+
hasher: &mut GenericStableHasher<H>,
59+
);
5660
}
5761

5862
impl<AstCtx: crate::HashStableContext> HashStable<AstCtx> for ast::Attribute {
59-
fn hash_stable(&self, hcx: &mut AstCtx, hasher: &mut StableHasher) {
63+
fn hash_stable<H: ExtendedHasher>(
64+
&self,
65+
hcx: &mut AstCtx,
66+
hasher: &mut GenericStableHasher<H>,
67+
) {
6068
hcx.hash_attr(self, hasher)
6169
}
6270
}

compiler/rustc_ast/src/ptr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use std::fmt::{self, Debug, Display};
2121
use std::ops::{Deref, DerefMut};
2222
use std::{slice, vec};
2323

24-
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
24+
use rustc_data_structures::stable_hasher::{ExtendedHasher, GenericStableHasher, HashStable};
2525
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
2626
/// An owned smart pointer.
2727
///
@@ -203,7 +203,7 @@ impl<CTX, T> HashStable<CTX> for P<T>
203203
where
204204
T: ?Sized + HashStable<CTX>,
205205
{
206-
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
206+
fn hash_stable<H: ExtendedHasher>(&self, hcx: &mut CTX, hasher: &mut GenericStableHasher<H>) {
207207
(**self).hash_stable(hcx, hasher);
208208
}
209209
}

compiler/rustc_ast/src/token.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::borrow::Cow;
22
use std::fmt;
33

4-
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
4+
use rustc_data_structures::stable_hasher::{ExtendedHasher, GenericStableHasher, HashStable};
55
use rustc_data_structures::sync::Lrc;
66
use rustc_macros::{Decodable, Encodable, HashStable_Generic};
77
use rustc_span::edition::Edition;
@@ -1055,7 +1055,7 @@ impl<CTX> HashStable<CTX> for Nonterminal
10551055
where
10561056
CTX: crate::HashStableContext,
10571057
{
1058-
fn hash_stable(&self, _hcx: &mut CTX, _hasher: &mut StableHasher) {
1058+
fn hash_stable<H: ExtendedHasher>(&self, _hcx: &mut CTX, _hasher: &mut GenericStableHasher<H>) {
10591059
panic!("interpolated tokens should not be present in the HIR")
10601060
}
10611061
}

compiler/rustc_ast/src/tokenstream.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use std::borrow::Cow;
1717
use std::{cmp, fmt, iter};
1818

19-
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
19+
use rustc_data_structures::stable_hasher::{ExtendedHasher, GenericStableHasher, HashStable};
2020
use rustc_data_structures::sync::{self, Lrc};
2121
use rustc_macros::{Decodable, Encodable, HashStable_Generic};
2222
use rustc_serialize::{Decodable, Encodable};
@@ -99,7 +99,7 @@ impl<CTX> HashStable<CTX> for TokenStream
9999
where
100100
CTX: crate::HashStableContext,
101101
{
102-
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
102+
fn hash_stable<H: ExtendedHasher>(&self, hcx: &mut CTX, hasher: &mut GenericStableHasher<H>) {
103103
for sub_tt in self.trees() {
104104
sub_tt.hash_stable(hcx, hasher);
105105
}
@@ -151,7 +151,7 @@ impl<D: SpanDecoder> Decodable<D> for LazyAttrTokenStream {
151151
}
152152

153153
impl<CTX> HashStable<CTX> for LazyAttrTokenStream {
154-
fn hash_stable(&self, _hcx: &mut CTX, _hasher: &mut StableHasher) {
154+
fn hash_stable<H: ExtendedHasher>(&self, _hcx: &mut CTX, _hasher: &mut GenericStableHasher<H>) {
155155
panic!("Attempted to compute stable hash for LazyAttrTokenStream");
156156
}
157157
}

compiler/rustc_codegen_cranelift/src/driver/aot.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_codegen_ssa::{
1515
errors as ssa_errors, CodegenResults, CompiledModule, CrateInfo, ModuleKind,
1616
};
1717
use rustc_data_structures::profiling::SelfProfilerRef;
18-
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
18+
use rustc_data_structures::stable_hasher::{ExtendedHasher, GenericStableHasher, HashStable};
1919
use rustc_data_structures::sync::{par_map, IntoDynSyncSend};
2020
use rustc_metadata::fs::copy_to_stdout;
2121
use rustc_metadata::EncodedMetadata;
@@ -43,7 +43,7 @@ enum OngoingModuleCodegen {
4343
}
4444

4545
impl<HCX> HashStable<HCX> for OngoingModuleCodegen {
46-
fn hash_stable(&self, _: &mut HCX, _: &mut StableHasher) {
46+
fn hash_stable<H: ExtendedHasher>(&self, _: &mut HCX, _: &mut GenericStableHasher<H>) {
4747
// do nothing
4848
}
4949
}

compiler/rustc_codegen_llvm/src/debuginfo/metadata/type_map.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::cell::RefCell;
22

33
use rustc_data_structures::fingerprint::Fingerprint;
44
use rustc_data_structures::fx::FxHashMap;
5-
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
5+
use rustc_data_structures::stable_hasher::{HashStable, StableBlake2sHasher256};
66
use rustc_macros::HashStable;
77
use rustc_middle::bug;
88
use rustc_middle::ty::{ParamEnv, PolyExistentialTraitRef, Ty, TyCtxt};
@@ -94,7 +94,7 @@ impl<'tcx> UniqueTypeId<'tcx> {
9494
///
9595
/// Right now this takes the form of a hex-encoded opaque hash value.
9696
pub fn generate_unique_id_string(self, tcx: TyCtxt<'tcx>) -> String {
97-
let mut hasher = StableHasher::new();
97+
let mut hasher = StableBlake2sHasher256::new();
9898
tcx.with_stable_hashing_context(|mut hcx| {
9999
hcx.while_hashing_spans(false, |hcx| self.hash_stable(hcx, &mut hasher))
100100
});

compiler/rustc_codegen_ssa/src/common.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,12 @@ pub enum TypeKind {
107107
// for now we content ourselves with providing a no-op HashStable
108108
// implementation for CGUs.
109109
mod temp_stable_hash_impls {
110-
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
110+
use rustc_data_structures::stable_hasher::{ExtendedHasher, GenericStableHasher, HashStable};
111111

112112
use crate::ModuleCodegen;
113113

114114
impl<HCX, M> HashStable<HCX> for ModuleCodegen<M> {
115-
fn hash_stable(&self, _: &mut HCX, _: &mut StableHasher) {
115+
fn hash_stable<H: ExtendedHasher>(&self, _: &mut HCX, _: &mut GenericStableHasher<H>) {
116116
// do nothing
117117
}
118118
}

compiler/rustc_data_structures/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobserver_crate = { version = "0.1.28", package = "jobserver" }
1515
measureme = "11"
1616
rustc-hash = "1.1.0"
1717
rustc-rayon = { version = "0.5.0", optional = true }
18-
rustc-stable-hash = { version = "0.1.0", features = ["nightly"] }
18+
rustc-stable-hash = { git = "https://github.com/Urgau/rustc-stable-hash.git", rev = "543696a", features = ["nightly"] }
1919
rustc_arena = { path = "../rustc_arena" }
2020
rustc_graphviz = { path = "../rustc_graphviz" }
2121
rustc_index = { path = "../rustc_index", package = "rustc_index" }

compiler/rustc_data_structures/src/fingerprint.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::hash::{Hash, Hasher};
22

33
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
4+
use rustc_stable_hash::hashers::Blake2s256Hash;
45

56
use crate::stable_hasher::{
67
impl_stable_traits_for_trivial_type, FromStableHash, Hash64, StableHasherHash,
@@ -157,15 +158,27 @@ impl FingerprintHasher for crate::unhash::Unhasher {
157158
}
158159
}
159160

160-
impl FromStableHash for Fingerprint {
161-
type Hash = StableHasherHash;
162-
161+
impl FromStableHash<StableHasherHash> for Fingerprint {
163162
#[inline]
164-
fn from(StableHasherHash([_0, _1]): Self::Hash) -> Self {
163+
fn from(StableHasherHash([_0, _1]): StableHasherHash) -> Self {
165164
Fingerprint(_0, _1)
166165
}
167166
}
168167

168+
impl FromStableHash<Blake2s256Hash> for Fingerprint {
169+
#[inline]
170+
fn from(Blake2s256Hash(bytes): Blake2s256Hash) -> Self {
171+
let p0 = u64::from_le_bytes(bytes[0..8].try_into().unwrap());
172+
let p1 = u64::from_le_bytes(bytes[8..16].try_into().unwrap());
173+
let p2 = u64::from_le_bytes(bytes[16..24].try_into().unwrap());
174+
let p3 = u64::from_le_bytes(bytes[24..32].try_into().unwrap());
175+
176+
// See https://stackoverflow.com/a/27952689 on why this function is
177+
// implemented this way.
178+
Fingerprint(p0.wrapping_mul(3).wrapping_add(p1), p2.wrapping_mul(3).wrapping_add(p3))
179+
}
180+
}
181+
169182
impl_stable_traits_for_trivial_type!(Fingerprint);
170183

171184
impl<E: Encoder> Encodable<E> for Fingerprint {

compiler/rustc_data_structures/src/hashes.rs

+17-8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use std::fmt;
1515
use std::ops::BitXorAssign;
1616

1717
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
18+
use rustc_stable_hash::hashers::Blake2s256Hash;
1819

1920
use crate::stable_hasher::{FromStableHash, StableHasherHash};
2021

@@ -58,11 +59,9 @@ impl<D: Decoder> Decodable<D> for Hash64 {
5859
}
5960
}
6061

61-
impl FromStableHash for Hash64 {
62-
type Hash = StableHasherHash;
63-
62+
impl FromStableHash<StableHasherHash> for Hash64 {
6463
#[inline]
65-
fn from(StableHasherHash([_0, __1]): Self::Hash) -> Self {
64+
fn from(StableHasherHash([_0, __1]): StableHasherHash) -> Self {
6665
Self { inner: _0 }
6766
}
6867
}
@@ -125,15 +124,25 @@ impl<D: Decoder> Decodable<D> for Hash128 {
125124
}
126125
}
127126

128-
impl FromStableHash for Hash128 {
129-
type Hash = StableHasherHash;
130-
127+
impl FromStableHash<StableHasherHash> for Hash128 {
131128
#[inline]
132-
fn from(StableHasherHash([_0, _1]): Self::Hash) -> Self {
129+
fn from(StableHasherHash([_0, _1]): StableHasherHash) -> Self {
133130
Self { inner: u128::from(_0) | (u128::from(_1) << 64) }
134131
}
135132
}
136133

134+
impl FromStableHash<Blake2s256Hash> for Hash128 {
135+
#[inline]
136+
fn from(Blake2s256Hash(bytes): Blake2s256Hash) -> Self {
137+
let p0 = u128::from_le_bytes(bytes[0..16].try_into().unwrap());
138+
let p1 = u128::from_le_bytes(bytes[16..32].try_into().unwrap());
139+
140+
// See https://stackoverflow.com/a/27952689 on why this function is
141+
// implemented this way.
142+
Self { inner: p0.wrapping_mul(3).wrapping_add(p1) }
143+
}
144+
}
145+
137146
impl fmt::Debug for Hash128 {
138147
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
139148
self.inner.fmt(f)

compiler/rustc_data_structures/src/intern.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::hash::{Hash, Hasher};
44
use std::ops::Deref;
55
use std::ptr;
66

7-
use crate::stable_hasher::{HashStable, StableHasher};
7+
use crate::stable_hasher::{ExtendedHasher, GenericStableHasher, HashStable};
88

99
mod private {
1010
#[derive(Clone, Copy, Debug)]
@@ -104,7 +104,7 @@ impl<T, CTX> HashStable<CTX> for Interned<'_, T>
104104
where
105105
T: HashStable<CTX>,
106106
{
107-
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
107+
fn hash_stable<H: ExtendedHasher>(&self, hcx: &mut CTX, hasher: &mut GenericStableHasher<H>) {
108108
self.0.hash_stable(hcx, hasher);
109109
}
110110
}

compiler/rustc_data_structures/src/packed.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::fmt;
33

44
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
55

6-
use crate::stable_hasher::{HashStable, StableHasher};
6+
use crate::stable_hasher::{ExtendedHasher, GenericStableHasher, HashStable};
77

88
/// A packed 128-bit integer. Useful for reducing the size of structures in
99
/// some cases.
@@ -55,7 +55,7 @@ impl fmt::UpperHex for Pu128 {
5555

5656
impl<CTX> HashStable<CTX> for Pu128 {
5757
#[inline]
58-
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
58+
fn hash_stable<H: ExtendedHasher>(&self, ctx: &mut CTX, hasher: &mut GenericStableHasher<H>) {
5959
{ self.0 }.hash_stable(ctx, hasher)
6060
}
6161
}

compiler/rustc_data_structures/src/sorted_map.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::ops::{Bound, Index, IndexMut, RangeBounds};
55

66
use rustc_macros::{Decodable_Generic, Encodable_Generic};
77

8-
use crate::stable_hasher::{HashStable, StableHasher, StableOrd};
8+
use crate::stable_hasher::{ExtendedHasher, GenericStableHasher, HashStable, StableOrd};
99

1010
mod index_map;
1111

@@ -311,7 +311,7 @@ impl<K: Ord, V> FromIterator<(K, V)> for SortedMap<K, V> {
311311

312312
impl<K: HashStable<CTX> + StableOrd, V: HashStable<CTX>, CTX> HashStable<CTX> for SortedMap<K, V> {
313313
#[inline]
314-
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
314+
fn hash_stable<H: ExtendedHasher>(&self, ctx: &mut CTX, hasher: &mut GenericStableHasher<H>) {
315315
self.data.hash_stable(ctx, hasher);
316316
}
317317
}

0 commit comments

Comments
 (0)