Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[perf] Change stable hasher to Blake2s #127809

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,12 @@ dependencies = [
"object 0.32.2",
]

[[package]]
name = "arrayref"
version = "0.3.7"
source = "registry+https:/rust-lang/crates.io-index"
checksum = "6b4930d2cb77ce62f89ee5d5289b4ac049559b1c45539271f5ed4fdc7db34545"

[[package]]
name = "arrayvec"
version = "0.7.4"
Expand Down Expand Up @@ -362,6 +368,17 @@ version = "2.5.0"
source = "registry+https:/rust-lang/crates.io-index"
checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1"

[[package]]
name = "blake2s_simd"
version = "1.0.2"
source = "registry+https:/rust-lang/crates.io-index"
checksum = "94230421e395b9920d23df13ea5d77a20e1725331f90fbbf6df6040b33f756ae"
dependencies = [
"arrayref",
"arrayvec",
"constant_time_eq",
]

[[package]]
name = "block-buffer"
version = "0.10.4"
Expand Down Expand Up @@ -848,6 +865,12 @@ dependencies = [
"windows-sys 0.52.0",
]

[[package]]
name = "constant_time_eq"
version = "0.3.0"
source = "registry+https:/rust-lang/crates.io-index"
checksum = "f7144d30dcf0fafbce74250a3963025d8d52177934239851c917d29f1df280c2"

[[package]]
name = "core"
version = "0.0.0"
Expand Down Expand Up @@ -3518,8 +3541,10 @@ checksum = "5be1bdc7edf596692617627bbfeaba522131b18e06ca4df2b6b689e3c5d5ce84"
[[package]]
name = "rustc-stable-hash"
version = "0.1.0"
source = "registry+https:/rust-lang/crates.io-index"
checksum = "e5c9f15eec8235d7cb775ee6f81891db79b98fd54ba1ad8fae565b88ef1ae4e2"
source = "git+https:/Urgau/rustc-stable-hash.git?rev=4f8c5a9#4f8c5a993536388a22fba12d0be76837dcd52017"
dependencies = [
"blake2s_simd",
]

