Skip to content

Commit

Permalink
Auto merge of #97575 - nnethercote:lazify-SourceFile-lines, r=Mark-Si…
Browse files Browse the repository at this point in the history
…mulacrum

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`
  • Loading branch information
bors committed Jun 2, 2022
2 parents 44e9516 + 72de7c4 commit e714405
Show file tree
Hide file tree
Showing 8 changed files with 239 additions and 123 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_query_impl/src/on_disk_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for Span {
let len = BytePos::decode(decoder);

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

Span::new(lo, hi, ctxt, parent)
Expand Down
16 changes: 10 additions & 6 deletions compiler/rustc_query_system/src/ich/impls_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl<'a> HashStable<StableHashingContext<'a>> for SourceFile {
external_src: _,
start_pos,
end_pos: _,
ref lines,
lines: _,
ref multibyte_chars,
ref non_narrow_chars,
ref normalized_pos,
Expand All @@ -79,11 +79,15 @@ impl<'a> HashStable<StableHashingContext<'a>> for SourceFile {

src_hash.hash_stable(hcx, hasher);

// We only hash the relative position within this source_file
lines.len().hash_stable(hcx, hasher);
for &line in lines.iter() {
stable_byte_pos(line, start_pos).hash_stable(hcx, hasher);
}
// We are always in `Lines` form by the time we reach here.
assert!(self.lines.borrow().is_lines());
self.lines(|lines| {
// We only hash the relative position within this source_file
lines.len().hash_stable(hcx, hasher);
for &line in lines.iter() {
stable_byte_pos(line, start_pos).hash_stable(hcx, hasher);
}
});

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

0 comments on commit e714405

Please sign in to comment.