Skip to content

Commit 2f44ef2

Browse files
incr.comp.: Encode DefIds as DefPathHashes instead of recomputing those during deserialization.
1 parent 24e54dd commit 2f44ef2

File tree

1 file changed

+65
-73
lines changed

1 file changed

+65
-73
lines changed

src/librustc/ty/maps/on_disk_cache.rs

Lines changed: 65 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
use dep_graph::{DepNodeIndex, SerializedDepNodeIndex};
1212
use errors::Diagnostic;
1313
use hir;
14-
use hir::def_id::{CrateNum, DefIndex, DefId, RESERVED_FOR_INCR_COMP_CACHE,
15-
LOCAL_CRATE};
16-
use hir::map::definitions::{Definitions, DefPathTable};
14+
use hir::def_id::{CrateNum, DefIndex, DefId, LocalDefId,
15+
RESERVED_FOR_INCR_COMP_CACHE, LOCAL_CRATE};
16+
use hir::map::definitions::DefPathHash;
1717
use middle::const_val::ByteArray;
1818
use middle::cstore::CrateStore;
1919
use rustc_data_structures::fx::FxHashMap;
@@ -37,7 +37,6 @@ use ty::subst::Substs;
3737
// Some magic values used for verifying that encoding and decoding. These are
3838
// basically random numbers.
3939
const PREV_DIAGNOSTICS_TAG: u64 = 0x1234_5678_A1A1_A1A1;
40-
const DEF_PATH_TABLE_TAG: u64 = 0x1234_5678_B2B2_B2B2;
4140
const QUERY_RESULT_INDEX_TAG: u64 = 0x1234_5678_C3C3_C3C3;
4241

4342
/// `OnDiskCache` provides an interface to incr. comp. data cached from the
@@ -56,10 +55,8 @@ pub struct OnDiskCache<'sess> {
5655
// compilation session.
5756
current_diagnostics: RefCell<FxHashMap<DepNodeIndex, Vec<Diagnostic>>>,
5857

59-
6058
prev_cnums: Vec<(u32, String, CrateDisambiguator)>,
6159
cnum_map: RefCell<Option<IndexVec<CrateNum, Option<CrateNum>>>>,
62-
prev_def_path_tables: Vec<DefPathTable>,
6360

6461
prev_filemap_starts: BTreeMap<BytePos, StableFilemapId>,
6562
codemap: &'sess CodeMap,
@@ -92,14 +89,13 @@ impl<'sess> OnDiskCache<'sess> {
9289
(header, decoder.position())
9390
};
9491

