Skip to content

Commit 9b70448

Browse files
committed
Auto merge of #143882 - cjgillot:relative-span-file, r=<try>
Also hash spans inside the same file as relative. r? `@ghost` for perf
2 parents 56835d7 + 0dddae1 commit 9b70448

File tree

4 files changed

+78
-51
lines changed

4 files changed

+78
-51
lines changed

compiler/rustc_middle/src/query/on_disk_cache.rs

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const TAG_FULL_SPAN: u8 = 0;
3838
// A partial span with no location information, encoded only with a `SyntaxContext`
3939
const TAG_PARTIAL_SPAN: u8 = 1;
4040
const TAG_RELATIVE_SPAN: u8 = 2;
41+
const TAG_RELATIVE_OUTER_SPAN: u8 = 3;
4142

4243
const TAG_SYNTAX_CONTEXT: u8 = 0;
4344
const TAG_EXPN_DATA: u8 = 1;
@@ -659,6 +660,19 @@ impl<'a, 'tcx> SpanDecoder for CacheDecoder<'a, 'tcx> {
659660
parent,
660661
);
661662

663+
return span;
664+
} else if tag == TAG_RELATIVE_OUTER_SPAN {
665+
let dlo = isize::decode(self);
666+
let dto = isize::decode(self);
667+
668+
let enclosing = self.tcx.source_span_untracked(parent.unwrap()).data_untracked();
669+
let span = Span::new(
670+
BytePos::from_usize((enclosing.lo.to_u32() as isize + dlo) as usize),
671+
BytePos::from_usize((enclosing.lo.to_u32() as isize + dto) as usize),
672+
ctxt,
673+
parent,
674+
);
675+
662676
return span;
663677
} else {
664678
debug_assert_eq!(tag, TAG_FULL_SPAN);
@@ -897,30 +911,33 @@ impl<'a, 'tcx> SpanEncoder for CacheEncoder<'a, 'tcx> {
897911
return TAG_PARTIAL_SPAN.encode(self);
898912
}
899913

900-
if let Some(parent) = span_data.parent {
901-
let enclosing = self.tcx.source_span_untracked(parent).data_untracked();
902-
if enclosing.contains(span_data) {
903-
TAG_RELATIVE_SPAN.encode(self);
904-
(span_data.lo - enclosing.lo).to_u32().encode(self);
905-
(span_data.hi - enclosing.lo).to_u32().encode(self);
906-
return;
907-
}
914+
let parent =
915+
span_data.parent.map(|parent| self.tcx.source_span_untracked(parent).data_untracked());
916+
if let Some(parent) = parent
917+
&& parent.contains(span_data)
918+
{
919+
TAG_RELATIVE_SPAN.encode(self);
920+
(span_data.lo - parent.lo).to_u32().encode(self);
921+
(span_data.hi - parent.lo).to_u32().encode(self);
922+
return;
908923
}
909924

910-
let pos = self.source_map.byte_pos_to_line_and_col(span_data.lo);
911-
let partial_span = match &pos {
912-
Some((file_lo, _, _)) => !file_lo.contains(span_data.hi),
913-
None => true,
925+
let Some((file_lo, line_lo, col_lo)) =
926+
self.source_map.byte_pos_to_line_and_col(span_data.lo)
927+
else {
928+
return TAG_PARTIAL_SPAN.encode(self);
914929
};
915930

916-
if partial_span {
917-
return TAG_PARTIAL_SPAN.encode(self);
931+
if let Some(parent) = parent
932+
&& file_lo.contains(parent.lo)
933+
{
934+
TAG_RELATIVE_OUTER_SPAN.encode(self);
935+
(span_data.lo.to_u32() as isize - parent.lo.to_u32() as isize).encode(self);
936+
(span_data.hi.to_u32() as isize - parent.lo.to_u32() as isize).encode(self);
937+
return;
918938
}
919939

920-
let (file_lo, line_lo, col_lo) = pos.unwrap();
921-
922940
let len = span_data.hi - span_data.lo;
923-
924941
let source_file_index = self.source_file_index(file_lo);
925942

926943
TAG_FULL_SPAN.encode(self);

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: 17 additions & 19 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.
@@ -123,27 +123,25 @@ impl<'sm> CachingSourceMapView<'sm> {
123123

124124
if lo_cache_idx != -1 && hi_cache_idx != -1 {
125125
// Cache hit for span lo and hi. Check if they belong to the same file.
126-
let result = {
127-
let lo = &self.line_cache[lo_cache_idx as usize];
128-
let hi = &self.line_cache[hi_cache_idx as usize];
126+
let lo_file_index = self.line_cache[lo_cache_idx as usize].file_index;
127+
let hi_file_index = self.line_cache[hi_cache_idx as usize].file_index;
129128

130-
if lo.file_index != hi.file_index {
131-
return None;
132-
}
133-
134-
(
135-
lo.file.stable_id,
136-
lo.line_number,
137-
span_data.lo - lo.line.start,
138-
hi.line_number,
139-
span_data.hi - hi.line.start,
140-
)
141-
};
129+
if lo_file_index != hi_file_index {
130+
return None;
131+
}
142132

143133
self.line_cache[lo_cache_idx as usize].touch(self.time_stamp);
144134
self.line_cache[hi_cache_idx as usize].touch(self.time_stamp);
145135

146-
return Some(result);
136+
let lo = &self.line_cache[lo_cache_idx as usize];
137+
let hi = &self.line_cache[hi_cache_idx as usize];
138+
return Some((
139+
&*lo.file,
140+
lo.line_number,
141+
span_data.lo - lo.line.start,
142+
hi.line_number,
143+
span_data.hi - hi.line.start,
144+
));
147145
}
148146

149147
// No cache hit or cache hit for only one of span lo and hi.
@@ -226,7 +224,7 @@ impl<'sm> CachingSourceMapView<'sm> {
226224
assert_eq!(lo.file_index, hi.file_index);
227225

228226
Some((
229-
lo.file.stable_id,
227+
&*lo.file,
230228
lo.line_number,
231229
span_data.lo - lo.line.start,
232230
hi.line_number,

compiler/rustc_span/src/lib.rs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2623,7 +2623,7 @@ pub trait HashStableContext {
26232623
fn span_data_to_lines_and_cols(
26242624
&mut self,
26252625
span: &SpanData,
2626-
) -> Option<(StableSourceFileId, usize, BytePos, usize, BytePos)>;
2626+
) -> Option<(&SourceFile, usize, BytePos, usize, BytePos)>;
26272627
fn hashing_controls(&self) -> HashingControls;
26282628
}
26292629

@@ -2641,6 +2641,7 @@ where
26412641
/// codepoint offsets. For the purpose of the hash that's sufficient.
26422642
/// Also, hashing filenames is expensive so we avoid doing it twice when the
26432643
/// span starts and ends in the same file, which is almost always the case.
2644+
// Important: changes to this method should be reflected in implementations of `SpanEncoder`.
26442645
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
26452646
const TAG_VALID_SPAN: u8 = 0;
26462647
const TAG_INVALID_SPAN: u8 = 1;
@@ -2659,15 +2660,15 @@ where
26592660
return;
26602661
}
26612662

2662-
if let Some(parent) = span.parent {
2663-
let def_span = ctx.def_span(parent).data_untracked();
2664-
if def_span.contains(span) {
2665-
// This span is enclosed in a definition: only hash the relative position.
2666-
Hash::hash(&TAG_RELATIVE_SPAN, hasher);
2667-
(span.lo - def_span.lo).to_u32().hash_stable(ctx, hasher);
2668-
(span.hi - def_span.lo).to_u32().hash_stable(ctx, hasher);
2669-
return;
2670-
}
2663+
let parent = span.parent.map(|parent| ctx.def_span(parent).data_untracked());
2664+
if let Some(parent) = parent
2665+
&& parent.contains(span)
2666+
{
2667+
// This span is enclosed in a definition: only hash the relative position.
2668+
Hash::hash(&TAG_RELATIVE_SPAN, hasher);
2669+
Hash::hash(&(span.lo - parent.lo), hasher);
2670+
Hash::hash(&(span.hi - parent.lo), hasher);
2671+
return;
26712672
}
26722673

26732674
// If this is not an empty or invalid span, we want to hash the last
@@ -2680,7 +2681,20 @@ where
26802681
};
26812682

26822683
Hash::hash(&TAG_VALID_SPAN, hasher);
2683-
Hash::hash(&file, hasher);
2684+
Hash::hash(&file.stable_id, hasher);
2685+
2686+
if let Some(parent) = parent
2687+
&& file.contains(parent.lo)
2688+
{
2689+
// This span is relative to another span in the same file,
2690+
// only hash the relative position.
2691+
Hash::hash(&TAG_RELATIVE_SPAN, hasher);
2692+
// Use signed difference as `span` may start before `parent`,
2693+
// for instance attributes start before their item's span.
2694+
Hash::hash(&(span.lo.to_u32() as isize - parent.lo.to_u32() as isize), hasher);
2695+
Hash::hash(&(span.hi.to_u32() as isize - parent.lo.to_u32() as isize), hasher);
2696+
return;
2697+
}
26842698

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

0 commit comments

Comments
 (0)