about summary refs log tree commit diff
path: root/compiler/rustc_query_system
diff options
context:
space:
mode:
authorNicholas Nethercote <n.nethercote@gmail.com>2022-05-30 15:59:45 +1000
committerNicholas Nethercote <n.nethercote@gmail.com>2022-06-01 10:36:39 +1000
commit0b81d7cdc653f540e9fa219820928184e0273f5f (patch)
treee76c89dfae58c1794785b639cd5553627918cf77 /compiler/rustc_query_system
parentbef2b7cd1c7bcb3393f10d5752fcf9ee3026bce8 (diff)
downloadrust-0b81d7cdc653f540e9fa219820928184e0273f5f.tar.gz
rust-0b81d7cdc653f540e9fa219820928184e0273f5f.zip
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 `Lock` 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.
Diffstat (limited to 'compiler/rustc_query_system')
-rw-r--r--compiler/rustc_query_system/src/ich/impls_syntax.rs17
1 files changed, 12 insertions, 5 deletions
diff --git a/compiler/rustc_query_system/src/ich/impls_syntax.rs b/compiler/rustc_query_system/src/ich/impls_syntax.rs
index acf2990b643..4efedafc155 100644
--- a/compiler/rustc_query_system/src/ich/impls_syntax.rs
+++ b/compiler/rustc_query_system/src/ich/impls_syntax.rs
@@ -5,7 +5,7 @@ use crate::ich::StableHashingContext;
 
 use rustc_ast as ast;
 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
-use rustc_span::{BytePos, NormalizedPos, SourceFile};
+use rustc_span::{BytePos, NormalizedPos, SourceFile, SourceFileLines};
 use std::assert_matches::assert_matches;
 
 use smallvec::SmallVec;
@@ -60,7 +60,7 @@ impl<'ctx> rustc_ast::HashStableContext for StableHashingContext<'ctx> {
 impl<'a> HashStable<StableHashingContext<'a>> for SourceFile {
     fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
         let SourceFile {
-            name: _, // We hash the smaller name_hash instead of this
+            ref name, // We hash the smaller name_hash instead of this
             name_hash,
             cnum,
             // Do not hash the source as it is not encoded
@@ -80,9 +80,16 @@ 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);
+        match &*lines.borrow() {
+            SourceFileLines::Lines { lines } => {
+                lines.len().hash_stable(hcx, hasher);
+                for &line in lines.iter() {
+                    stable_byte_pos(line, start_pos).hash_stable(hcx, hasher);
+                }
+            }
+            SourceFileLines::Diffs { .. } => {
+                panic!("called hash_stable on SourceFileLines::Diffs for {:?}", name);
+            }
         }
 
         // We only hash the relative position within this source_file