Skip to content

Simplify span caches #145505

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 23 additions & 28 deletions compiler/rustc_middle/src/query/on_disk_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,34 +645,29 @@ impl<'a, 'tcx> SpanDecoder for CacheDecoder<'a, 'tcx> {
let parent = Option::<LocalDefId>::decode(self);
let tag: u8 = Decodable::decode(self);

if tag == TAG_PARTIAL_SPAN {
return Span::new(BytePos(0), BytePos(0), ctxt, parent);
} else if tag == TAG_RELATIVE_SPAN {
let dlo = u32::decode(self);
let dto = u32::decode(self);

let enclosing = self.tcx.source_span_untracked(parent.unwrap()).data_untracked();
let span = Span::new(
enclosing.lo + BytePos::from_u32(dlo),
enclosing.lo + BytePos::from_u32(dto),
ctxt,
parent,
);

return span;
} else {
debug_assert_eq!(tag, TAG_FULL_SPAN);
}

let file_lo_index = SourceFileIndex::decode(self);
let line_lo = usize::decode(self);
let col_lo = RelativeBytePos::decode(self);
let len = BytePos::decode(self);

let file_lo = self.file_index_to_file(file_lo_index);
let lo = file_lo.lines()[line_lo - 1] + col_lo;
let lo = file_lo.absolute_position(lo);
let hi = lo + len;
let (lo, hi) = match tag {
TAG_PARTIAL_SPAN => (BytePos(0), BytePos(0)),
TAG_RELATIVE_SPAN => {
let dlo = u32::decode(self);
let dto = u32::decode(self);

let enclosing = self.tcx.source_span_untracked(parent.unwrap()).data_untracked();
(enclosing.lo + BytePos::from_u32(dlo), enclosing.lo + BytePos::from_u32(dto))
}
TAG_FULL_SPAN => {
let file_lo_index = SourceFileIndex::decode(self);
let line_lo = usize::decode(self);
let col_lo = RelativeBytePos::decode(self);
let len = BytePos::decode(self);

let file_lo = self.file_index_to_file(file_lo_index);
let lo = file_lo.lines()[line_lo - 1] + col_lo;
let lo = file_lo.absolute_position(lo);
let hi = lo + len;
(lo, hi)
}
_ => unreachable!(),
};

Span::new(lo, hi, ctxt, parent)
}
Expand Down
30 changes: 14 additions & 16 deletions compiler/rustc_span/src/caching_source_map_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,27 +123,25 @@ impl<'sm> CachingSourceMapView<'sm> {

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

if lo.file_index != hi.file_index {
return None;
}

(
lo.file.stable_id,
lo.line_number,
span_data.lo - lo.line.start,
hi.line_number,
span_data.hi - hi.line.start,
)
};
if lo_file_index != hi_file_index {
return None;
}

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

return Some(result);
let lo = &self.line_cache[lo_cache_idx as usize];
let hi = &self.line_cache[hi_cache_idx as usize];
return Some((
lo.file.stable_id,
lo.line_number,
span_data.lo - lo.line.start,
hi.line_number,
span_data.hi - hi.line.start,
));
}

// No cache hit or cache hit for only one of span lo and hi.
Expand Down
Loading