Skip to content

Commit e714405

Browse files
committed
Auto merge of #97575 - nnethercote:lazify-SourceFile-lines, r=Mark-Simulacrum
Lazify `SourceFile::lines`. `SourceFile::lines` is a big part of metadata. It's stored in a compressed form (a difference list) to save disk space. Decoding it is a big fraction of compile time for very small crates/programs. This commit introduces a new type `SourceFileLines` which has a `Lines` form and a `Diffs` form. The latter is used when the metadata is first read, and it is only decoded into the `Lines` form when line data is actually needed. This avoids the decoding cost for many files, especially in `std`. It's a performance win of up to 15% for tiny crates/programs where metadata decoding is a high part of compilation costs. A `RefCell` is needed because the methods that access lines data (which can trigger decoding) take `&self` rather than `&mut self`. To allow for this, `SourceFile::lines` now takes a `FnMut` that operates on the lines slice rather than returning the lines slice. r? `@Mark-Simulacrum`
2 parents 44e9516 + 72de7c4 commit e714405

File tree

8 files changed

+239
-123
lines changed

8 files changed

+239
-123
lines changed

compiler/rustc_query_impl/src/on_disk_cache.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for Span {
713713
let len = BytePos::decode(decoder);
714714

715715
let file_lo = decoder.file_index_to_file(file_lo_index);
716-
let lo = file_lo.lines[line_lo - 1] + col_lo;
716+
let lo = file_lo.lines(|lines| lines[line_lo - 1] + col_lo);
717717
let hi = lo + len;
718718

719719
Span::new(lo, hi, ctxt, parent)

compiler/rustc_query_system/src/ich/impls_syntax.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl<'a> HashStable<StableHashingContext<'a>> for SourceFile {
6969
external_src: _,
7070
start_pos,
7171
end_pos: _,
72-
ref lines,
72+
lines: _,
7373
ref multibyte_chars,
7474
ref non_narrow_chars,
7575
ref normalized_pos,
@@ -79,11 +79,15 @@ impl<'a> HashStable<StableHashingContext<'a>> for SourceFile {
7979

8080
src_hash.hash_stable(hcx, hasher);
8181

82-
// We only hash the relative position within this source_file
83-
lines.len().hash_stable(hcx, hasher);
84-
for &line in lines.iter() {
85-
stable_byte_pos(line, start_pos).hash_stable(hcx, hasher);
86-
}
82+
// We are always in `Lines` form by the time we reach here.
83+
assert!(self.lines.borrow().is_lines());
84+
self.lines(|lines| {
85+
// We only hash the relative position within this source_file
86+
lines.len().hash_stable(hcx, hasher);
87+
for &line in lines.iter() {
88+
stable_byte_pos(line, start_pos).hash_stable(hcx, hasher);
89+
}
90+
});
8791

8892
// We only hash the relative position within this source_file
8993
multibyte_chars.len().hash_stable(hcx, hasher);

0 commit comments

Comments
 (0)