Skip to content

Commit d944ea1

Browse files
committed
Remove WithCachedTypeInfo::stable_hash.
We store a stable hash value in the most common interned values (e.g. types, predicates, regions). This is 16 bytes of data. - In non-incremental builds it's a straightforward performance loss: the stable hash isn't computed or used, and the 16 bytes of space goes to waste (but it still gets hashed when interning). - In incremental builds it avoids some hashing but doesn't seem to actually be a genuine performance win, and the complexity doesn't seem worth it.
1 parent 913e4be commit d944ea1

8 files changed

Lines changed: 25 additions & 131 deletions

File tree

compiler/rustc_middle/src/ty/context.rs

Lines changed: 14 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,12 @@ use std::{fmt, iter, mem};
1818
use rustc_abi::{ExternAbi, FieldIdx, Layout, LayoutData, TargetDataLayout, VariantIdx};
1919
use rustc_ast as ast;
2020
use rustc_data_structures::defer;
21-
use rustc_data_structures::fingerprint::Fingerprint;
2221
use rustc_data_structures::fx::FxHashMap;
2322
use rustc_data_structures::intern::Interned;
2423
use rustc_data_structures::jobserver::Proxy;
2524
use rustc_data_structures::profiling::SelfProfilerRef;
2625
use rustc_data_structures::sharded::{IntoPointer, ShardedHashMap};
27-
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
26+
use rustc_data_structures::stable_hasher::HashStable;
2827
use rustc_data_structures::steal::Steal;
2928
use rustc_data_structures::sync::{
3029
self, DynSend, DynSync, FreezeReadGuard, Lock, RwLock, WorkerLocal,
@@ -47,9 +46,7 @@ use rustc_span::def_id::{CRATE_DEF_ID, DefPathHash, StableCrateId};
4746
use rustc_span::{DUMMY_SP, Ident, Span, Symbol, kw, sym};
4847
use rustc_type_ir::TyKind::*;
4948
pub use rustc_type_ir::lift::Lift;
50-
use rustc_type_ir::{
51-
CollectAndApply, FnSigKind, TypeFlags, WithCachedTypeInfo, elaborate, search_graph,
52-
};
49+
use rustc_type_ir::{CollectAndApply, FnSigKind, WithCachedTypeInfo, elaborate, search_graph};
5350
use tracing::{debug, instrument};
5451

5552
use crate::arena::Arena;
@@ -247,16 +244,13 @@ impl<'tcx> CtxtInterners<'tcx> {
247244
/// Interns a type. (Use `mk_*` functions instead, where possible.)
248245
#[allow(rustc::usage_of_ty_tykind)]
249246
#[inline(never)]
250-
fn intern_ty(&self, kind: TyKind<'tcx>, sess: &Session, untracked: &Untracked) -> Ty<'tcx> {
247+
fn intern_ty(&self, kind: TyKind<'tcx>) -> Ty<'tcx> {
251248
Ty(Interned::new_unchecked(
252249
self.type_
253250
.intern(kind, |kind| {
254251
let flags = ty::FlagComputation::<TyCtxt<'tcx>>::for_kind(&kind);
255-
let stable_hash = self.stable_hash(&flags, sess, untracked, &kind);
256-
257252
InternedInSet(self.arena.alloc(WithCachedTypeInfo {
258253
internee: kind,
259-
stable_hash,
260254
flags: flags.flags,
261255
outer_exclusive_binder: flags.outer_exclusive_binder,
262256
}))
@@ -268,21 +262,13 @@ impl<'tcx> CtxtInterners<'tcx> {
268262
/// Interns a const. (Use `mk_*` functions instead, where possible.)
269263
#[allow(rustc::usage_of_ty_tykind)]
270264
#[inline(never)]
271-
fn intern_const(
272-
&self,
273-
kind: ty::ConstKind<'tcx>,
274-
sess: &Session,
275-
untracked: &Untracked,
276-
) -> Const<'tcx> {
265+
fn intern_const(&self, kind: ty::ConstKind<'tcx>) -> Const<'tcx> {
277266
Const(Interned::new_unchecked(
278267
self.const_
279268
.intern(kind, |kind: ty::ConstKind<'_>| {
280269
let flags = ty::FlagComputation::<TyCtxt<'tcx>>::for_const_kind(&kind);
281-
let stable_hash = self.stable_hash(&flags, sess, untracked, &kind);
282-
283270
InternedInSet(self.arena.alloc(WithCachedTypeInfo {
284271
internee: kind,
285-
stable_hash,
286272
flags: flags.flags,
287273
outer_exclusive_binder: flags.outer_exclusive_binder,
288274
}))
@@ -291,43 +277,15 @@ impl<'tcx> CtxtInterners<'tcx> {
291277
))
292278
}
293279

294-
fn stable_hash<'a, T: HashStable<StableHashingContext<'a>>>(
295-
&self,
296-
flags: &ty::FlagComputation<TyCtxt<'tcx>>,
297-
sess: &'a Session,
298-
untracked: &'a Untracked,
299-
val: &T,
300-
) -> Fingerprint {
301-
// It's impossible to hash inference variables (and will ICE), so we don't need to try to cache them.
302-
// Without incremental, we rarely stable-hash types, so let's not do it proactively.
303-
if flags.flags.intersects(TypeFlags::HAS_INFER) || sess.opts.incremental.is_none() {
304-
Fingerprint::ZERO
305-
} else {
306-
let mut hasher = StableHasher::new();
307-
let mut hcx = StableHashingContext::new(sess, untracked);
308-
val.hash_stable(&mut hcx, &mut hasher);
309-
hasher.finish()
310-
}
311-
}
312-
313280
/// Interns a predicate. (Use `mk_predicate` instead, where possible.)
314281
#[inline(never)]
315-
fn intern_predicate(
316-
&self,
317-
kind: Binder<'tcx, PredicateKind<'tcx>>,
318-
sess: &Session,
319-
untracked: &Untracked,
320-
) -> Predicate<'tcx> {
282+
fn intern_predicate(&self, kind: Binder<'tcx, PredicateKind<'tcx>>) -> Predicate<'tcx> {
321283
Predicate(Interned::new_unchecked(
322284
self.predicate
323285
.intern(kind, |kind| {
324286
let flags = ty::FlagComputation::<TyCtxt<'tcx>>::for_predicate(kind);
325-
326-
let stable_hash = self.stable_hash(&flags, sess, untracked, &kind);
327-
328287
InternedInSet(self.arena.alloc(WithCachedTypeInfo {
329288
internee: kind,
330-
stable_hash,
331289
flags: flags.flags,
332290
outer_exclusive_binder: flags.outer_exclusive_binder,
333291
}))
@@ -463,12 +421,8 @@ pub struct CommonConsts<'tcx> {
463421
}
464422

465423
impl<'tcx> CommonTypes<'tcx> {
466-
fn new(
467-
interners: &CtxtInterners<'tcx>,
468-
sess: &Session,
469-
untracked: &Untracked,
470-
) -> CommonTypes<'tcx> {
471-
let mk = |ty| interners.intern_ty(ty, sess, untracked);
424+
fn new(interners: &CtxtInterners<'tcx>) -> CommonTypes<'tcx> {
425+
let mk = |ty| interners.intern_ty(ty);
472426

473427
let ty_vars =
474428
(0..NUM_PREINTERNED_TY_VARS).map(|n| mk(Infer(ty::TyVar(TyVid::from(n))))).collect();
@@ -584,18 +538,8 @@ impl<'tcx> CommonLifetimes<'tcx> {
584538
}
585539

586540
impl<'tcx> CommonConsts<'tcx> {
587-
fn new(
588-
interners: &CtxtInterners<'tcx>,
589-
types: &CommonTypes<'tcx>,
590-
sess: &Session,
591-
untracked: &Untracked,
592-
) -> CommonConsts<'tcx> {
593-
let mk_const = |c| {
594-
interners.intern_const(
595-
c, sess, // This is only used to create a stable hashing context.
596-
untracked,
597-
)
598-
};
541+
fn new(interners: &CtxtInterners<'tcx>, types: &CommonTypes<'tcx>) -> CommonConsts<'tcx> {
542+
let mk_const = |c| interners.intern_const(c);
599543

600544
let mk_valtree = |v| {
601545
ty::ValTree(Interned::new_unchecked(
@@ -1088,9 +1032,9 @@ impl<'tcx> TyCtxt<'tcx> {
10881032
s.dcx().emit_fatal(err);
10891033
});
10901034
let interners = CtxtInterners::new(arena);
1091-
let common_types = CommonTypes::new(&interners, s, &untracked);
1035+
let common_types = CommonTypes::new(&interners);
10921036
let common_lifetimes = CommonLifetimes::new(&interners);
1093-
let common_consts = CommonConsts::new(&interners, &common_types, s, &untracked);
1037+
let common_consts = CommonConsts::new(&interners, &common_types);
10941038

10951039
let gcx = gcx_cell.get_or_init(|| GlobalCtxt {
10961040
sess: s,
@@ -2221,12 +2165,7 @@ impl<'tcx> TyCtxt<'tcx> {
22212165

22222166
#[inline]
22232167
pub fn mk_predicate(self, binder: Binder<'tcx, PredicateKind<'tcx>>) -> Predicate<'tcx> {
2224-
self.interners.intern_predicate(
2225-
binder,
2226-
self.sess,
2227-
// This is only used to create a stable hashing context.
2228-
&self.untracked,
2229-
)
2168+
self.interners.intern_predicate(binder)
22302169
}
22312170

22322171
#[inline]
@@ -2347,24 +2286,14 @@ impl<'tcx> TyCtxt<'tcx> {
23472286

23482287
#[inline]
23492288
pub fn mk_ct_from_kind(self, kind: ty::ConstKind<'tcx>) -> Const<'tcx> {
2350-
self.interners.intern_const(
2351-
kind,
2352-
self.sess,
2353-
// This is only used to create a stable hashing context.
2354-
&self.untracked,
2355-
)
2289+
self.interners.intern_const(kind)
23562290
}
23572291

23582292
// Avoid this in favour of more specific `Ty::new_*` methods, where possible.
23592293
#[allow(rustc::usage_of_ty_tykind)]
23602294
#[inline]
23612295
pub fn mk_ty_from_kind(self, st: TyKind<'tcx>) -> Ty<'tcx> {
2362-
self.interners.intern_ty(
2363-
st,
2364-
self.sess,
2365-
// This is only used to create a stable hashing context.
2366-
&self.untracked,
2367-
)
2296+
self.interners.intern_ty(st)
23682297
}
23692298

23702299
pub fn mk_param_from_def(self, param: &ty::GenericParamDef) -> GenericArg<'tcx> {

compiler/rustc_middle/src/ty/predicate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,6 @@ mod size_asserts {
644644
use super::*;
645645
// tidy-alphabetical-start
646646
static_assert_size!(PredicateKind<'_>, 40);
647-
static_assert_size!(WithCachedTypeInfo<PredicateKind<'_>>, 64);
647+
static_assert_size!(WithCachedTypeInfo<PredicateKind<'_>>, 48);
648648
// tidy-alphabetical-end
649649
}

compiler/rustc_middle/src/ty/region.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,6 @@ mod size_asserts {
460460
use super::*;
461461
// tidy-alphabetical-start
462462
static_assert_size!(RegionKind<'_>, 20);
463-
static_assert_size!(ty::WithCachedTypeInfo<RegionKind<'_>>, 48);
463+
static_assert_size!(ty::WithCachedTypeInfo<RegionKind<'_>>, 28);
464464
// tidy-alphabetical-end
465465
}

compiler/rustc_middle/src/ty/sty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2167,6 +2167,6 @@ mod size_asserts {
21672167
use super::*;
21682168
// tidy-alphabetical-start
21692169
static_assert_size!(TyKind<'_>, 32);
2170-
static_assert_size!(ty::WithCachedTypeInfo<TyKind<'_>>, 56);
2170+
static_assert_size!(ty::WithCachedTypeInfo<TyKind<'_>>, 40);
21712171
// tidy-alphabetical-end
21722172
}

compiler/rustc_type_ir/src/ty_info.rs

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,19 @@ use std::cmp::Ordering;
22
use std::hash::{Hash, Hasher};
33
use std::ops::Deref;
44

5-
#[cfg(feature = "nightly")]
6-
use rustc_data_structures::fingerprint::Fingerprint;
75
#[cfg(feature = "nightly")]
86
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
97
use rustc_type_ir_macros::GenericTypeVisitable;
108

119
use crate::{DebruijnIndex, TypeFlags};
1210

1311
/// A helper type that you can wrap round your own type in order to automatically
14-
/// cache the stable hash, type flags and debruijn index on creation and
15-
/// not recompute it whenever the information is needed.
16-
/// This is only done in incremental mode. You can also opt out of caching by using
17-
/// StableHash::ZERO for the hash, in which case the hash gets computed each time.
18-
/// This is useful if you have values that you intern but never (can?) use for stable
19-
/// hashing.
12+
/// cache the type flags and debruijn index on creation and not recompute it
13+
/// whenever the information is needed.
2014
#[derive(Copy, Clone, GenericTypeVisitable)]
2115
pub struct WithCachedTypeInfo<T> {
2216
pub internee: T,
2317

24-
#[cfg(feature = "nightly")]
25-
pub stable_hash: Fingerprint,
26-
2718
/// This field provides fast access to information that is also contained
2819
/// in `kind`.
2920
///
@@ -87,39 +78,13 @@ impl<T> Deref for WithCachedTypeInfo<T> {
8778
impl<T: Hash> Hash for WithCachedTypeInfo<T> {
8879
#[inline]
8980
fn hash<H: Hasher>(&self, s: &mut H) {
90-
#[cfg(feature = "nightly")]
91-
if self.stable_hash != Fingerprint::ZERO {
92-
return self.stable_hash.hash(s);
93-
}
94-
9581
self.internee.hash(s)
9682
}
9783
}
9884

9985
#[cfg(feature = "nightly")]
10086
impl<T: HashStable<Hcx>, Hcx> HashStable<Hcx> for WithCachedTypeInfo<T> {
10187
fn hash_stable(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
102-
if self.stable_hash == Fingerprint::ZERO || cfg!(debug_assertions) {
103-
// No cached hash available. This can only mean that incremental is disabled.
104-
// We don't cache stable hashes in non-incremental mode, because they are used
105-
// so rarely that the performance actually suffers.
106-
107-
// We need to build the hash as if we cached it and then hash that hash, as
108-
// otherwise the hashes will differ between cached and non-cached mode.
109-
let stable_hash: Fingerprint = {
110-
let mut hasher = StableHasher::new();
111-
self.internee.hash_stable(hcx, &mut hasher);
112-
hasher.finish()
113-
};
114-
if cfg!(debug_assertions) && self.stable_hash != Fingerprint::ZERO {
115-
assert_eq!(
116-
stable_hash, self.stable_hash,
117-
"cached stable hash does not match freshly computed stable hash"
118-
);
119-
}
120-
stable_hash.hash_stable(hcx, hasher);
121-
} else {
122-
self.stable_hash.hash_stable(hcx, hasher);
123-
}
88+
self.internee.hash_stable(hcx, hasher);
12489
}
12590
}

tests/debuginfo/function-names.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
// Const generic parameter
4040
//@ gdb-command:info functions -q function_names::const_generic_fn.*
4141
//@ gdb-check:[...]static fn function_names::const_generic_fn_bool<false>();
42-
//@ gdb-check:[...]static fn function_names::const_generic_fn_non_int<{CONST#6dd80cc0c950c171}>();
42+
//@ gdb-check:[...]static fn function_names::const_generic_fn_non_int<{CONST#68cf9e429c783d36}>();
4343
//@ gdb-check:[...]static fn function_names::const_generic_fn_signed_int<-7>();
4444
//@ gdb-check:[...]static fn function_names::const_generic_fn_unsigned_int<14>();
4545

tests/ui/symbol-names/basic.legacy.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error: symbol-name(_ZN5basic4main17h947b7a9ed2b2bf56E)
1+
error: symbol-name(_ZN5basic4main17h27f62b2d0b2beac6E)
22
--> $DIR/basic.rs:8:1
33
|
44
LL | #[rustc_dump_symbol_name]
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^
66

7-
error: demangling(basic::main::h947b7a9ed2b2bf56)
7+
error: demangling(basic::main::h27f62b2d0b2beac6)
88
--> $DIR/basic.rs:8:1
99
|
1010
LL | #[rustc_dump_symbol_name]

tests/ui/symbol-names/issue-60925.legacy.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error: symbol-name(_ZN11issue_609253foo37Foo$LT$issue_60925..llv$u6d$..Foo$GT$3foo17hba5ac046b858f549E)
1+
error: symbol-name(_ZN11issue_609253foo37Foo$LT$issue_60925..llv$u6d$..Foo$GT$3foo17h1eb769490ff06e77E)
22
--> $DIR/issue-60925.rs:21:9
33
|
44
LL | #[rustc_dump_symbol_name]
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^
66

7-
error: demangling(issue_60925::foo::Foo<issue_60925::llvm::Foo>::foo::hba5ac046b858f549)
7+
error: demangling(issue_60925::foo::Foo<issue_60925::llvm::Foo>::foo::h1eb769490ff06e77)
88
--> $DIR/issue-60925.rs:21:9
99
|
1010
LL | #[rustc_dump_symbol_name]

0 commit comments

Comments
 (0)