Skip to content

Commit

Permalink
update to use blake3 instead of xxhash per rustc MCP resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
Xaeroxe committed Jul 13, 2024
1 parent 1bf6fb9 commit 97ae01a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 31 deletions.
44 changes: 32 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ anstream = "0.6.13"
anstyle = "1.0.6"
anyhow = "1.0.82"
base64 = "0.22.1"
blake3 = "1.5.2"
bytesize = "1.3"
cargo = { path = "" }
cargo-credential = { version = "0.4.2", path = "credential/cargo-credential" }
Expand Down Expand Up @@ -101,7 +102,6 @@ toml_edit = { version = "0.22.14", features = ["serde"] }
tracing = { version = "0.1.40", default-features = false, features = ["std"] } # be compatible with rustc_log: https://github.com/rust-lang/rust/blob/e51e98dde6a/compiler/rustc_log/Cargo.toml#L9
tracing-chrome = "0.7.2"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
twox-hash = "1.6.3"
unicase = "2.7.0"
unicode-width = "0.1.12"
unicode-xid = "0.2.4"
Expand Down Expand Up @@ -147,6 +147,7 @@ anstream.workspace = true
anstyle.workspace = true
anyhow.workspace = true
base64.workspace = true
blake3.workspace = true
bytesize.workspace = true
cargo-credential.workspace = true
cargo-platform.workspace = true
Expand Down Expand Up @@ -200,7 +201,6 @@ toml.workspace = true
toml_edit.workspace = true
tracing = { workspace = true, features = ["attributes"] }
tracing-subscriber.workspace = true
twox-hash.workspace = true
unicase.workspace = true
unicode-width.workspace = true
url.workspace = true
Expand Down
28 changes: 12 additions & 16 deletions src/cargo/core/compiler/fingerprint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,6 @@ use serde::de;
use serde::ser;
use serde::{Deserialize, Serialize};
use tracing::{debug, info};
use twox_hash::XxHash64;

use crate::core::compiler::unit_graph::UnitDep;
use crate::core::Package;
Expand Down Expand Up @@ -2516,14 +2515,13 @@ pub fn parse_rustc_dep_info(rustc_dep_info: &Path) -> CargoResult<RustcDepInfo>
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum ChecksumAlgo {
Sha256,
XxHash,
Blake3,
}

impl ChecksumAlgo {
fn hash_len(&self) -> usize {
match self {
ChecksumAlgo::Sha256 => 32,
ChecksumAlgo::XxHash => 8,
ChecksumAlgo::Sha256 | ChecksumAlgo::Blake3 => 32,
}
}
}
Expand All @@ -2534,7 +2532,7 @@ impl FromStr for ChecksumAlgo {
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"sha256" => Ok(Self::Sha256),
"xxhash" => Ok(Self::XxHash),
"blake3" => Ok(Self::Blake3),
_ => Err(InvalidChecksumAlgo {}),
}
}
Expand All @@ -2544,7 +2542,7 @@ impl Display for ChecksumAlgo {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
ChecksumAlgo::Sha256 => "sha256",
ChecksumAlgo::XxHash => "xxhash",
ChecksumAlgo::Blake3 => "blake3",
})
}
}
Expand All @@ -2554,7 +2552,7 @@ pub struct InvalidChecksumAlgo {}

impl Display for InvalidChecksumAlgo {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "expected `sha256`, or `xxhash`")
write!(f, "expected `sha256`, or `blake3`")
}
}

Expand All @@ -2573,11 +2571,9 @@ impl Checksum {
}

pub fn compute(algo: ChecksumAlgo, contents: impl Read) -> Result<Self, io::Error> {
// Buffer size is the same as default for std::io::BufReader.
// This was completely arbitrary and can probably be improved upon.
//
// Mostly I just don't want to read the entire file into memory at once if it's massive.
let mut buf = vec![0; 8 * 1024];
// Buffer size is the recommended amount to fully leverage SIMD instructions on AVX-512 as per
// blake3 documentation.
let mut buf = vec![0; 16 * 1024];
let mut ret = Self {
algo,
value: [0; 32],
Expand Down Expand Up @@ -2617,13 +2613,13 @@ impl Checksum {
value,
)?;
}
ChecksumAlgo::XxHash => {
ChecksumAlgo::Blake3 => {
digest(
XxHash64::with_seed(0),
blake3::Hasher::new(),
|h, b| {
h.write(b);
h.update(b);
},
|h, out| out.copy_from_slice(&h.finish().to_be_bytes()),
|h, out| out.copy_from_slice(h.finalize().as_bytes()),
contents,
&mut buf,
value,
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ fn prepare_rustc(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoResult
base.arg("-Z").arg("binary-dep-depinfo");
}
if build_runner.bcx.gctx.cli_unstable().checksum_freshness {
base.arg("-Z").arg("checksum-hash-algorithm=xxhash");
base.arg("-Z").arg("checksum-hash-algorithm=blake3");
}

if is_primary {
Expand Down

0 comments on commit 97ae01a

Please sign in to comment.