Skip to content

Commit 4172c7c

Browse files
committed
Auto merge of rust-lang#127809 - Urgau:blake2-stable-hash, r=<try>
[perf] Change stable hasher to Blake2s Based on rust-lang/rustc-stable-hash@main...Urgau:rustc-stable-hash:blake2 With inputs from https://jszym.com/blog/short_input_hash/ and rust-lang#127754. cc `@michaelwoerister` `@Kobzol` r? ghost
2 parents 5572759 + 9b14ce5 commit 4172c7c

File tree

7 files changed

+62
-11
lines changed

7 files changed

+62
-11
lines changed

Cargo.lock

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

365+
[[package]]
366+
name = "blake2"
367+
version = "0.10.6"
368+
source = "registry+https://github.com/rust-lang/crates.io-index"
369+
checksum = "46502ad458c9a52b69d4d4d32775c788b7a1b85e8bc9d482d92250fc0e3f8efe"
370+
dependencies = [
371+
"digest",
372+
]
373+
365374
[[package]]
366375
name = "block-buffer"
367376
version = "0.10.4"
@@ -1131,6 +1140,7 @@ checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292"
11311140
dependencies = [
11321141
"block-buffer",
11331142
"crypto-common",
1143+
"subtle",
11341144
]
11351145

11361146
[[package]]
@@ -3518,8 +3528,10 @@ checksum = "5be1bdc7edf596692617627bbfeaba522131b18e06ca4df2b6b689e3c5d5ce84"
35183528
[[package]]
35193529
name = "rustc-stable-hash"
35203530
version = "0.1.0"
3521-
source = "registry+https://github.com/rust-lang/crates.io-index"
3522-
checksum = "e5c9f15eec8235d7cb775ee6f81891db79b98fd54ba1ad8fae565b88ef1ae4e2"
3531+
source = "git+https://github.com/Urgau/rustc-stable-hash.git?rev=5dbe93b#5dbe93b5959372a64f6f7ea60382fe35be55dcb3"
3532+
dependencies = [
3533+
"blake2",
3534+
]
35233535

35243536
[[package]]
35253537
name = "rustc-std-workspace-alloc"
@@ -5408,6 +5420,12 @@ dependencies = [
54085420
"syn 1.0.109",
54095421
]
54105422

