Skip to content

Commit 6f7ba5f

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 3de0a7c + faed860 commit 6f7ba5f

File tree

7 files changed

+71
-11
lines changed

7 files changed

+71
-11
lines changed

Cargo.lock

+27-2
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,12 @@ dependencies = [
237237
"object 0.35.0",
238238
]
239239

240+
[[package]]
241+
name = "arrayref"
242+
version = "0.3.7"
243+
source = "registry+https://github.com/rust-lang/crates.io-index"
244+
checksum = "6b4930d2cb77ce62f89ee5d5289b4ac049559b1c45539271f5ed4fdc7db34545"
245+
240246
[[package]]
241247
name = "arrayvec"
242248
version = "0.7.4"
@@ -356,6 +362,17 @@ version = "2.5.0"
356362
source = "registry+https://github.com/rust-lang/crates.io-index"
357363
checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1"
358364

365+
[[package]]
366+
name = "blake2s_simd"
367+
version = "1.0.2"
368+
source = "registry+https://github.com/rust-lang/crates.io-index"
369+
checksum = "94230421e395b9920d23df13ea5d77a20e1725331f90fbbf6df6040b33f756ae"
370+
dependencies = [
371+
"arrayref",
372+
"arrayvec",
373+
"constant_time_eq",
374+
]
375+
359376
[[package]]
360377
name = "block-buffer"
361378
version = "0.10.4"
@@ -842,6 +859,12 @@ dependencies = [
842859
"windows-sys 0.52.0",
843860
]
844861

862+
[[package]]
863+
name = "constant_time_eq"
864+
version = "0.3.0"
865+
source = "registry+https://github.com/rust-lang/crates.io-index"
866+
checksum = "f7144d30dcf0fafbce74250a3963025d8d52177934239851c917d29f1df280c2"
867+
845868
[[package]]
846869
name = "core"
847870
version = "0.0.0"
@@ -3544,8 +3567,10 @@ checksum = "5be1bdc7edf596692617627bbfeaba522131b18e06ca4df2b6b689e3c5d5ce84"
35443567
[[package]]
35453568
name = "rustc-stable-hash"
35463569
version = "0.1.0"
3547-
source = "registry+https://github.com/rust-lang/crates.io-index"
3548-
checksum = "e5c9f15eec8235d7cb775ee6f81891db79b98fd54ba1ad8fae565b88ef1ae4e2"
3570+
source = "git+https://github.com/Urgau/rustc-stable-hash.git?rev=4f8c5a9#4f8c5a993536388a22fba12d0be76837dcd52017"
3571+
dependencies = [
3572+
"blake2s_simd",
3573+
]
35493574

35503575
[[package]]
35513576
name = "rustc-std-workspace-alloc"

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 = "4f8c5a9", 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

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const LICENSES: &[&str] = &[
2121
"Apache-2.0",
2222
"Apache-2.0/MIT",
2323
"BSD-2-Clause OR Apache-2.0 OR MIT", // zerocopy
24+
"CC0-1.0 OR MIT-0 OR Apache-2.0", // contant_time_eq
2425
"ISC",
2526
"MIT / Apache-2.0",
2627
"MIT OR Apache-2.0 OR LGPL-2.1-or-later", // r-efi, r-efi-alloc
@@ -83,6 +84,7 @@ pub(crate) const WORKSPACES: &[(&str, ExceptionList, Option<(&[&str], &[&str])>)
8384
const EXCEPTIONS: ExceptionList = &[
8485
// tidy-alphabetical-start
8586
("ar_archive_writer", "Apache-2.0 WITH LLVM-exception"), // rustc
87+
("arrayref", "BSD-2-Clause"), // rustc
8688
("colored", "MPL-2.0"), // rustfmt
8789
("dissimilar", "Apache-2.0"), // rustdoc, rustc_lexer (few tests) via expect-test, (dev deps)
8890
("fluent-langneg", "Apache-2.0"), // rustc (fluent translations)
@@ -237,15 +239,18 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
237239
"annotate-snippets",
238240
"anstyle",
239241
"ar_archive_writer",
242+
"arrayref",
240243
"arrayvec",
241244
"autocfg",
242245
"bitflags",
246+
"blake2s_simd",
243247
"block-buffer",
244248
"byteorder", // via ruzstd in object in thorin-dwp
245249
"cc",
246250
"cfg-if",
247251
"cfg_aliases",
248252
"compiler_builtins",
253+
"constant_time_eq",
249254
"cpufeatures",
250255
"crc32fast",
251256
"crossbeam-channel",

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=4f8c5a9#4f8c5a993536388a22fba12d0be76837dcd52017""#,
1214
];
1315

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

0 commit comments

Comments
 (0)