Skip to content

Commit 5be5149

Browse files
committed
Encode CrateNum using the StableCrateId for incr. comp.
1 parent 47d3875 commit 5be5149

File tree

3 files changed

+42
-71
lines changed

3 files changed

+42
-71
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,10 @@ impl<'a, 'tcx> DecodeContext<'a, 'tcx> {
252252
self.cdata.expect("missing CrateMetadata in DecodeContext")
253253
}
254254

255+
fn map_encoded_cnum_to_current(&self, cnum: CrateNum) -> CrateNum {
256+
if cnum == LOCAL_CRATE { self.cdata().cnum } else { self.cdata().cnum_map[cnum] }
257+
}
258+
255259
fn read_lazy_with_meta<T: ?Sized + LazyMeta>(
256260
&mut self,
257261
meta: T::Meta,
@@ -324,10 +328,6 @@ impl<'a, 'tcx> TyDecoder<'tcx> for DecodeContext<'a, 'tcx> {
324328
r
325329
}
326330

327-
fn map_encoded_cnum_to_current(&self, cnum: CrateNum) -> CrateNum {
328-
if cnum == LOCAL_CRATE { self.cdata().cnum } else { self.cdata().cnum_map[cnum] }
329-
}
330-
331331
fn decode_alloc_id(&mut self) -> Result<rustc_middle::mir::interpret::AllocId, Self::Error> {
332332
if let Some(alloc_decoding_session) = self.alloc_decoding_session {
333333
alloc_decoding_session.decode_alloc_id(self)

compiler/rustc_middle/src/ty/codec.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::mir::{
1515
use crate::ty::subst::SubstsRef;
1616
use crate::ty::{self, List, Ty, TyCtxt};
1717
use rustc_data_structures::fx::FxHashMap;
18-
use rustc_hir::def_id::{CrateNum, DefId};
18+
use rustc_hir::def_id::DefId;
1919
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
2020
use rustc_span::Span;
2121
use std::hash::Hash;
@@ -179,8 +179,6 @@ pub trait TyDecoder<'tcx>: Decoder {
179179
where
180180
F: FnOnce(&mut Self) -> R;
181181

182-
fn map_encoded_cnum_to_current(&self, cnum: CrateNum) -> CrateNum;
183-
184182
fn positioned_at_shorthand(&self) -> bool {
185183
(self.peek_byte() & (SHORTHAND_OFFSET as u8)) != 0
186184
}

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

+37-64
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_data_structures::sync::{HashMapExt, Lock, Lrc, OnceCell};
99
use rustc_data_structures::thin_vec::ThinVec;
1010
use rustc_data_structures::unhash::UnhashMap;
1111
use rustc_errors::Diagnostic;
12-
use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, LOCAL_CRATE};
12+
use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, StableCrateId, LOCAL_CRATE};
1313
use rustc_hir::definitions::DefPathHash;
1414
use rustc_index::vec::{Idx, IndexVec};
1515
use rustc_query_system::dep_graph::DepContext;
@@ -18,7 +18,7 @@ use rustc_serialize::{
1818
opaque::{self, FileEncodeResult, FileEncoder, IntEncodedWithFixedSize},
1919
Decodable, Decoder, Encodable, Encoder,
2020
};
21-
use rustc_session::{CrateDisambiguator, Session};
21+
use rustc_session::Session;
2222
use rustc_span::hygiene::{
2323
ExpnDataDecodeMode, ExpnDataEncodeMode, ExpnId, HygieneDecodeContext, HygieneEncodeContext,
2424
SyntaxContext, SyntaxContextData,
@@ -51,8 +51,7 @@ pub struct OnDiskCache<'sess> {
5151
// session.
5252
current_diagnostics: Lock<FxHashMap<DepNodeIndex, Vec<Diagnostic>>>,
5353

54-
prev_cnums: Vec<(u32, String, CrateDisambiguator)>,
55-
cnum_map: OnceCell<IndexVec<CrateNum, Option<CrateNum>>>,
54+
cnum_map: OnceCell<UnhashMap<StableCrateId, CrateNum>>,
5655

5756
source_map: &'sess SourceMap,
5857
file_index_to_stable_id: FxHashMap<SourceFileIndex, StableSourceFileId>,
@@ -113,7 +112,6 @@ pub struct OnDiskCache<'sess> {
113112
#[derive(Encodable, Decodable)]
114113
struct Footer {
115114
file_index_to_stable_id: FxHashMap<SourceFileIndex, StableSourceFileId>,
116-
prev_cnums: Vec<(u32, String, CrateDisambiguator)>,
117115
query_result_index: EncodedQueryResultIndex,
118116
diagnostics_index: EncodedQueryResultIndex,
119117
// The location of all allocations.
@@ -186,7 +184,6 @@ impl<'sess> OnDiskCache<'sess> {
186184
serialized_data: data,
187185
file_index_to_stable_id: footer.file_index_to_stable_id,
188186
file_index_to_file: Default::default(),
189-
prev_cnums: footer.prev_cnums,
190187
cnum_map: OnceCell::new(),
191188
source_map: sess.source_map(),
192189
current_diagnostics: Default::default(),
@@ -207,7 +204,6 @@ impl<'sess> OnDiskCache<'sess> {
207204
serialized_data: Vec::new(),
208205
file_index_to_stable_id: Default::default(),
209206
file_index_to_file: Default::default(),
210-
prev_cnums: vec![],
211207
cnum_map: OnceCell::new(),
212208
source_map,
213209
current_diagnostics: Default::default(),
@@ -327,16 +323,6 @@ impl<'sess> OnDiskCache<'sess> {
327323
interpret_alloc_index
328324
};
329325

330-
let sorted_cnums = sorted_cnums_including_local_crate(tcx);
331-
let prev_cnums: Vec<_> = sorted_cnums
332-
.iter()
333-
.map(|&cnum| {
334-
let crate_name = tcx.crate_name(cnum).to_string();
335-
let crate_disambiguator = tcx.crate_disambiguator(cnum);
336-
(cnum.as_u32(), crate_name, crate_disambiguator)
337-
})
338-
.collect();
339-
340326
let mut syntax_contexts = FxHashMap::default();
341327
let mut expn_ids = FxHashMap::default();
342328

@@ -368,7 +354,6 @@ impl<'sess> OnDiskCache<'sess> {
368354
TAG_FILE_FOOTER,
369355
&Footer {
370356
file_index_to_stable_id,
371-
prev_cnums,
372357
query_result_index,
373358
diagnostics_index,
374359
interpret_alloc_index,
@@ -385,16 +370,7 @@ impl<'sess> OnDiskCache<'sess> {
385370
// DO NOT WRITE ANYTHING TO THE ENCODER AFTER THIS POINT! The address
386371
// of the footer must be the last thing in the data stream.
387372

388-
return Ok(());
389-
390-
fn sorted_cnums_including_local_crate(tcx: TyCtxt<'_>) -> Vec<CrateNum> {
391-
let mut cnums = vec![LOCAL_CRATE];
392-
cnums.extend_from_slice(tcx.crates());
393-
cnums.sort_unstable();
394-
// Just to be sure...
395-
cnums.dedup();
396-
cnums
397-
}
373+
Ok(())
398374
})
399375
}
400376

@@ -429,12 +405,11 @@ impl<'sess> OnDiskCache<'sess> {
429405
self.foreign_def_path_hashes.get(hash).copied()
430406
}
431407

432-
fn try_remap_cnum(&self, tcx: TyCtxt<'_>, cnum: u32) -> Option<CrateNum> {
433-
let cnum_map =
434-
self.cnum_map.get_or_init(|| Self::compute_cnum_map(tcx, &self.prev_cnums[..]));
435-
debug!("try_remap_cnum({}): cnum_map={:?}", cnum, cnum_map);
408+
fn try_remap_cnum(&self, tcx: TyCtxt<'_>, stable_crate_id: StableCrateId) -> Option<CrateNum> {
409+
let cnum_map = self.cnum_map.get_or_init(|| Self::compute_cnum_map(tcx));
410+
debug!("try_remap_cnum({:?}): cnum_map={:?}", stable_crate_id, cnum_map);
436411

437-
cnum_map[CrateNum::from_u32(cnum)]
412+
cnum_map.get(&stable_crate_id).copied()
438413
}
439414

440415
pub(crate) fn store_foreign_def_id_hash(&self, def_id: DefId, hash: DefPathHash) {
@@ -533,8 +508,7 @@ impl<'sess> OnDiskCache<'sess> {
533508
where
534509
T: Decodable<CacheDecoder<'a, 'tcx>>,
535510
{
536-
let cnum_map =
537-
self.cnum_map.get_or_init(|| Self::compute_cnum_map(tcx, &self.prev_cnums[..]));
511+
let cnum_map = self.cnum_map.get_or_init(|| Self::compute_cnum_map(tcx));
538512

539513
let mut decoder = CacheDecoder {
540514
tcx,
@@ -555,31 +529,16 @@ impl<'sess> OnDiskCache<'sess> {
555529
// current-session-`CrateNum`. There might be `CrateNum`s from the previous
556530
// `Session` that don't occur in the current one. For these, the mapping
557531
// maps to None.
558-
fn compute_cnum_map(
559-
tcx: TyCtxt<'_>,
560-
prev_cnums: &[(u32, String, CrateDisambiguator)],
561-
) -> IndexVec<CrateNum, Option<CrateNum>> {
532+
fn compute_cnum_map(tcx: TyCtxt<'_>) -> UnhashMap<StableCrateId, CrateNum> {
562533
tcx.dep_graph.with_ignore(|| {
563-
let current_cnums = tcx
564-
.all_crate_nums(())
534+
tcx.all_crate_nums(())
565535
.iter()
536+
.chain(std::iter::once(&LOCAL_CRATE))
566537
.map(|&cnum| {
567-
let crate_name = tcx.crate_name(cnum).to_string();
568-
let crate_disambiguator = tcx.crate_disambiguator(cnum);
569-
((crate_name, crate_disambiguator), cnum)
538+
let hash = tcx.def_path_hash(cnum.as_def_id()).stable_crate_id();
539+
(hash, cnum)
570540
})
571-
.collect::<FxHashMap<_, _>>();
572-
573-
let map_size = prev_cnums.iter().map(|&(cnum, ..)| cnum).max().unwrap_or(0) + 1;
574-
let mut map = IndexVec::from_elem_n(None, map_size as usize);
575-
576-
for &(prev_cnum, ref crate_name, crate_disambiguator) in prev_cnums {
577-
let key = (crate_name.clone(), crate_disambiguator);
578-
map[CrateNum::from_u32(prev_cnum)] = current_cnums.get(&key).cloned();
579-
}
580-
581-
map[LOCAL_CRATE] = Some(LOCAL_CRATE);
582-
map
541+
.collect()
583542
})
584543
}
585544

@@ -612,7 +571,7 @@ impl<'sess> OnDiskCache<'sess> {
612571
debug!("def_path_hash_to_def_id({:?}): raw_def_id = {:?}", hash, raw_def_id);
613572
// If the owning crate no longer exists, the corresponding definition definitely
614573
// no longer exists.
615-
let krate = self.try_remap_cnum(tcx, raw_def_id.krate)?;
574+
let krate = self.try_remap_cnum(tcx, hash.stable_crate_id())?;
616575
debug!("def_path_hash_to_def_id({:?}): krate = {:?}", hash, krate);
617576
// If our `DefPathHash` corresponded to a definition in the local crate,
618577
// we should have either found it in `local_def_path_hash_to_def_id`, or
@@ -644,7 +603,7 @@ pub struct CacheDecoder<'a, 'tcx> {
644603
tcx: TyCtxt<'tcx>,
645604
opaque: opaque::Decoder<'a>,
646605
source_map: &'a SourceMap,
647-
cnum_map: &'a IndexVec<CrateNum, Option<CrateNum>>,
606+
cnum_map: &'a UnhashMap<StableCrateId, CrateNum>,
648607
file_index_to_file: &'a Lock<FxHashMap<SourceFileIndex, Lrc<SourceFile>>>,
649608
file_index_to_stable_id: &'a FxHashMap<SourceFileIndex, StableSourceFileId>,
650609
alloc_decoding_session: AllocDecodingSession<'a>,
@@ -765,10 +724,6 @@ impl<'a, 'tcx> TyDecoder<'tcx> for CacheDecoder<'a, 'tcx> {
765724
r
766725
}
767726

768-
fn map_encoded_cnum_to_current(&self, cnum: CrateNum) -> CrateNum {
769-
self.cnum_map[cnum].unwrap_or_else(|| bug!("could not find new `CrateNum` for {:?}", cnum))
770-
}
771-
772727
fn decode_alloc_id(&mut self) -> Result<interpret::AllocId, Self::Error> {
773728
let alloc_decoding_session = self.alloc_decoding_session;
774729
alloc_decoding_session.decode_alloc_id(self)
@@ -850,8 +805,9 @@ impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for Span {
850805

851806
impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for CrateNum {
852807
fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Result<Self, String> {
853-
let cnum = CrateNum::from_u32(u32::decode(d)?);
854-
Ok(d.map_encoded_cnum_to_current(cnum))
808+
let stable_id = StableCrateId::decode(d)?;
809+
let cnum = d.cnum_map[&stable_id];
810+
Ok(cnum)
855811
}
856812
}
857813

@@ -1061,6 +1017,23 @@ where
10611017
}
10621018
}
10631019

1020+
impl<'a, 'tcx, E> Encodable<CacheEncoder<'a, 'tcx, E>> for CrateNum
1021+
where
1022+
E: 'a + OpaqueEncoder,
1023+
{
1024+
fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) -> Result<(), E::Error> {
1025+
let def_path_hash = s.tcx.def_path_hash(self.as_def_id());
1026+
// Store additional information when we encode a foreign `DefId`,
1027+
// so that we can map its `DefPathHash` back to a `DefId` in the next
1028+
// compilation session.
1029+
if *self != LOCAL_CRATE {
1030+
s.latest_foreign_def_path_hashes
1031+
.insert(def_path_hash, RawDefId { krate: self.as_u32(), index: 0 });
1032+
}
1033+
def_path_hash.stable_crate_id().encode(s)
1034+
}
1035+
}
1036+
10641037
impl<'a, 'tcx, E> Encodable<CacheEncoder<'a, 'tcx, E>> for DefId
10651038
where
10661039
E: 'a + OpaqueEncoder,

0 commit comments

Comments
 (0)