5423+
[[package]]
5424+
name = "subtle"
5425+
version = "2.6.1"
5426+
source = "registry+https://github.com/rust-lang/crates.io-index"
5427+
checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
5428+
54115429
[[package]]
54125430
name = "suggest-tests"
54135431
version = "0.1.0"

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 = "5dbe93b", features = ["blake2", "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

+9-2
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,15 @@ impl FromStableHash for Fingerprint {
158158
type Hash = StableHasherHash;
159159

160160
#[inline]
161-
fn from(StableHasherHash([_0, _1]): Self::Hash) -> Self {
162-
Fingerprint(_0, _1)
161+
fn from(StableHasherHash(bytes): Self::Hash) -> Self {
162+
let p0 = u64::from_le_bytes(bytes[0..8].try_into().unwrap());
163+
let p1 = u64::from_le_bytes(bytes[8..16].try_into().unwrap());
164+
let p2 = u64::from_le_bytes(bytes[16..24].try_into().unwrap());
165+
let p3 = u64::from_le_bytes(bytes[24..32].try_into().unwrap());
166+
167+
// See https://stackoverflow.com/a/27952689 on why this function is
168+
// implemented this way.
169+
Fingerprint(p0.wrapping_mul(3).wrapping_add(p1), p2.wrapping_mul(3).wrapping_add(p3))
163170
}
164171
}
165172

compiler/rustc_data_structures/src/hashes.rs

+25-4
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,19 @@ impl FromStableHash for Hash64 {
6060
type Hash = StableHasherHash;
6161

6262
#[inline]
63-
fn from(StableHasherHash([_0, __1]): Self::Hash) -> Self {
64-
Self { inner: _0 }
63+
fn from(StableHasherHash(bytes): Self::Hash) -> Self {
64+
let p0 = u64::from_le_bytes(bytes[0..8].try_into().unwrap());
65+
let p1 = u64::from_le_bytes(bytes[8..16].try_into().unwrap());
66+
let p2 = u64::from_le_bytes(bytes[16..24].try_into().unwrap());
67+
let p3 = u64::from_le_bytes(bytes[24..32].try_into().unwrap());
68+
69+
// See https://stackoverflow.com/a/27952689 on why this function is
70+
// implemented this way.
71+
let m0 = p0.wrapping_mul(3).wrapping_add(p1);
72+
let m1 = p2.wrapping_mul(3).wrapping_add(p3);
73+
let h = m0.wrapping_mul(3).wrapping_add(m1);
74+
75+
Self { inner: h }
6576
}
6677
}
6778

@@ -127,8 +138,18 @@ impl FromStableHash for Hash128 {
127138
type Hash = StableHasherHash;
128139

129140
#[inline]
130-
fn from(StableHasherHash([_0, _1]): Self::Hash) -> Self {
131-
Self { inner: u128::from(_0) | (u128::from(_1) << 64) }
141+
fn from(StableHasherHash(bytes): Self::Hash) -> Self {
142+
let p0 = u64::from_le_bytes(bytes[0..8].try_into().unwrap());
143+
let p1 = u64::from_le_bytes(bytes[8..16].try_into().unwrap());
144+
let p2 = u64::from_le_bytes(bytes[16..24].try_into().unwrap());
145+
let p3 = u64::from_le_bytes(bytes[24..32].try_into().unwrap());
146+
147+
// See https://stackoverflow.com/a/27952689 on why this function is
148+
// implemented this way.
149+
let upper = p0.wrapping_mul(3).wrapping_add(p1);
150+
let lower = p2.wrapping_mul(3).wrapping_add(p3);
151+
152+
Self { inner: u128::from(lower) | (u128::from(upper) << 64) }
132153
}
133154
}
134155

compiler/rustc_data_structures/src/stable_hasher.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ mod tests;
1111

1212
pub use crate::hashes::{Hash128, Hash64};
1313

14+
pub use rustc_stable_hash::hashers::Blake2s256Hash as StableHasherHash;
15+
pub use rustc_stable_hash::hashers::StableBlake2sHasher256 as StableHasher;
1416
pub use rustc_stable_hash::FromStableHash;
15-
pub use rustc_stable_hash::SipHasher128Hash as StableHasherHash;
16-
pub use rustc_stable_hash::StableSipHasher128 as StableHasher;
1717

1818
/// Something that implements `HashStable<CTX>` can be hashed in a way that is
1919
/// stable across multiple compilation sessions.

src/tools/tidy/src/deps.rs

+3
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ const EXCEPTIONS: ExceptionList = &[
9494
("ryu", "Apache-2.0 OR BSL-1.0"), // BSL is not acceptble, but we use it under Apache-2.0 // cargo/... (because of serde)
9595
("self_cell", "Apache-2.0"), // rustc (fluent translations)
9696
("snap", "BSD-3-Clause"), // rustc
97+
("subtle", "BSD-3-Clause"), // rustc blake2 deps
9798
("wasm-encoder", "Apache-2.0 WITH LLVM-exception"), // rustc
9899
("wasmparser", "Apache-2.0 WITH LLVM-exception"), // rustc
99100
// tidy-alphabetical-end
@@ -235,6 +236,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
235236
"arrayvec",
236237
"autocfg",
237238
"bitflags",
239+
"blake2",
238240
"block-buffer",
239241
"byteorder", // via ruzstd in object in thorin-dwp
240242
"cc",
@@ -376,6 +378,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
376378
"stacker",
377379
"static_assertions",
378380
"strsim",
381+
"subtle",
379382
"syn",
380383
"synstructure",
381384
"tempfile",

src/tools/tidy/src/extdeps.rs

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const ALLOWED_SOURCES: &[&str] = &[
99
r#""registry+https://github.com/rust-lang/crates.io-index""#,
1010
// This is `rust_team_data` used by `site` in src/tools/rustc-perf,
1111
r#""git+https://github.com/rust-lang/team#a5260e76d3aa894c64c56e6ddc8545b9a98043ec""#,
12+
// WIP Blake2 in rustc-stable-hash
13+
r#""git+https://github.com/Urgau/rustc-stable-hash.git?rev=5dbe93b#5dbe93b5959372a64f6f7ea60382fe35be55dcb3""#,
1214
];
1315

1416
/// Checks for external package sources. `root` is the path to the directory that contains the

0 commit comments

Comments
 (0)