Skip to content

Commit 8769184

Browse files
incr.comp.: Add documentation for OnDiskCache.
1 parent f55425d commit 8769184

File tree

1 file changed

+55
-28
lines changed

1 file changed

+55
-28
lines changed

src/librustc/ty/maps/on_disk_cache.rs

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,42 @@ use std::mem;
2222
use syntax::codemap::{CodeMap, StableFilemapId};
2323
use syntax_pos::{BytePos, Span, NO_EXPANSION, DUMMY_SP};
2424

25+
/// `OnDiskCache` provides an interface to incr. comp. data cached from the
26+
/// previous compilation session. This data will eventually include the results
27+
/// of a few selected queries (like `typeck_tables_of` and `mir_optimized`) and
28+
/// any diagnostics that have been emitted during a query.
2529
pub struct OnDiskCache<'sess> {
30+
// The diagnostics emitted during the previous compilation session.
2631
prev_diagnostics: FxHashMap<SerializedDepNodeIndex, Vec<Diagnostic>>,
2732

33+
// This field collects all Diagnostics emitted during the current
34+
// compilation session.
35+
current_diagnostics: RefCell<FxHashMap<DepNodeIndex, Vec<Diagnostic>>>,
36+
37+
// This will eventually be needed for creating Decoders that can rebase
38+
// spans.
2839
_prev_filemap_starts: BTreeMap<BytePos, StableFilemapId>,
2940
codemap: &'sess CodeMap,
30-
31-
current_diagnostics: RefCell<FxHashMap<DepNodeIndex, Vec<Diagnostic>>>,
3241
}
3342

43+
// This type is used only for (de-)serialization.
3444
#[derive(RustcEncodable, RustcDecodable)]
3545
struct Header {
3646
prev_filemap_starts: BTreeMap<BytePos, StableFilemapId>,
3747
}
3848

49+
// This type is used only for (de-)serialization.
3950
#[derive(RustcEncodable, RustcDecodable)]
4051
struct Body {
4152
diagnostics: Vec<(SerializedDepNodeIndex, Vec<Diagnostic>)>,
4253
}
4354

4455
impl<'sess> OnDiskCache<'sess> {
45-
pub fn new_empty(codemap: &'sess CodeMap) -> OnDiskCache<'sess> {
46-
OnDiskCache {
47-
prev_diagnostics: FxHashMap(),
48-
_prev_filemap_starts: BTreeMap::new(),
49-
codemap,
50-
current_diagnostics: RefCell::new(FxHashMap()),
51-
}
52-
}
53-
56+
/// Create a new OnDiskCache instance from the serialized data in `data`.
57+
/// Note that the current implementation (which only deals with diagnostics
58+
/// so far) will eagerly deserialize the complete cache. Once we are
59+
/// dealing with larger amounts of data (i.e. cached query results),
60+
/// deserialization will need to happen lazily.
5461
pub fn new(sess: &'sess Session, data: &[u8]) -> OnDiskCache<'sess> {
5562
debug_assert!(sess.opts.incremental.is_some());
5663

@@ -75,6 +82,15 @@ impl<'sess> OnDiskCache<'sess> {
7582
}
7683
}
7784

85+
pub fn new_empty(codemap: &'sess CodeMap) -> OnDiskCache<'sess> {
86+
OnDiskCache {
87+
prev_diagnostics: FxHashMap(),
88+
_prev_filemap_starts: BTreeMap::new(),
89+
codemap,
90+
current_diagnostics: RefCell::new(FxHashMap()),
91+
}
92+
}
93+
7894
pub fn serialize<'a, 'tcx, E>(&self,
7995
encoder: &mut E)
8096
-> Result<(), E::Error>
@@ -101,12 +117,16 @@ impl<'sess> OnDiskCache<'sess> {
101117
Ok(())
102118
}
103119

120+
/// Load a diagnostic emitted during the previous compilation session.
104121
pub fn load_diagnostics(&self,
105122
dep_node_index: SerializedDepNodeIndex)
106123
-> Vec<Diagnostic> {
107124
self.prev_diagnostics.get(&dep_node_index).cloned().unwrap_or(vec![])
108125
}
109126

127+
/// Store a diagnostic emitted during the current compilation session.
128+
/// Anything stored like this will be available via `load_diagnostics` in
129+
/// the next compilation session.
110130
pub fn store_diagnostics(&self,
111131
dep_node_index: DepNodeIndex,
112132
diagnostics: Vec<Diagnostic>) {
@@ -115,6 +135,10 @@ impl<'sess> OnDiskCache<'sess> {
115135
debug_assert!(prev.is_none());
116136
}
117137

138+
/// Store a diagnostic emitted during computation of an anonymous query.
139+
/// Since many anonymous queries can share the same `DepNode`, we aggregate
140+
/// them -- as opposed to regular queries where we assume that there is a
141+
/// 1:1 relationship between query-key and `DepNode`.
118142
pub fn store_diagnostics_for_anon_node(&self,
119143
dep_node_index: DepNodeIndex,
120144
mut diagnostics: Vec<Diagnostic>) {
@@ -128,23 +152,9 @@ impl<'sess> OnDiskCache<'sess> {
128152
}
129153
}
130154

131-
impl<'a> SpecializedDecoder<Span> for CacheDecoder<'a> {
132-
fn specialized_decode(&mut self) -> Result<Span, Self::Error> {
133-
let lo = BytePos::decode(self)?;
134-
let hi = BytePos::decode(self)?;
135-
136-
if let Some((prev_filemap_start, filemap_id)) = self.find_filemap_prev_bytepos(lo) {
137-
if let Some(current_filemap) = self.codemap.filemap_by_stable_id(filemap_id) {
138-
let lo = (lo + current_filemap.start_pos) - prev_filemap_start;
139-
let hi = (hi + current_filemap.start_pos) - prev_filemap_start;
140-
return Ok(Span::new(lo, hi, NO_EXPANSION));
141-
}
142-
}
143-
144-
Ok(DUMMY_SP)
145-
}
146-
}
147-
155+
/// A decoder that can read the incr. comp. cache. It is similar to the one
156+
/// we use for crate metadata decoding in that it can rebase spans and
157+
/// eventually will also handle things that contain `Ty` instances.
148158
struct CacheDecoder<'a> {
149159
opaque: opaque::Decoder<'a>,
150160
codemap: &'a CodeMap,
@@ -202,3 +212,20 @@ impl<'sess> Decoder for CacheDecoder<'sess> {
202212
self.opaque.error(err)
203213
}
204214
}
215+
216+
impl<'a> SpecializedDecoder<Span> for CacheDecoder<'a> {
217+
fn specialized_decode(&mut self) -> Result<Span, Self::Error> {
218+
let lo = BytePos::decode(self)?;
219+
let hi = BytePos::decode(self)?;
220+
221+
if let Some((prev_filemap_start, filemap_id)) = self.find_filemap_prev_bytepos(lo) {
222+
if let Some(current_filemap) = self.codemap.filemap_by_stable_id(filemap_id) {
223+
let lo = (lo + current_filemap.start_pos) - prev_filemap_start;
224+
let hi = (hi + current_filemap.start_pos) - prev_filemap_start;
225+
return Ok(Span::new(lo, hi, NO_EXPANSION));
226+
}
227+
}
228+
229+
Ok(DUMMY_SP)
230+
}
231+
}

0 commit comments

Comments
 (0)