From 79ee8dc08073a0abf93d2b494ac5c1be11d4342e Mon Sep 17 00:00:00 2001 From: Daniel Smith Date: Thu, 2 Jul 2026 14:59:41 -0400 Subject: [PATCH] Hash metadata with XXH3-128 instead of SipHash-128 for the crate SVH MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The SVH is computed by hashing the entire encoded metadata blob, and SipHash-128 (~2.5 Ir/byte) dominated the cost — it accounted for the whole of the measured regression on metadata-heavy crates. Switch the byte pass to XXH3-128, which is ~10x cheaper per byte on its runtime-detected AVX2 path. XXH3 is non-cryptographic, which is fine here: the SVH is only a change detector, must be reproducible (so it is never keyed secretly, meaning even the previous SipHash use was not collision-resistant), and compiling a crate already executes its build scripts and proc-macros, so the crate author is trusted regardless. The untracked supplements (dep-tracking hash and the HIR hash for e.g. DefKind::GlobalAsm) are still folded in via a StableHasher fingerprint, so coverage is unchanged. twox-hash 2.x is already in the dependency graph via ruzstd; this only adds a direct edge (xxhash3_128 + std features for the runtime SIMD dispatch). --- Cargo.lock | 1 + compiler/rustc_metadata/Cargo.toml | 1 + compiler/rustc_metadata/src/rmeta/encoder.rs | 34 +++++++++++++------- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 99c2914ff34..b5fb5057716 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4428,6 +4428,7 @@ dependencies = [ "rustc_target", "tempfile", "tracing", + "twox-hash 2.1.2", ] [[package]] diff --git a/compiler/rustc_metadata/Cargo.toml b/compiler/rustc_metadata/Cargo.toml index e036c2d7e23..900dad3be1d 100644 --- a/compiler/rustc_metadata/Cargo.toml +++ b/compiler/rustc_metadata/Cargo.toml @@ -34,6 +34,7 @@ rustc_structures = { path = "../rustc_structures" } rustc_target = { path = "../rustc_target" } tempfile = "3.7.1" tracing = "0.1" +twox-hash = { version = "2", default-features = false, features = ["xxhash3_128", "std"] } # tidy-alphabetical-end [target.'cfg(target_os = "aix")'.dependencies] diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index f9343cb35f1..afa5646bea8 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -2,7 +2,6 @@ use std::borrow::Borrow; use std::cell::RefCell; use std::collections::hash_map::Entry; use std::fs::File; -use std::hash::Hasher; use std::io::{Read, Seek, SeekFrom, Write}; use std::ops::Deref; use std::path::{Path, PathBuf}; @@ -44,6 +43,7 @@ use rustc_span::{ }; use rustc_structures::CrateType; use tracing::{debug, instrument, trace}; +use twox_hash::XxHash3_128; use crate::diagnostics::{FailCreateFileEncoder, FailWriteFile}; use crate::eii::EiiMapEncodedKeyValue; @@ -51,7 +51,7 @@ use crate::rmeta::*; pub(super) struct EncodeContext<'a, 'tcx> { opaque: FileEncoder<'a>, - metadata_hasher: Arc>, + metadata_hasher: Arc>, tcx: TyCtxt<'tcx>, feat: &'tcx rustc_feature::Features, tables: TableBuilders, @@ -802,7 +802,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { // `crate_hash` query computes directly without consulting `local_crate_hash`. let hash = if tcx.sess.opts.unstable_opts.metadata_crate_hash { self.opaque.flush(); - Svh::new(self.metadata_hasher.lock().unwrap().clone().finish()) + Svh::new(Fingerprint::from_le_bytes( + self.metadata_hasher.lock().unwrap().finish_128().to_le_bytes(), + )) } else { tcx.crate_hash(LOCAL_CRATE) }; @@ -2625,22 +2627,30 @@ fn with_encode_metadata_header( // Under `-Z metadata-crate-hash=no` the SVH comes from the legacy `crate_hash` query instead and // this hasher is never consulted, so we skip seeding it and feeding metadata bytes into it. let metadata_crate_hash = tcx.sess.opts.unstable_opts.metadata_crate_hash; - let stable_hasher = Arc::new(Mutex::new(StableHasher::new())); + // The SVH is an XXH3-128 digest of the encoded metadata bytes. XXH3 is a fast, non-cryptographic + // hash; that is sufficient here because the SVH is only a change detector, is never keyed + // secretly (it must be reproducible), and compiling a crate already runs its build scripts and + // proc-macros, so the crate author is trusted regardless. + let metadata_hasher = Arc::new(Mutex::new(XxHash3_128::new())); if metadata_crate_hash { + // Fold in inputs that are not part of the encoded metadata bytes, reduced to a single + // fingerprint via the stable hasher and then mixed into the byte digest. let hir_body_hash = compute_hir_hash(tcx); - tcx.with_stable_hashing_context(|mut hcx| { - let mut hasher = stable_hasher.lock().unwrap(); + let supplement: Fingerprint = tcx.with_stable_hashing_context(|mut hcx| { + let mut hasher = StableHasher::new(); // Add dep_tracking_hash to ensure the SVH changes when any tracked flag changes. - tcx.sess.opts.dep_tracking_hash(true).stable_hash(&mut hcx, &mut *hasher); + tcx.sess.opts.dep_tracking_hash(true).stable_hash(&mut hcx, &mut hasher); // Add HIR hash for untracked elements, e.g. DefKind::GlobalAsm. - hir_body_hash.stable_hash(&mut hcx, &mut *hasher); + hir_body_hash.stable_hash(&mut hcx, &mut hasher); + hasher.finish() }); + metadata_hasher.lock().unwrap().write(&supplement.to_le_bytes()); } - // Feed every flushed byte into `stable_hasher` so the SVH covers the entire encoded metadata. + // Feed every flushed byte into `metadata_hasher` so the SVH covers the entire encoded metadata. let mut flush_strategy = { - let stable_hasher = Arc::clone(&stable_hasher); - move |bytes: &[u8]| stable_hasher.lock().unwrap().write(bytes) + let metadata_hasher = Arc::clone(&metadata_hasher); + move |bytes: &[u8]| metadata_hasher.lock().unwrap().write(bytes) }; let mut encoder = if metadata_crate_hash { @@ -2667,7 +2677,7 @@ fn with_encode_metadata_header( let mut ecx = EncodeContext { opaque: encoder, - metadata_hasher: Arc::clone(&stable_hasher), + metadata_hasher: Arc::clone(&metadata_hasher), tcx, feat: tcx.features(), tables: Default::default(),