Skip to content

Commit 1066af5

Browse files
authoredFeb 22, 2025
Rollup merge of rust-lang#137410 - saethlin:stable-dep-tracking-hash, r=workingjubilee
Use StableHasher + Hash64 for dep_tracking_hash This is similar to rust-lang#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.
2 parents d3e46d2 + fd451dc commit 1066af5

File tree

5 files changed

+20
-16
lines changed

5 files changed

+20
-16
lines changed
 

‎Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3779,6 +3779,7 @@ dependencies = [
37793779
"rustc_fluent_macro",
37803780
"rustc_fs_util",
37813781
"rustc_graphviz",
3782+
"rustc_hashes",
37823783
"rustc_hir",
37833784
"rustc_macros",
37843785
"rustc_middle",

‎compiler/rustc_incremental/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ rustc_errors = { path = "../rustc_errors" }
1212
rustc_fluent_macro = { path = "../rustc_fluent_macro" }
1313
rustc_fs_util = { path = "../rustc_fs_util" }
1414
rustc_graphviz = { path = "../rustc_graphviz" }
15+
rustc_hashes = { path = "../rustc_hashes" }
1516
rustc_hir = { path = "../rustc_hir" }
1617
rustc_macros = { path = "../rustc_macros" }
1718
rustc_middle = { path = "../rustc_middle" }

‎compiler/rustc_incremental/src/persist/load.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::sync::Arc;
55

66
use rustc_data_structures::memmap::Mmap;
77
use rustc_data_structures::unord::UnordMap;
8+
use rustc_hashes::Hash64;
89
use rustc_middle::dep_graph::{DepGraph, DepsType, SerializedDepGraph, WorkProductMap};
910
use rustc_middle::query::on_disk_cache::OnDiskCache;
1011
use rustc_serialize::Decodable;
@@ -154,7 +155,7 @@ fn load_dep_graph(sess: &Session) -> LoadResult<(Arc<SerializedDepGraph>, WorkPr
154155
sess.dcx().emit_warn(errors::CorruptFile { path: &path });
155156
return LoadResult::DataOutOfDate;
156157
};
157-
let prev_commandline_args_hash = u64::decode(&mut decoder);
158+
let prev_commandline_args_hash = Hash64::decode(&mut decoder);
158159

159160
if prev_commandline_args_hash != expected_hash {
160161
if sess.opts.unstable_opts.incremental_info {

‎compiler/rustc_session/src/config.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -2928,12 +2928,13 @@ pub enum WasiExecModel {
29282928
/// how the hash should be calculated when adding a new command-line argument.
29292929
pub(crate) mod dep_tracking {
29302930
use std::collections::BTreeMap;
2931-
use std::hash::{DefaultHasher, Hash};
2931+
use std::hash::Hash;
29322932
use std::num::NonZero;
29332933
use std::path::PathBuf;
29342934

29352935
use rustc_abi::Align;
29362936
use rustc_data_structures::fx::FxIndexMap;
2937+
use rustc_data_structures::stable_hasher::StableHasher;
29372938
use rustc_errors::LanguageIdentifier;
29382939
use rustc_feature::UnstableFeatures;
29392940
use rustc_hashes::Hash64;
@@ -2960,7 +2961,7 @@ pub(crate) mod dep_tracking {
29602961
pub(crate) trait DepTrackingHash {
29612962
fn hash(
29622963
&self,
2963-
hasher: &mut DefaultHasher,
2964+
hasher: &mut StableHasher,
29642965
error_format: ErrorOutputType,
29652966
for_crate_hash: bool,
29662967
);
@@ -2969,7 +2970,7 @@ pub(crate) mod dep_tracking {
29692970
macro_rules! impl_dep_tracking_hash_via_hash {
29702971
($($t:ty),+ $(,)?) => {$(
29712972
impl DepTrackingHash for $t {
2972-
fn hash(&self, hasher: &mut DefaultHasher, _: ErrorOutputType, _for_crate_hash: bool) {
2973+
fn hash(&self, hasher: &mut StableHasher, _: ErrorOutputType, _for_crate_hash: bool) {
29732974
Hash::hash(self, hasher);
29742975
}
29752976
}
@@ -2979,7 +2980,7 @@ pub(crate) mod dep_tracking {
29792980
impl<T: DepTrackingHash> DepTrackingHash for Option<T> {
29802981
fn hash(
29812982
&self,
2982-
hasher: &mut DefaultHasher,
2983+
hasher: &mut StableHasher,
29832984
error_format: ErrorOutputType,
29842985
for_crate_hash: bool,
29852986
) {
@@ -3064,7 +3065,7 @@ pub(crate) mod dep_tracking {
30643065
{
30653066
fn hash(
30663067
&self,
3067-
hasher: &mut DefaultHasher,
3068+
hasher: &mut StableHasher,
30683069
error_format: ErrorOutputType,
30693070
for_crate_hash: bool,
30703071
) {
@@ -3083,7 +3084,7 @@ pub(crate) mod dep_tracking {
30833084
{
30843085
fn hash(
30853086
&self,
3086-
hasher: &mut DefaultHasher,
3087+
hasher: &mut StableHasher,
30873088
error_format: ErrorOutputType,
30883089
for_crate_hash: bool,
30893090
) {
@@ -3099,7 +3100,7 @@ pub(crate) mod dep_tracking {
30993100
impl<T: DepTrackingHash> DepTrackingHash for Vec<T> {
31003101
fn hash(
31013102
&self,
3102-
hasher: &mut DefaultHasher,
3103+
hasher: &mut StableHasher,
31033104
error_format: ErrorOutputType,
31043105
for_crate_hash: bool,
31053106
) {
@@ -3114,7 +3115,7 @@ pub(crate) mod dep_tracking {
31143115
impl<T: DepTrackingHash, V: DepTrackingHash> DepTrackingHash for FxIndexMap<T, V> {
31153116
fn hash(
31163117
&self,
3117-
hasher: &mut DefaultHasher,
3118+
hasher: &mut StableHasher,
31183119
error_format: ErrorOutputType,
31193120
for_crate_hash: bool,
31203121
) {
@@ -3129,7 +3130,7 @@ pub(crate) mod dep_tracking {
31293130
impl DepTrackingHash for OutputTypes {
31303131
fn hash(
31313132
&self,
3132-
hasher: &mut DefaultHasher,
3133+
hasher: &mut StableHasher,
31333134
error_format: ErrorOutputType,
31343135
for_crate_hash: bool,
31353136
) {
@@ -3146,7 +3147,7 @@ pub(crate) mod dep_tracking {
31463147
// This is a stable hash because BTreeMap is a sorted container
31473148
pub(crate) fn stable_hash(
31483149
sub_hashes: BTreeMap<&'static str, &dyn DepTrackingHash>,
3149-
hasher: &mut DefaultHasher,
3150+
hasher: &mut StableHasher,
31503151
error_format: ErrorOutputType,
31513152
for_crate_hash: bool,
31523153
) {

‎compiler/rustc_session/src/options.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use std::collections::BTreeMap;
2-
use std::hash::{DefaultHasher, Hasher};
32
use std::num::{IntErrorKind, NonZero};
43
use std::path::PathBuf;
54
use std::str;
65

76
use rustc_abi::Align;
87
use rustc_data_structures::fx::FxIndexMap;
98
use rustc_data_structures::profiling::TimePassesFormat;
9+
use rustc_data_structures::stable_hasher::StableHasher;
1010
use rustc_errors::{ColorConfig, LanguageIdentifier, TerminalUrl};
1111
use rustc_feature::UnstableFeatures;
1212
use rustc_hashes::Hash64;
@@ -251,7 +251,7 @@ macro_rules! top_level_options {
251251
}
252252

253253
impl Options {
254-
pub fn dep_tracking_hash(&self, for_crate_hash: bool) -> u64 {
254+
pub fn dep_tracking_hash(&self, for_crate_hash: bool) -> Hash64 {
255255
let mut sub_hashes = BTreeMap::new();
256256
$({
257257
hash_opt!($opt,
@@ -260,7 +260,7 @@ macro_rules! top_level_options {
260260
for_crate_hash,
261261
[$dep_tracking_marker]);
262262
})*
263-
let mut hasher = DefaultHasher::new();
263+
let mut hasher = StableHasher::new();
264264
dep_tracking::stable_hash(sub_hashes,
265265
&mut hasher,
266266
self.error_format,
@@ -545,7 +545,7 @@ macro_rules! options {
545545
build_options(early_dcx, matches, target_modifiers, $stat, $prefix, $outputname)
546546
}
547547

548-
fn dep_tracking_hash(&self, for_crate_hash: bool, error_format: ErrorOutputType) -> u64 {
548+
fn dep_tracking_hash(&self, for_crate_hash: bool, error_format: ErrorOutputType) -> Hash64 {
549549
let mut sub_hashes = BTreeMap::new();
550550
$({
551551
hash_opt!($opt,
@@ -554,7 +554,7 @@ macro_rules! options {
554554
for_crate_hash,
555555
[$dep_tracking_marker]);
556556
})*
557-
let mut hasher = DefaultHasher::new();
557+
let mut hasher = StableHasher::new();
558558
dep_tracking::stable_hash(sub_hashes,
559559
&mut hasher,
560560
error_format,

0 commit comments

Comments
 (0)
Please sign in to comment.