From 340c7dcfe2aca360393dcbca4036f0629938436d Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Tue, 9 Jul 2024 15:13:02 -0400 Subject: [PATCH] WIP --- Cargo.lock | 2 +- Cargo.toml | 2 +- .../build_runner/compilation_files.rs | 4 +- src/cargo/core/compiler/compile_kind.rs | 4 +- src/cargo/core/compiler/fingerprint/mod.rs | 2 +- src/cargo/core/source_id.rs | 52 ++++++++++--------- src/cargo/ops/cargo_compile/mod.rs | 4 +- src/cargo/util/hasher.rs | 22 +------- src/cargo/util/hex.rs | 4 +- src/cargo/util/rustc.rs | 6 +-- 10 files changed, 42 insertions(+), 60 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 13cc7d7482e..0ac97e4eb40 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2945,7 +2945,7 @@ checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" [[package]] name = "rustc-stable-hash" version = "0.1.0" -source = "git+https://github.com/rust-lang/rustc-stable-hash.git?rev=cb8e141b08fb839606a5f79f9b56087cd54b764d#cb8e141b08fb839606a5f79f9b56087cd54b764d" +source = "git+https://github.com/rust-lang/rustc-stable-hash.git?rev=3805516b78c7b2946ae2071d71ffc8235399652d#3805516b78c7b2946ae2071d71ffc8235399652d" [[package]] name = "rustfix" diff --git a/Cargo.toml b/Cargo.toml index c35b3d5ab6a..bf672a893b0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -78,7 +78,7 @@ pulldown-cmark = { version = "0.11.0", default-features = false, features = ["ht rand = "0.8.5" regex = "1.10.4" rusqlite = { version = "0.31.0", features = ["bundled"] } -rustc-stable-hash = { git = "https://github.com/rust-lang/rustc-stable-hash.git", rev = "cb8e141b08fb839606a5f79f9b56087cd54b764d" } +rustc-stable-hash = { git = "https://github.com/rust-lang/rustc-stable-hash.git", rev = "3805516b78c7b2946ae2071d71ffc8235399652d" } rustfix = { version = "0.8.2", path = "crates/rustfix" } same-file = "1.0.6" security-framework = "2.10.0" diff --git a/src/cargo/core/compiler/build_runner/compilation_files.rs b/src/cargo/core/compiler/build_runner/compilation_files.rs index 942b1add240..7be7832a10a 100644 --- a/src/cargo/core/compiler/build_runner/compilation_files.rs +++ b/src/cargo/core/compiler/build_runner/compilation_files.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use std::fmt; -use std::hash::Hash; +use std::hash::{Hash, Hasher}; use std::path::{Path, PathBuf}; use std::sync::Arc; @@ -650,7 +650,7 @@ fn compute_metadata( unit.is_std.hash(&mut hasher); MetaInfo { - meta_hash: Metadata(hasher.finish()), + meta_hash: Metadata(Hasher::finish(&hasher)), use_extra_filename: should_use_metadata(bcx, unit), } } diff --git a/src/cargo/core/compiler/compile_kind.rs b/src/cargo/core/compiler/compile_kind.rs index 937c353dcba..deb518afb48 100644 --- a/src/cargo/core/compiler/compile_kind.rs +++ b/src/cargo/core/compiler/compile_kind.rs @@ -8,7 +8,7 @@ use anyhow::Context as _; use serde::Serialize; use std::collections::BTreeSet; use std::fs; -use std::hash::Hash; +use std::hash::{Hash, Hasher}; use std::path::Path; /// Indicator for how a unit is being compiled. @@ -195,6 +195,6 @@ impl CompileTarget { self.name.hash(&mut hasher); } } - hasher.finish() + Hasher::finish(&hasher) } } diff --git a/src/cargo/core/compiler/fingerprint/mod.rs b/src/cargo/core/compiler/fingerprint/mod.rs index 6ffebd753cd..fb23b97be2c 100644 --- a/src/cargo/core/compiler/fingerprint/mod.rs +++ b/src/cargo/core/compiler/fingerprint/mod.rs @@ -1482,7 +1482,7 @@ fn calculate_normal( local: Mutex::new(local), memoized_hash: Mutex::new(None), metadata, - config: config.finish(), + config: Hasher::finish(&config), compile_kind, rustflags: extra_flags, fs_status: FsStatus::Stale, diff --git a/src/cargo/core/source_id.rs b/src/cargo/core/source_id.rs index f7033e86aaf..7d5d780ac12 100644 --- a/src/cargo/core/source_id.rs +++ b/src/cargo/core/source_id.rs @@ -793,9 +793,11 @@ mod tests { // cross-platform stable hash for absolute paths. #[test] fn test_stable_hash() { - use crate::util::StableHasher; + use std::hash::Hasher; use std::path::Path; + use crate::util::StableHasher; + #[cfg(not(windows))] let ws_root = Path::new("/tmp/ws"); #[cfg(windows)] @@ -804,68 +806,68 @@ mod tests { let gen_hash = |source_id: SourceId| { let mut hasher = StableHasher::new(); source_id.stable_hash(ws_root, &mut hasher); - hasher.finish() + Hasher::finish(&hasher) }; let source_id = SourceId::crates_io(&GlobalContext::default().unwrap()).unwrap(); - assert_eq!(gen_hash(source_id), 14747226178473219715); - assert_eq!(crate::util::hex::short_hash(&source_id), "83d63c3e13aca8cc"); + assert_eq!(gen_hash(source_id), 7062945687441624357); + assert_eq!(crate::util::hex::short_hash(&source_id), "25cdd57fae9f0462"); let url = "https://my-crates.io".into_url().unwrap(); let source_id = SourceId::for_registry(&url).unwrap(); - assert_eq!(gen_hash(source_id), 2056262832525457700); - assert_eq!(crate::util::hex::short_hash(&source_id), "24b984d12650891c"); + assert_eq!(gen_hash(source_id), 8310250053664888498); + assert_eq!(crate::util::hex::short_hash(&source_id), "b2d65deb64f05373"); let url = "https://your-crates.io".into_url().unwrap(); let source_id = SourceId::for_alt_registry(&url, "alt").unwrap(); - assert_eq!(gen_hash(source_id), 7851411715584162426); - assert_eq!(crate::util::hex::short_hash(&source_id), "7afabb545bd1f56c"); + assert_eq!(gen_hash(source_id), 14149534903000258933); + assert_eq!(crate::util::hex::short_hash(&source_id), "755952de063f5dc4"); let url = "sparse+https://my-crates.io".into_url().unwrap(); let source_id = SourceId::for_registry(&url).unwrap(); - assert_eq!(gen_hash(source_id), 15233380663065439616); - assert_eq!(crate::util::hex::short_hash(&source_id), "80ed51ce00d767d3"); + assert_eq!(gen_hash(source_id), 16249512552851930162); + assert_eq!(crate::util::hex::short_hash(&source_id), "327cfdbd92dd81e1"); let url = "sparse+https://your-crates.io".into_url().unwrap(); let source_id = SourceId::for_alt_registry(&url, "alt").unwrap(); - assert_eq!(gen_hash(source_id), 12749290624384351691); - assert_eq!(crate::util::hex::short_hash(&source_id), "cbbda5344694eeb0"); + assert_eq!(gen_hash(source_id), 6156697384053352292); + assert_eq!(crate::util::hex::short_hash(&source_id), "64a713b6a6fb7055"); let url = "file:///tmp/ws/crate".into_url().unwrap(); let source_id = SourceId::for_git(&url, GitReference::DefaultBranch).unwrap(); - assert_eq!(gen_hash(source_id), 3109465066469481245); - assert_eq!(crate::util::hex::short_hash(&source_id), "1d5b66d8000a272b"); + assert_eq!(gen_hash(source_id), 473480029881867801); + assert_eq!(crate::util::hex::short_hash(&source_id), "199e591d94239206"); let path = &ws_root.join("crate"); let source_id = SourceId::for_local_registry(path).unwrap(); #[cfg(not(windows))] { - assert_eq!(gen_hash(source_id), 17171351456028149232); - assert_eq!(crate::util::hex::short_hash(&source_id), "f0c5f1e92be54cee"); + assert_eq!(gen_hash(source_id), 11515846423845066584); + assert_eq!(crate::util::hex::short_hash(&source_id), "58d73c154f81d09f"); } #[cfg(windows)] { - assert_eq!(gen_hash(source_id), 10712195329887934127); - assert_eq!(crate::util::hex::short_hash(&source_id), "af96919ae55ca994"); + assert_eq!(gen_hash(source_id), 6146331155906064276); + assert_eq!(crate::util::hex::short_hash(&source_id), "946fb2239f274c55"); } let source_id = SourceId::for_path(path).unwrap(); - assert_eq!(gen_hash(source_id), 13241112980875747369); + assert_eq!(gen_hash(source_id), 215644081443634269); #[cfg(not(windows))] - assert_eq!(crate::util::hex::short_hash(&source_id), "e5ba2edec163e65a"); + assert_eq!(crate::util::hex::short_hash(&source_id), "64bace89c92b101f"); #[cfg(windows)] - assert_eq!(crate::util::hex::short_hash(&source_id), "429dd6f2283a9b5c"); + assert_eq!(crate::util::hex::short_hash(&source_id), "01e1e6c391813fb6"); let source_id = SourceId::for_directory(path).unwrap(); #[cfg(not(windows))] { - assert_eq!(gen_hash(source_id), 12461124588148212881); - assert_eq!(crate::util::hex::short_hash(&source_id), "91c47582caceeeac"); + assert_eq!(gen_hash(source_id), 6127590343904940368); + assert_eq!(crate::util::hex::short_hash(&source_id), "505191d1f3920955"); } #[cfg(windows)] { - assert_eq!(gen_hash(source_id), 17000469607053345884); - assert_eq!(crate::util::hex::short_hash(&source_id), "5c443d0709cdedeb"); + assert_eq!(gen_hash(source_id), 10423446877655960172); + assert_eq!(crate::util::hex::short_hash(&source_id), "6c8ad69db585a790"); } } diff --git a/src/cargo/ops/cargo_compile/mod.rs b/src/cargo/ops/cargo_compile/mod.rs index a47df41f600..d228eb91129 100644 --- a/src/cargo/ops/cargo_compile/mod.rs +++ b/src/cargo/ops/cargo_compile/mod.rs @@ -36,7 +36,7 @@ //! ["Cargo Target"]: https://doc.rust-lang.org/nightly/cargo/reference/cargo-targets.html use std::collections::{HashMap, HashSet}; -use std::hash::Hash; +use std::hash::{Hash, Hasher}; use std::sync::Arc; use crate::core::compiler::unit_dependencies::build_unit_dependencies; @@ -648,7 +648,7 @@ fn traverse_and_share( .collect(); // Here, we have recursively traversed this unit's dependencies, and hashed them: we can // finalize the dep hash. - let new_dep_hash = dep_hash.finish(); + let new_dep_hash = Hasher::finish(&dep_hash); // This is the key part of the sharing process: if the unit is a runtime dependency, whose // target is the same as the host, we canonicalize the compile kind to `CompileKind::Host`. diff --git a/src/cargo/util/hasher.rs b/src/cargo/util/hasher.rs index 60d37f8863c..0decc83c918 100644 --- a/src/cargo/util/hasher.rs +++ b/src/cargo/util/hasher.rs @@ -2,24 +2,4 @@ //! //! This is a wrapper around [`rustc_stable_hash::StableHasher`]. -pub struct StableHasher(rustc_stable_hash::StableHasher); - -impl StableHasher { - pub fn new() -> StableHasher { - StableHasher(rustc_stable_hash::StableHasher::new()) - } - - pub fn finish(self) -> u64 { - self.0.finalize().0 - } -} - -impl std::hash::Hasher for StableHasher { - fn finish(&self) -> u64 { - panic!("call StableHasher::finish instead"); - } - - fn write(&mut self, bytes: &[u8]) { - self.0.write(bytes) - } -} +pub use rustc_stable_hash::StableSipHasher128 as StableHasher; diff --git a/src/cargo/util/hex.rs b/src/cargo/util/hex.rs index 2d06d9b5939..c0583e4a3af 100644 --- a/src/cargo/util/hex.rs +++ b/src/cargo/util/hex.rs @@ -10,7 +10,7 @@ pub fn to_hex(num: u64) -> String { pub fn hash_u64(hashable: H) -> u64 { let mut hasher = StableHasher::new(); hashable.hash(&mut hasher); - hasher.finish() + Hasher::finish(&hasher) } pub fn hash_u64_file(mut file: &File) -> std::io::Result { @@ -23,7 +23,7 @@ pub fn hash_u64_file(mut file: &File) -> std::io::Result { } hasher.write(&buf[..n]); } - Ok(hasher.finish()) + Ok(Hasher::finish(&hasher)) } pub fn short_hash(hashable: &H) -> String { diff --git a/src/cargo/util/rustc.rs b/src/cargo/util/rustc.rs index 4e035b22dbd..2ba8a06e81a 100644 --- a/src/cargo/util/rustc.rs +++ b/src/cargo/util/rustc.rs @@ -1,6 +1,6 @@ use std::collections::hash_map::HashMap; use std::env; -use std::hash::Hash; +use std::hash::{Hash, Hasher}; use std::path::{Path, PathBuf}; use std::sync::Mutex; @@ -374,7 +374,7 @@ fn rustc_fingerprint( _ => (), } - Ok(hasher.finish()) + Ok(Hasher::finish(&hasher)) } fn process_fingerprint(cmd: &ProcessBuilder, extra_fingerprint: u64) -> u64 { @@ -384,5 +384,5 @@ fn process_fingerprint(cmd: &ProcessBuilder, extra_fingerprint: u64) -> u64 { let mut env = cmd.get_envs().iter().collect::>(); env.sort_unstable(); env.hash(&mut hasher); - hasher.finish() + Hasher::finish(&hasher) }