Skip to content

Commit 741e208

Browse files
Encode SourceFile source crate as StableCrateId in incr. comp. OnDiskCache.
1 parent 68c3b0e commit 741e208

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

compiler/rustc_middle/src/ty/query/on_disk_cache.rs

+33-5
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub struct OnDiskCache<'sess> {
5454
cnum_map: OnceCell<UnhashMap<StableCrateId, CrateNum>>,
5555

5656
source_map: &'sess SourceMap,
57-
file_index_to_stable_id: FxHashMap<SourceFileIndex, StableSourceFileId>,
57+
file_index_to_stable_id: FxHashMap<SourceFileIndex, EncodedSourceFileId>,
5858

5959
// Caches that are populated lazily during decoding.
6060
file_index_to_file: Lock<FxHashMap<SourceFileIndex, Lrc<SourceFile>>>,
@@ -111,7 +111,7 @@ pub struct OnDiskCache<'sess> {
111111
// This type is used only for serialization and deserialization.
112112
#[derive(Encodable, Decodable)]
113113
struct Footer {
114-
file_index_to_stable_id: FxHashMap<SourceFileIndex, StableSourceFileId>,
114+
file_index_to_stable_id: FxHashMap<SourceFileIndex, EncodedSourceFileId>,
115115
query_result_index: EncodedQueryResultIndex,
116116
diagnostics_index: EncodedQueryResultIndex,
117117
// The location of all allocations.
@@ -157,6 +157,32 @@ crate struct RawDefId {
157157
pub index: u32,
158158
}
159159

160+
/// An `EncodedSourceFileId` is the same as a `StableSourceFileId` except that
161+
/// the source crate is represented as a [StableCrateId] instead of as a
162+
/// `CrateNum`. This way `EncodedSourceFileId` can be encoded and decoded
163+
/// without any additional context, i.e. with a simple `opaque::Decoder` (which
164+
/// is the only thing available when decoding the cache's [Footer].
165+
#[derive(Encodable, Decodable, Clone, Debug)]
166+
struct EncodedSourceFileId {
167+
file_name_hash: u64,
168+
stable_crate_id: StableCrateId,
169+
}
170+
171+
impl EncodedSourceFileId {
172+
fn translate(&self, cnum_map: &UnhashMap<StableCrateId, CrateNum>) -> StableSourceFileId {
173+
let cnum = cnum_map[&self.stable_crate_id];
174+
StableSourceFileId { file_name_hash: self.file_name_hash, cnum }
175+
}
176+
177+
fn new(tcx: TyCtxt<'_>, file: &SourceFile) -> EncodedSourceFileId {
178+
let source_file_id = StableSourceFileId::new(file);
179+
EncodedSourceFileId {
180+
file_name_hash: source_file_id.file_name_hash,
181+
stable_crate_id: tcx.stable_crate_id(source_file_id.cnum),
182+
}
183+
}
184+
}
185+
160186
impl<'sess> OnDiskCache<'sess> {
161187
/// Creates a new `OnDiskCache` instance from the serialized data in `data`.
162188
pub fn new(sess: &'sess Session, data: Vec<u8>, start_pos: usize) -> Self {
@@ -238,7 +264,8 @@ impl<'sess> OnDiskCache<'sess> {
238264
let index = SourceFileIndex(index as u32);
239265
let file_ptr: *const SourceFile = &**file as *const _;
240266
file_to_file_index.insert(file_ptr, index);
241-
file_index_to_stable_id.insert(index, StableSourceFileId::new(&file));
267+
let source_file_id = EncodedSourceFileId::new(tcx, &file);
268+
file_index_to_stable_id.insert(index, source_file_id);
242269
}
243270

244271
(file_to_file_index, file_index_to_stable_id)
@@ -605,7 +632,7 @@ pub struct CacheDecoder<'a, 'tcx> {
605632
source_map: &'a SourceMap,
606633
cnum_map: &'a UnhashMap<StableCrateId, CrateNum>,
607634
file_index_to_file: &'a Lock<FxHashMap<SourceFileIndex, Lrc<SourceFile>>>,
608-
file_index_to_stable_id: &'a FxHashMap<SourceFileIndex, StableSourceFileId>,
635+
file_index_to_stable_id: &'a FxHashMap<SourceFileIndex, EncodedSourceFileId>,
609636
alloc_decoding_session: AllocDecodingSession<'a>,
610637
syntax_contexts: &'a FxHashMap<u32, AbsoluteBytePos>,
611638
expn_data: &'a FxHashMap<u32, AbsoluteBytePos>,
@@ -618,14 +645,15 @@ impl<'a, 'tcx> CacheDecoder<'a, 'tcx> {
618645
ref file_index_to_file,
619646
ref file_index_to_stable_id,
620647
ref source_map,
648+
ref cnum_map,
621649
..
622650
} = *self;
623651

624652
file_index_to_file
625653
.borrow_mut()
626654
.entry(index)
627655
.or_insert_with(|| {
628-
let stable_id = file_index_to_stable_id[&index];
656+
let stable_id = file_index_to_stable_id[&index].translate(cnum_map);
629657
source_map
630658
.source_file_by_stable_id(stable_id)
631659
.expect("failed to lookup `SourceFile` in new context")

compiler/rustc_span/src/source_map.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,13 @@ impl FileLoader for RealFileLoader {
133133
pub struct StableSourceFileId {
134134
// A hash of the source file's FileName. This is hash so that it's size
135135
// is more predictable than if we included the actual FileName value.
136-
file_name_hash: u64,
136+
pub file_name_hash: u64,
137137

138138
// The CrateNum of the crate this source file was originally parsed for.
139139
// We cannot include this information in the hash because at the time
140140
// of hashing we don't have the context to map from the CrateNum's numeric
141141
// value to a StableCrateId.
142-
cnum: CrateNum,
142+
pub cnum: CrateNum,
143143
}
144144

145145
// FIXME: we need a more globally consistent approach to the problem solved by

0 commit comments

Comments
 (0)