[[package]]
name = "rustc-std-workspace-alloc"
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_data_structures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobserver_crate = { version = "0.1.28", package = "jobserver" }
measureme = "11"
rustc-hash = "1.1.0"
rustc-rayon = { version = "0.5.0", optional = true }
rustc-stable-hash = { version = "0.1.0", features = ["nightly"] }
rustc-stable-hash = { git = "https:/Urgau/rustc-stable-hash.git", rev = "4f8c5a9", features = ["blake2", "nightly"] }
rustc_arena = { path = "../rustc_arena" }
rustc_graphviz = { path = "../rustc_graphviz" }
rustc_index = { path = "../rustc_index", package = "rustc_index" }
Expand Down
11 changes: 9 additions & 2 deletions compiler/rustc_data_structures/src/fingerprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,15 @@ impl FromStableHash for Fingerprint {
type Hash = StableHasherHash;

#[inline]
fn from(StableHasherHash([_0, _1]): Self::Hash) -> Self {
Fingerprint(_0, _1)
fn from(StableHasherHash(bytes): Self::Hash) -> Self {
let p0 = u64::from_le_bytes(bytes[0..8].try_into().unwrap());
let p1 = u64::from_le_bytes(bytes[8..16].try_into().unwrap());
let p2 = u64::from_le_bytes(bytes[16..24].try_into().unwrap());
let p3 = u64::from_le_bytes(bytes[24..32].try_into().unwrap());

// See https://stackoverflow.com/a/27952689 on why this function is
// implemented this way.
Fingerprint(p0.wrapping_mul(3).wrapping_add(p1), p2.wrapping_mul(3).wrapping_add(p3))
}
}

Expand Down
29 changes: 25 additions & 4 deletions compiler/rustc_data_structures/src/hashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,19 @@ impl FromStableHash for Hash64 {
type Hash = StableHasherHash;

#[inline]
fn from(StableHasherHash([_0, __1]): Self::Hash) -> Self {
Self { inner: _0 }
fn from(StableHasherHash(bytes): Self::Hash) -> Self {
let p0 = u64::from_le_bytes(bytes[0..8].try_into().unwrap());
let p1 = u64::from_le_bytes(bytes[8..16].try_into().unwrap());
let p2 = u64::from_le_bytes(bytes[16..24].try_into().unwrap());
let p3 = u64::from_le_bytes(bytes[24..32].try_into().unwrap());

// See https://stackoverflow.com/a/27952689 on why this function is
// implemented this way.
let m0 = p0.wrapping_mul(3).wrapping_add(p1);
let m1 = p2.wrapping_mul(3).wrapping_add(p3);
let h = m0.wrapping_mul(3).wrapping_add(m1);

Self { inner: h }
}
}

Expand Down Expand Up @@ -127,8 +138,18 @@ impl FromStableHash for Hash128 {
type Hash = StableHasherHash;

#[inline]
fn from(StableHasherHash([_0, _1]): Self::Hash) -> Self {
Self { inner: u128::from(_0) | (u128::from(_1) << 64) }
fn from(StableHasherHash(bytes): Self::Hash) -> Self {
let p0 = u64::from_le_bytes(bytes[0..8].try_into().unwrap());
let p1 = u64::from_le_bytes(bytes[8..16].try_into().unwrap());
let p2 = u64::from_le_bytes(bytes[16..24].try_into().unwrap());
let p3 = u64::from_le_bytes(bytes[24..32].try_into().unwrap());

// See https://stackoverflow.com/a/27952689 on why this function is
// implemented this way.
let upper = p0.wrapping_mul(3).wrapping_add(p1);
let lower = p2.wrapping_mul(3).wrapping_add(p3);

Self { inner: u128::from(lower) | (u128::from(upper) << 64) }
}
}

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_data_structures/src/stable_hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ mod tests;

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

pub use rustc_stable_hash::hashers::Blake2s256Hash as StableHasherHash;
pub use rustc_stable_hash::hashers::StableBlake2sHasher256 as StableHasher;
pub use rustc_stable_hash::FromStableHash;
pub use rustc_stable_hash::SipHasher128Hash as StableHasherHash;
pub use rustc_stable_hash::StableSipHasher128 as StableHasher;

/// Something that implements `HashStable<CTX>` can be hashed in a way that is
/// stable across multiple compilation sessions.
Expand Down
5 changes: 5 additions & 0 deletions src/tools/tidy/src/deps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const LICENSES: &[&str] = &[
"Apache-2.0",
"Apache-2.0/MIT",
"BSD-2-Clause OR Apache-2.0 OR MIT", // zerocopy
"CC0-1.0 OR MIT-0 OR Apache-2.0", // contant_time_eq
"ISC",
"MIT / Apache-2.0",
"MIT OR Apache-2.0 OR LGPL-2.1-or-later", // r-efi, r-efi-alloc
Expand Down Expand Up @@ -83,6 +84,7 @@ pub(crate) const WORKSPACES: &[(&str, ExceptionList, Option<(&[&str], &[&str])>)
const EXCEPTIONS: ExceptionList = &[
// tidy-alphabetical-start
("ar_archive_writer", "Apache-2.0 WITH LLVM-exception"), // rustc
("arrayref", "BSD-2-Clause"), // rustc
("colored", "MPL-2.0"), // rustfmt
("dissimilar", "Apache-2.0"), // rustdoc, rustc_lexer (few tests) via expect-test, (dev deps)
("fluent-langneg", "Apache-2.0"), // rustc (fluent translations)
Expand Down Expand Up @@ -232,15 +234,18 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
"annotate-snippets",
"anstyle",
"ar_archive_writer",
"arrayref",
"arrayvec",
"autocfg",
"bitflags",
"blake2s_simd",
"block-buffer",
"byteorder", // via ruzstd in object in thorin-dwp
"cc",
"cfg-if",
"cfg_aliases",
"compiler_builtins",
"constant_time_eq",
"cpufeatures",
"crc32fast",
"crossbeam-channel",
Expand Down
2 changes: 2 additions & 0 deletions src/tools/tidy/src/extdeps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const ALLOWED_SOURCES: &[&str] = &[
r#""registry+https:/rust-lang/crates.io-index""#,
// This is `rust_team_data` used by `site` in src/tools/rustc-perf,
r#""git+https:/rust-lang/team#a5260e76d3aa894c64c56e6ddc8545b9a98043ec""#,
// WIP Blake2 in rustc-stable-hash
r#""git+https:/Urgau/rustc-stable-hash.git?rev=4f8c5a9#4f8c5a993536388a22fba12d0be76837dcd52017""#,
];

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