Skip to content

Commit f38bdb4

Browse files
committed
perf: skip span hashing in non-incremental builds
1 parent 3659db0 commit f38bdb4

3 files changed

Lines changed: 22 additions & 12 deletions

File tree

compiler/rustc_middle/src/ich.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,25 @@ enum CachingSourceMap<'a> {
2222
/// things (e.g., each `DefId`/`DefPath` is only hashed once).
2323
pub struct StableHashState<'a> {
2424
untracked: &'a Untracked,
25-
// The value of `-Z incremental-ignore-spans`.
26-
// This field should only be used by `unstable_opts_incremental_ignore_span`
27-
incremental_ignore_spans: bool,
25+
// The session-wide default for `hash_spans`, computed in `Self::new`. Only
26+
// used by `assert_default_stable_hash_controls`.
27+
default_hash_spans: bool,
2828
caching_source_map: CachingSourceMap<'a>,
2929
stable_hash_controls: StableHashControls,
3030
}
3131

3232
impl<'a> StableHashState<'a> {
3333
#[inline]
3434
pub fn new(sess: &'a Session, untracked: &'a Untracked) -> Self {
35-
let hash_spans_initial = !sess.opts.unstable_opts.incremental_ignore_spans;
35+
// Only hash spans for incremental and coverage, where hashes must react to
36+
// code moving around; otherwise skip them and their source map lookups.
37+
// Spans are also skipped if `-Z incremental-ignore-spans` is set.
38+
let hash_spans_initial = (sess.opts.incremental.is_some() || sess.instrument_coverage())
39+
&& !sess.opts.unstable_opts.incremental_ignore_spans;
3640

3741
StableHashState {
3842
untracked,
39-
incremental_ignore_spans: sess.opts.unstable_opts.incremental_ignore_spans,
43+
default_hash_spans: hash_spans_initial,
4044
caching_source_map: CachingSourceMap::Unused(sess.source_map()),
4145
stable_hash_controls: StableHashControls { hash_spans: hash_spans_initial },
4246
}
@@ -178,15 +182,15 @@ impl<'a> StableHashCtxt for StableHashState<'a> {
178182
let stable_hash_controls = self.stable_hash_controls;
179183
let StableHashControls { hash_spans } = stable_hash_controls;
180184

181-
// Note that we require that `hash_spans` be the inverse of the global `-Z
182-
// incremental-ignore-spans` option. Normally, this option is disabled, in which case
183-
// `hash_spans` must be true.
185+
// Note that we require that `hash_spans` matches the session-wide default computed in
186+
// `StableHashState::new` from the compilation mode and the global `-Z
187+
// incremental-ignore-spans` option.
184188
//
185189
// Span hashing can also be disabled without `-Z incremental-ignore-spans`. This is the
186190
// case for instance when building a hash for name mangling. Such configuration must not be
187191
// used for metadata.
188192
assert_eq!(
189-
hash_spans, !self.incremental_ignore_spans,
193+
hash_spans, self.default_hash_spans,
190194
"Attempted hashing of {msg} with non-default StableHashControls: {stable_hash_controls:?}"
191195
);
192196
}

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,12 @@ fn encode_query_values_inner<'a, 'tcx, C, V>(
100100
}
101101

102102
pub(crate) fn verify_query_key_hashes<'tcx>(tcx: TyCtxt<'tcx>) {
103-
if tcx.sess.opts.unstable_opts.incremental_verify_ich || cfg!(debug_assertions) {
103+
// Only run this if the dep graph is enabled. Otherwise there are no `DepNode`s
104+
// to collide, and non-incremental builds skip span hashing, so keys differing
105+
// only in spans collide by design.
106+
if tcx.dep_graph.is_fully_enabled()
107+
&& (tcx.sess.opts.unstable_opts.incremental_verify_ich || cfg!(debug_assertions))
108+
{
104109
tcx.sess.time("verify_query_key_hashes", || {
105110
for_each_query_vtable!(ALL, tcx, |query| {
106111
verify_query_key_hashes_inner(query, tcx);

compiler/rustc_span/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1983,8 +1983,9 @@ impl<S: SpanEncoder> Encodable<S> for SourceFile {
19831983
self.normalized_source_len.encode(s);
19841984
self.unnormalized_source_len.encode(s);
19851985

1986-
// We are always in `Lines` form by the time we reach here.
1987-
assert!(self.lines.read().is_lines());
1986+
// Imported files may still be in compressed `Diffs` form if nothing needed
1987+
// their line table (non-incremental builds do not force the conversion by
1988+
// hashing spans). `lines()` converts on demand.
19881989
let lines = self.lines();
19891990
// Store the length.
19901991
s.emit_u32(lines.len() as u32);

0 commit comments

Comments
 (0)