95-
let (prev_diagnostics, prev_def_path_tables, query_result_index) = {
92+
let (prev_diagnostics, query_result_index) = {
9693
let mut decoder = CacheDecoder {
9794
tcx: None,
9895
opaque: opaque::Decoder::new(&data[..], post_header_pos),
9996
codemap: sess.codemap(),
10097
prev_filemap_starts: &header.prev_filemap_starts,
10198
cnum_map: &IndexVec::new(),
102-
prev_def_path_tables: &Vec::new(),
10399
};
104100

105101
// Decode Diagnostics
@@ -111,11 +107,6 @@ impl<'sess> OnDiskCache<'sess> {
111107
diagnostics.into_iter().collect()
112108
};
113109

114-
// Decode DefPathTables
115-
let prev_def_path_tables: Vec<DefPathTable> =
116-
decode_tagged(&mut decoder, DEF_PATH_TABLE_TAG)
117-
.expect("Error while trying to decode cached DefPathTables.");
118-
119110
// Decode the *position* of the query result index
120111
let query_result_index_pos = {
121112
let pos_pos = data.len() - IntEncodedWithFixedSize::ENCODED_SIZE;
@@ -131,7 +122,7 @@ impl<'sess> OnDiskCache<'sess> {
131122
decode_tagged(decoder, QUERY_RESULT_INDEX_TAG)
132123
}).expect("Error while trying to decode query result index.");
133124

134-
(prev_diagnostics, prev_def_path_tables, query_result_index)
125+
(prev_diagnostics, query_result_index)
135126
};
136127

137128
OnDiskCache {
@@ -140,7 +131,6 @@ impl<'sess> OnDiskCache<'sess> {
140131
prev_filemap_starts: header.prev_filemap_starts,
141132
prev_cnums: header.prev_cnums,
142133
cnum_map: RefCell::new(None),
143-
prev_def_path_tables,
144134
codemap: sess.codemap(),
145135
current_diagnostics: RefCell::new(FxHashMap()),
146136
query_result_index: query_result_index.into_iter().collect(),
@@ -154,7 +144,6 @@ impl<'sess> OnDiskCache<'sess> {
154144
prev_filemap_starts: BTreeMap::new(),
155145
prev_cnums: vec![],
156146
cnum_map: RefCell::new(None),
157-
prev_def_path_tables: Vec::new(),
158147
codemap,
159148
current_diagnostics: RefCell::new(FxHashMap()),
160149
query_result_index: FxHashMap(),
@@ -172,10 +161,10 @@ impl<'sess> OnDiskCache<'sess> {
172161
let _in_ignore = tcx.dep_graph.in_ignore();
173162

174163
let mut encoder = CacheEncoder {
164+
tcx,
175165
encoder,
176166
type_shorthands: FxHashMap(),
177167
predicate_shorthands: FxHashMap(),
178-
definitions: tcx.hir.definitions(),
179168
};
180169

181170

@@ -212,22 +201,6 @@ impl<'sess> OnDiskCache<'sess> {
212201
encoder.encode_tagged(PREV_DIAGNOSTICS_TAG, &diagnostics)?;
213202

214203

215-
// Encode all DefPathTables
216-
let upstream_def_path_tables = tcx.all_crate_nums(LOCAL_CRATE)
217-
.iter()
218-
.map(|&cnum| (cnum, cstore.def_path_table(cnum)))
219-
.collect::<FxHashMap<_,_>>();
220-
let def_path_tables: Vec<&DefPathTable> = sorted_cnums.into_iter().map(|cnum| {
221-
if cnum == LOCAL_CRATE {
222-
tcx.hir.definitions().def_path_table()
223-
} else {
224-
&*upstream_def_path_tables[&cnum]
225-
}
226-
}).collect();
227-
228-
encoder.encode_tagged(DEF_PATH_TABLE_TAG, &def_path_tables)?;
229-
230-
231204
// Encode query results
232205
let mut query_result_index = EncodedQueryResultIndex::new();
233206

@@ -297,7 +270,6 @@ impl<'sess> OnDiskCache<'sess> {
297270
codemap: self.codemap,
298271
prev_filemap_starts: &self.prev_filemap_starts,
299272
cnum_map: cnum_map.as_ref().unwrap(),
300-
prev_def_path_tables: &self.prev_def_path_tables,
301273
};
302274

303275
match decode_tagged(&mut decoder, dep_node_index) {
@@ -373,7 +345,6 @@ struct CacheDecoder<'a, 'tcx: 'a, 'x> {
373345
codemap: &'x CodeMap,
374346
prev_filemap_starts: &'x BTreeMap<BytePos, StableFilemapId>,
375347
cnum_map: &'x IndexVec<CrateNum, Option<CrateNum>>,
376-
prev_def_path_tables: &'x Vec<DefPathTable>,
377348
}
378349

379350
impl<'a, 'tcx, 'x> CacheDecoder<'a, 'tcx, 'x> {
@@ -548,32 +519,24 @@ impl<'a, 'tcx, 'x> SpecializedDecoder<DefIndex> for CacheDecoder<'a, 'tcx, 'x> {
548519
// sessions, to map the old DefId to the new one.
549520
impl<'a, 'tcx, 'x> SpecializedDecoder<DefId> for CacheDecoder<'a, 'tcx, 'x> {
550521
fn specialized_decode(&mut self) -> Result<DefId, Self::Error> {
551-
// Decode the unmapped CrateNum
552-
let prev_cnum = CrateNum::default_decode(self)?;
553-
554-
// Decode the unmapped DefIndex
555-
let def_index = DefIndex::default_decode(self)?;
556-
557-
// Unmapped CrateNum and DefIndex are valid keys for the *cached*
558-
// DefPathTables, so we use them to look up the DefPathHash.
559-
let def_path_hash = self.prev_def_path_tables[prev_cnum.index()]
560-
.def_path_hash(def_index);
522+
// Load the DefPathHash which is was we encoded the DefId as.
523+
let def_path_hash = DefPathHash::decode(self)?;
561524

562525
// Using the DefPathHash, we can lookup the new DefId
563526
Ok(self.tcx().def_path_hash_to_def_id.as_ref().unwrap()[&def_path_hash])
564527
}
565528
}
566529

530+
impl<'a, 'tcx, 'x> SpecializedDecoder<LocalDefId> for CacheDecoder<'a, 'tcx, 'x> {
531+
fn specialized_decode(&mut self) -> Result<LocalDefId, Self::Error> {
532+
Ok(LocalDefId::from_def_id(DefId::decode(self)?))
533+
}
534+
}
535+
567536
impl<'a, 'tcx, 'x> SpecializedDecoder<hir::HirId> for CacheDecoder<'a, 'tcx, 'x> {
568537
fn specialized_decode(&mut self) -> Result<hir::HirId, Self::Error> {
569-
// Decode the unmapped DefIndex of the HirId.
570-
let def_index = DefIndex::default_decode(self)?;
571-
572-
// Use the unmapped DefIndex to look up the DefPathHash in the cached
573-
// DefPathTable. For HirIds we know that we always have to look in the
574-
// *local* DefPathTable.
575-
let def_path_hash = self.prev_def_path_tables[LOCAL_CRATE.index()]
576-
.def_path_hash(def_index);
538+
// Load the DefPathHash which is was we encoded the DefIndex as.
539+
let def_path_hash = DefPathHash::decode(self)?;
577540

578541
// Use the DefPathHash to map to the current DefId.
579542
let def_id = self.tcx()
@@ -666,16 +629,17 @@ for CacheDecoder<'a, 'tcx, 'x> {
666629

667630
//- ENCODING -------------------------------------------------------------------
668631

669-
struct CacheEncoder<'enc, 'tcx, E>
670-
where E: 'enc + ty_codec::TyEncoder
632+
struct CacheEncoder<'enc, 'a, 'tcx, E>
633+
where E: 'enc + ty_codec::TyEncoder,
634+
'tcx: 'a,
671635
{
636+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
672637
encoder: &'enc mut E,
673638
type_shorthands: FxHashMap<ty::Ty<'tcx>, usize>,
674639
predicate_shorthands: FxHashMap<ty::Predicate<'tcx>, usize>,
675-
definitions: &'enc Definitions,
676640
}
677641

678-
impl<'enc, 'tcx, E> CacheEncoder<'enc, 'tcx, E>
642+
impl<'enc, 'a, 'tcx, E> CacheEncoder<'enc, 'a, 'tcx, E>
679643
where E: 'enc + ty_codec::TyEncoder
680644
{
681645
/// Encode something with additional information that allows to do some
@@ -699,15 +663,15 @@ impl<'enc, 'tcx, E> CacheEncoder<'enc, 'tcx, E>
699663
}
700664
}
701665

702-
impl<'enc, 'tcx, E> ty_codec::TyEncoder for CacheEncoder<'enc, 'tcx, E>
666+
impl<'enc, 'a, 'tcx, E> ty_codec::TyEncoder for CacheEncoder<'enc, 'a, 'tcx, E>
703667
where E: 'enc + ty_codec::TyEncoder
704668
{
705669
fn position(&self) -> usize {
706670
self.encoder.position()
707671
}
708672
}
709673

710-
impl<'enc, 'tcx, E> SpecializedEncoder<ty::Ty<'tcx>> for CacheEncoder<'enc, 'tcx, E>
674+
impl<'enc, 'a, 'tcx, E> SpecializedEncoder<ty::Ty<'tcx>> for CacheEncoder<'enc, 'a, 'tcx, E>
711675
where E: 'enc + ty_codec::TyEncoder
712676
{
713677
fn specialized_encode(&mut self, ty: &ty::Ty<'tcx>) -> Result<(), Self::Error> {
@@ -716,8 +680,8 @@ impl<'enc, 'tcx, E> SpecializedEncoder<ty::Ty<'tcx>> for CacheEncoder<'enc, 'tcx
716680
}
717681
}
718682

719-
impl<'enc, 'tcx, E> SpecializedEncoder<ty::GenericPredicates<'tcx>>
720-
for CacheEncoder<'enc, 'tcx, E>
683+
impl<'enc, 'a, 'tcx, E> SpecializedEncoder<ty::GenericPredicates<'tcx>>
684+
for CacheEncoder<'enc, 'a, 'tcx, E>
721685
where E: 'enc + ty_codec::TyEncoder
722686
{
723687
fn specialized_encode(&mut self,
@@ -728,7 +692,7 @@ impl<'enc, 'tcx, E> SpecializedEncoder<ty::GenericPredicates<'tcx>>
728692
}
729693
}
730694

731-
impl<'enc, 'tcx, E> SpecializedEncoder<hir::HirId> for CacheEncoder<'enc, 'tcx, E>
695+
impl<'enc, 'a, 'tcx, E> SpecializedEncoder<hir::HirId> for CacheEncoder<'enc, 'a, 'tcx, E>
732696
where E: 'enc + ty_codec::TyEncoder
733697
{
734698
fn specialized_encode(&mut self, id: &hir::HirId) -> Result<(), Self::Error> {
@@ -737,18 +701,46 @@ impl<'enc, 'tcx, E> SpecializedEncoder<hir::HirId> for CacheEncoder<'enc, 'tcx,
737701
local_id,
738702
} = *id;
739703

740-
owner.encode(self)?;
704+
let def_path_hash = self.tcx.hir.definitions().def_path_hash(owner);
705+
706+
def_path_hash.encode(self)?;
741707
local_id.encode(self)
742708
}
743709
}
744710

711+
712+
impl<'enc, 'a, 'tcx, E> SpecializedEncoder<DefId> for CacheEncoder<'enc, 'a, 'tcx, E>
713+
where E: 'enc + ty_codec::TyEncoder
714+
{
715+
fn specialized_encode(&mut self, id: &DefId) -> Result<(), Self::Error> {
716+
let def_path_hash = self.tcx.def_path_hash(*id);
717+
def_path_hash.encode(self)
718+
}
719+
}
720+
721+
impl<'enc, 'a, 'tcx, E> SpecializedEncoder<LocalDefId> for CacheEncoder<'enc, 'a, 'tcx, E>
722+
where E: 'enc + ty_codec::TyEncoder
723+
{
724+
fn specialized_encode(&mut self, id: &LocalDefId) -> Result<(), Self::Error> {
725+
id.to_def_id().encode(self)
726+
}
727+
}
728+
729+
impl<'enc, 'a, 'tcx, E> SpecializedEncoder<DefIndex> for CacheEncoder<'enc, 'a, 'tcx, E>
730+
where E: 'enc + ty_codec::TyEncoder
731+
{
732+
fn specialized_encode(&mut self, _: &DefIndex) -> Result<(), Self::Error> {
733+
bug!("Encoding DefIndex without context.")
734+
}
735+
}
736+
745737
// NodeIds are not stable across compilation sessions, so we store them in their
746738
// HirId representation. This allows use to map them to the current NodeId.
747-
impl<'enc, 'tcx, E> SpecializedEncoder<NodeId> for CacheEncoder<'enc, 'tcx, E>
739+
impl<'enc, 'a, 'tcx, E> SpecializedEncoder<NodeId> for CacheEncoder<'enc, 'a, 'tcx, E>
748740
where E: 'enc + ty_codec::TyEncoder
749741
{
750742
fn specialized_encode(&mut self, node_id: &NodeId) -> Result<(), Self::Error> {
751-
let hir_id = self.definitions.node_to_hir_id(*node_id);
743+
let hir_id = self.tcx.hir.node_to_hir_id(*node_id);
752744
hir_id.encode(self)
753745
}
754746
}
@@ -761,7 +753,7 @@ macro_rules! encoder_methods {
761753
}
762754
}
763755

764-
impl<'enc, 'tcx, E> Encoder for CacheEncoder<'enc, 'tcx, E>
756+
impl<'enc, 'a, 'tcx, E> Encoder for CacheEncoder<'enc, 'a, 'tcx, E>
765757
where E: 'enc + ty_codec::TyEncoder
766758
{
767759
type Error = E::Error;
@@ -803,8 +795,8 @@ impl IntEncodedWithFixedSize {
803795
impl UseSpecializedEncodable for IntEncodedWithFixedSize {}
804796
impl UseSpecializedDecodable for IntEncodedWithFixedSize {}
805797

806-
impl<'enc, 'tcx, E> SpecializedEncoder<IntEncodedWithFixedSize>
807-
for CacheEncoder<'enc, 'tcx, E>
798+
impl<'enc, 'a, 'tcx, E> SpecializedEncoder<IntEncodedWithFixedSize>
799+
for CacheEncoder<'enc, 'a, 'tcx, E>
808800
where E: 'enc + ty_codec::TyEncoder
809801
{
810802
fn specialized_encode(&mut self, x: &IntEncodedWithFixedSize) -> Result<(), Self::Error> {
@@ -836,12 +828,12 @@ for CacheDecoder<'a, 'tcx, 'x> {
836828
}
837829
}
838830

839-
fn encode_query_results<'x, 'a, 'tcx, Q, E>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
840-
encoder: &mut CacheEncoder<'x, 'tcx, E>,
841-
query_result_index: &mut EncodedQueryResultIndex)
842-
-> Result<(), E::Error>
831+
fn encode_query_results<'enc, 'a, 'tcx, Q, E>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
832+
encoder: &mut CacheEncoder<'enc, 'a, 'tcx, E>,
833+
query_result_index: &mut EncodedQueryResultIndex)
834+
-> Result<(), E::Error>
843835
where Q: super::plumbing::GetCacheInternal<'tcx>,
844-
E: 'x + TyEncoder,
836+
E: 'enc + TyEncoder,
845837
Q::Value: Encodable,
846838
{
847839
for (key, entry) in Q::get_cache_internal(tcx).map.iter() {

0 commit comments

Comments
 (0)