Skip to content

Commit 721c4cd

Browse files
Auto merge of #150540 - JonathanBrouwer:incremental_test, r=<try>
Improve relative hashing of spans
2 parents 2848c2e + 5a3b9bc commit 721c4cd

4 files changed

Lines changed: 39 additions & 42 deletions

File tree

compiler/rustc_middle/src/query/on_disk_cache.rs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ use rustc_span::hygiene::{
2020
};
2121
use rustc_span::source_map::Spanned;
2222
use rustc_span::{
23-
BlobDecoder, BytePos, ByteSymbol, CachingSourceMapView, ExpnData, ExpnHash, Pos,
24-
RelativeBytePos, SourceFile, Span, SpanDecoder, SpanEncoder, StableSourceFileId, Symbol,
23+
BlobDecoder, BytePos, ByteSymbol, CachingSourceMapView, ExpnData, ExpnHash, RelativeBytePos,
24+
SourceFile, Span, SpanDecoder, SpanEncoder, StableSourceFileId, Symbol,
2525
};
2626

2727
use crate::dep_graph::{DepNodeIndex, SerializedDepNodeIndex};
@@ -652,7 +652,10 @@ impl<'a, 'tcx> SpanDecoder for CacheDecoder<'a, 'tcx> {
652652
let dto = u32::decode(self);
653653

654654
let enclosing = self.tcx.source_span_untracked(parent.unwrap()).data_untracked();
655-
(enclosing.lo + BytePos::from_u32(dlo), enclosing.lo + BytePos::from_u32(dto))
655+
(
656+
BytePos(enclosing.lo.0.wrapping_add(dlo)),
657+
BytePos(enclosing.lo.0.wrapping_add(dto)),
658+
)
656659
}
657660
TAG_FULL_SPAN => {
658661
let file_lo_index = SourceFileIndex::decode(self);
@@ -894,28 +897,23 @@ impl<'a, 'tcx> SpanEncoder for CacheEncoder<'a, 'tcx> {
894897
return TAG_PARTIAL_SPAN.encode(self);
895898
}
896899

897-
if let Some(parent) = span_data.parent {
898-
let enclosing = self.tcx.source_span_untracked(parent).data_untracked();
899-
if enclosing.contains(span_data) {
900-
TAG_RELATIVE_SPAN.encode(self);
901-
(span_data.lo - enclosing.lo).to_u32().encode(self);
902-
(span_data.hi - enclosing.lo).to_u32().encode(self);
903-
return;
904-
}
905-
}
906-
907-
let pos = self.source_map.byte_pos_to_line_and_col(span_data.lo);
908-
let partial_span = match &pos {
909-
Some((file_lo, _, _)) => !file_lo.contains(span_data.hi),
910-
None => true,
900+
let parent =
901+
span_data.parent.map(|parent| self.tcx.source_span_untracked(parent).data_untracked());
902+
let Some((file_lo, line_lo, col_lo)) =
903+
self.source_map.byte_pos_to_line_and_col(span_data.lo)
904+
else {
905+
return TAG_PARTIAL_SPAN.encode(self);
911906
};
912907

913-
if partial_span {
914-
return TAG_PARTIAL_SPAN.encode(self);
908+
if let Some(parent) = parent
909+
&& file_lo.contains(parent.lo)
910+
{
911+
TAG_RELATIVE_SPAN.encode(self);
912+
(span_data.lo.0.wrapping_sub(parent.lo.0)).encode(self);
913+
(span_data.hi.0.wrapping_sub(parent.lo.0)).encode(self);
914+
return;
915915
}
916916

917-
let (file_lo, line_lo, col_lo) = pos.unwrap();
918-
919917
let len = span_data.hi - span_data.lo;
920918

921919
let source_file_index = self.source_file_index(file_lo);

compiler/rustc_query_system/src/ich/hcx.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ use rustc_hir::definitions::DefPathHash;
55
use rustc_session::Session;
66
use rustc_session::cstore::Untracked;
77
use rustc_span::source_map::SourceMap;
8-
use rustc_span::{
9-
BytePos, CachingSourceMapView, DUMMY_SP, Span, SpanData, StableSourceFileId, Symbol,
10-
};
8+
use rustc_span::{BytePos, CachingSourceMapView, DUMMY_SP, SourceFile, Span, SpanData, Symbol};
119

1210
use crate::ich;
1311

@@ -118,7 +116,7 @@ impl<'a> rustc_span::HashStableContext for StableHashingContext<'a> {
118116
fn span_data_to_lines_and_cols(
119117
&mut self,
120118
span: &SpanData,
121-
) -> Option<(StableSourceFileId, usize, BytePos, usize, BytePos)> {
119+
) -> Option<(&SourceFile, usize, BytePos, usize, BytePos)> {
122120
self.source_map().span_data_to_lines_and_cols(span)
123121
}
124122

compiler/rustc_span/src/caching_source_map_view.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::ops::Range;
22
use std::sync::Arc;
33

44
use crate::source_map::SourceMap;
5-
use crate::{BytePos, Pos, RelativeBytePos, SourceFile, SpanData, StableSourceFileId};
5+
use crate::{BytePos, Pos, RelativeBytePos, SourceFile, SpanData};
66

77
#[derive(Clone)]
88
struct CacheEntry {
@@ -114,7 +114,7 @@ impl<'sm> CachingSourceMapView<'sm> {
114114
pub fn span_data_to_lines_and_cols(
115115
&mut self,
116116
span_data: &SpanData,
117-
) -> Option<(StableSourceFileId, usize, BytePos, usize, BytePos)> {
117+
) -> Option<(&SourceFile, usize, BytePos, usize, BytePos)> {
118118
self.time_stamp += 1;
119119

120120
// Check if lo and hi are in the cached lines.
@@ -136,7 +136,7 @@ impl<'sm> CachingSourceMapView<'sm> {
136136
let lo = &self.line_cache[lo_cache_idx as usize];
137137
let hi = &self.line_cache[hi_cache_idx as usize];
138138
return Some((
139-
lo.file.stable_id,
139+
&*lo.file,
140140
lo.line_number,
141141
span_data.lo - lo.line.start,
142142
hi.line_number,
@@ -224,7 +224,7 @@ impl<'sm> CachingSourceMapView<'sm> {
224224
assert_eq!(lo.file_index, hi.file_index);
225225

226226
Some((
227-
lo.file.stable_id,
227+
&*lo.file,
228228
lo.line_number,
229229
span_data.lo - lo.line.start,
230230
hi.line_number,

compiler/rustc_span/src/lib.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2831,7 +2831,7 @@ pub trait HashStableContext {
28312831
fn span_data_to_lines_and_cols(
28322832
&mut self,
28332833
span: &SpanData,
2834-
) -> Option<(StableSourceFileId, usize, BytePos, usize, BytePos)>;
2834+
) -> Option<(&SourceFile, usize, BytePos, usize, BytePos)>;
28352835
fn hashing_controls(&self) -> HashingControls;
28362836
}
28372837

@@ -2849,6 +2849,8 @@ where
28492849
/// codepoint offsets. For the purpose of the hash that's sufficient.
28502850
/// Also, hashing filenames is expensive so we avoid doing it twice when the
28512851
/// span starts and ends in the same file, which is almost always the case.
2852+
///
2853+
/// IMPORTANT: changes to this method should be reflected in implementations of `SpanEncoder` and `SpanDecoder`.
28522854
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
28532855
const TAG_VALID_SPAN: u8 = 0;
28542856
const TAG_INVALID_SPAN: u8 = 1;
@@ -2867,28 +2869,27 @@ where
28672869
return;
28682870
}
28692871

2870-
if let Some(parent) = span.parent {
2871-
let def_span = ctx.def_span(parent).data_untracked();
2872-
if def_span.contains(span) {
2873-
// This span is enclosed in a definition: only hash the relative position.
2874-
Hash::hash(&TAG_RELATIVE_SPAN, hasher);
2875-
(span.lo - def_span.lo).to_u32().hash_stable(ctx, hasher);
2876-
(span.hi - def_span.lo).to_u32().hash_stable(ctx, hasher);
2877-
return;
2878-
}
2879-
}
2880-
28812872
// If this is not an empty or invalid span, we want to hash the last
28822873
// position that belongs to it, as opposed to hashing the first
28832874
// position past it.
2875+
let parent = span.parent.map(|parent| ctx.def_span(parent).data_untracked());
28842876
let Some((file, line_lo, col_lo, line_hi, col_hi)) = ctx.span_data_to_lines_and_cols(&span)
28852877
else {
28862878
Hash::hash(&TAG_INVALID_SPAN, hasher);
28872879
return;
28882880
};
28892881

2882+
if let Some(parent) = parent
2883+
&& file.contains(parent.lo)
2884+
{
2885+
Hash::hash(&TAG_RELATIVE_SPAN, hasher);
2886+
Hash::hash(&(span.lo.0.wrapping_sub(parent.lo.0)), hasher);
2887+
Hash::hash(&(span.hi.0.wrapping_sub(parent.lo.0)), hasher);
2888+
return;
2889+
}
2890+
28902891
Hash::hash(&TAG_VALID_SPAN, hasher);
2891-
Hash::hash(&file, hasher);
2892+
Hash::hash(&file.stable_id, hasher);
28922893

28932894
// Hash both the length and the end location (line/column) of a span. If we
28942895
// hash only the length, for example, then two otherwise equal spans with

0 commit comments

Comments
 (0)