Rollup merge of #137410 - saethlin:stable-dep-tracking-hash, r=workingjubilee

Use StableHasher + Hash64 for dep_tracking_hash

This is similar to https://github.com/rust-lang/rust/pull/137095. We currently have a +/- 1 byte jitter in the size of dep graphs reported on perf.rust-lang.org. I think this fixes that jitter.

When I introduced `Hash64`, I wired it through most of the compiler by making it an output of `StableHasher::finalize` then fixing the compile errors. I missed this case because the `u64` hash in this function is being produced by `DefaultHasher` instead. That seems pretty sketchy because the code seems confident that the hash needs to be stable, and we have a mechanism for stable hashing that we weren't using here.
This commit is contained in:
Matthias Krüger 2025-02-22 11:36:46 +01:00 committed by GitHub
commit 1066af5b1a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 20 additions and 16 deletions

View File

@ -3779,6 +3779,7 @@ dependencies = [
"rustc_fluent_macro",
"rustc_fs_util",
"rustc_graphviz",
"rustc_hashes",
"rustc_hir",
"rustc_macros",
"rustc_middle",

View File

@ -12,6 +12,7 @@ rustc_errors = { path = "../rustc_errors" }
rustc_fluent_macro = { path = "../rustc_fluent_macro" }
rustc_fs_util = { path = "../rustc_fs_util" }
rustc_graphviz = { path = "../rustc_graphviz" }
rustc_hashes = { path = "../rustc_hashes" }
rustc_hir = { path = "../rustc_hir" }
rustc_macros = { path = "../rustc_macros" }
rustc_middle = { path = "../rustc_middle" }

View File

@ -5,6 +5,7 @@ use std::sync::Arc;
use rustc_data_structures::memmap::Mmap;
use rustc_data_structures::unord::UnordMap;
use rustc_hashes::Hash64;
use rustc_middle::dep_graph::{DepGraph, DepsType, SerializedDepGraph, WorkProductMap};
use rustc_middle::query::on_disk_cache::OnDiskCache;
use rustc_serialize::Decodable;
@ -154,7 +155,7 @@ fn load_dep_graph(sess: &Session) -> LoadResult<(Arc<SerializedDepGraph>, WorkPr
sess.dcx().emit_warn(errors::CorruptFile { path: &path });
return LoadResult::DataOutOfDate;
};
let prev_commandline_args_hash = u64::decode(&mut decoder);
let prev_commandline_args_hash = Hash64::decode(&mut decoder);
if prev_commandline_args_hash != expected_hash {
if sess.opts.unstable_opts.incremental_info {

View File

@ -2928,12 +2928,13 @@ pub enum WasiExecModel {
/// how the hash should be calculated when adding a new command-line argument.
pub(crate) mod dep_tracking {
use std::collections::BTreeMap;
use std::hash::{DefaultHasher, Hash};
use std::hash::Hash;
use std::num::NonZero;
use std::path::PathBuf;
use rustc_abi::Align;
use rustc_data_structures::fx::FxIndexMap;
use rustc_data_structures::stable_hasher::StableHasher;
use rustc_errors::LanguageIdentifier;
use rustc_feature::UnstableFeatures;
use rustc_hashes::Hash64;
@ -2960,7 +2961,7 @@ pub(crate) mod dep_tracking {
pub(crate) trait DepTrackingHash {
fn hash(
&self,
hasher: &mut DefaultHasher,
hasher: &mut StableHasher,
error_format: ErrorOutputType,
for_crate_hash: bool,
);
@ -2969,7 +2970,7 @@ pub(crate) mod dep_tracking {
macro_rules! impl_dep_tracking_hash_via_hash {
($($t:ty),+ $(,)?) => {$(
impl DepTrackingHash for $t {
fn hash(&self, hasher: &mut DefaultHasher, _: ErrorOutputType, _for_crate_hash: bool) {
fn hash(&self, hasher: &mut StableHasher, _: ErrorOutputType, _for_crate_hash: bool) {
Hash::hash(self, hasher);
}
}
@ -2979,7 +2980,7 @@ pub(crate) mod dep_tracking {
impl<T: DepTrackingHash> DepTrackingHash for Option<T> {
fn hash(
&self,
hasher: &mut DefaultHasher,
hasher: &mut StableHasher,
error_format: ErrorOutputType,
for_crate_hash: bool,
) {
@ -3064,7 +3065,7 @@ pub(crate) mod dep_tracking {
{
fn hash(
&self,
hasher: &mut DefaultHasher,
hasher: &mut StableHasher,
error_format: ErrorOutputType,
for_crate_hash: bool,
) {
@ -3083,7 +3084,7 @@ pub(crate) mod dep_tracking {
{
fn hash(
&self,
hasher: &mut DefaultHasher,
hasher: &mut StableHasher,
error_format: ErrorOutputType,
for_crate_hash: bool,
) {
@ -3099,7 +3100,7 @@ pub(crate) mod dep_tracking {
impl<T: DepTrackingHash> DepTrackingHash for Vec<T> {
fn hash(
&self,
hasher: &mut DefaultHasher,
hasher: &mut StableHasher,
error_format: ErrorOutputType,
for_crate_hash: bool,
) {
@ -3114,7 +3115,7 @@ pub(crate) mod dep_tracking {
impl<T: DepTrackingHash, V: DepTrackingHash> DepTrackingHash for FxIndexMap<T, V> {
fn hash(
&self,
hasher: &mut DefaultHasher,
hasher: &mut StableHasher,
error_format: ErrorOutputType,
for_crate_hash: bool,
) {
@ -3129,7 +3130,7 @@ pub(crate) mod dep_tracking {
impl DepTrackingHash for OutputTypes {
fn hash(
&self,
hasher: &mut DefaultHasher,
hasher: &mut StableHasher,
error_format: ErrorOutputType,
for_crate_hash: bool,
) {
@ -3146,7 +3147,7 @@ pub(crate) mod dep_tracking {
// This is a stable hash because BTreeMap is a sorted container
pub(crate) fn stable_hash(
sub_hashes: BTreeMap<&'static str, &dyn DepTrackingHash>,
hasher: &mut DefaultHasher,
hasher: &mut StableHasher,
error_format: ErrorOutputType,
for_crate_hash: bool,
) {

View File

@ -1,5 +1,4 @@
use std::collections::BTreeMap;
use std::hash::{DefaultHasher, Hasher};
use std::num::{IntErrorKind, NonZero};
use std::path::PathBuf;
use std::str;
@ -7,6 +6,7 @@ use std::str;
use rustc_abi::Align;
use rustc_data_structures::fx::FxIndexMap;
use rustc_data_structures::profiling::TimePassesFormat;
use rustc_data_structures::stable_hasher::StableHasher;
use rustc_errors::{ColorConfig, LanguageIdentifier, TerminalUrl};
use rustc_feature::UnstableFeatures;
use rustc_hashes::Hash64;
@ -251,7 +251,7 @@ macro_rules! top_level_options {
}
impl Options {
pub fn dep_tracking_hash(&self, for_crate_hash: bool) -> u64 {
pub fn dep_tracking_hash(&self, for_crate_hash: bool) -> Hash64 {
let mut sub_hashes = BTreeMap::new();
$({
hash_opt!($opt,
@ -260,7 +260,7 @@ macro_rules! top_level_options {
for_crate_hash,
[$dep_tracking_marker]);
})*
let mut hasher = DefaultHasher::new();
let mut hasher = StableHasher::new();
dep_tracking::stable_hash(sub_hashes,
&mut hasher,
self.error_format,
@ -545,7 +545,7 @@ macro_rules! options {
build_options(early_dcx, matches, target_modifiers, $stat, $prefix, $outputname)
}
fn dep_tracking_hash(&self, for_crate_hash: bool, error_format: ErrorOutputType) -> u64 {
fn dep_tracking_hash(&self, for_crate_hash: bool, error_format: ErrorOutputType) -> Hash64 {
let mut sub_hashes = BTreeMap::new();
$({
hash_opt!($opt,
@ -554,7 +554,7 @@ macro_rules! options {
for_crate_hash,
[$dep_tracking_marker]);
})*
let mut hasher = DefaultHasher::new();
let mut hasher = StableHasher::new();
dep_tracking::stable_hash(sub_hashes,
&mut hasher,
error_format,