Skip to content

Commit 8cbc022

Browse files
incr.comp.: Include header when loading cache files in order to get the same byte offsets as when saving.
1 parent 67d2b1b commit 8cbc022

File tree

3 files changed

+28
-20
lines changed

3 files changed

+28
-20
lines changed

src/librustc/ty/maps/on_disk_cache.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ impl<'sess> OnDiskCache<'sess> {
5858
/// so far) will eagerly deserialize the complete cache. Once we are
5959
/// dealing with larger amounts of data (i.e. cached query results),
6060
/// deserialization will need to happen lazily.
61-
pub fn new(sess: &'sess Session, data: &[u8]) -> OnDiskCache<'sess> {
61+
pub fn new(sess: &'sess Session, data: &[u8], start_pos: usize) -> OnDiskCache<'sess> {
6262
debug_assert!(sess.opts.incremental.is_some());
6363

64-
let mut decoder = opaque::Decoder::new(&data[..], 0);
64+
let mut decoder = opaque::Decoder::new(&data[..], start_pos);
6565
let header = Header::decode(&mut decoder).unwrap();
6666

6767
let prev_diagnostics: FxHashMap<_, _> = {

src/librustc_incremental/persist/file_format.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,25 @@ pub fn write_file_header<W: io::Write>(stream: &mut W) -> io::Result<()> {
5353

5454
/// Reads the contents of a file with a file header as defined in this module.
5555
///
56-
/// - Returns `Ok(Some(data))` if the file existed and was generated by a
56+
/// - Returns `Ok(Some(data, pos))` if the file existed and was generated by a
5757
/// compatible compiler version. `data` is the entire contents of the file
58-
/// *after* the header.
58+
/// and `pos` points to the first byte after the header.
5959
/// - Returns `Ok(None)` if the file did not exist or was generated by an
6060
/// incompatible version of the compiler.
6161
/// - Returns `Err(..)` if some kind of IO error occurred while reading the
6262
/// file.
63-
pub fn read_file(sess: &Session, path: &Path) -> io::Result<Option<Vec<u8>>> {
63+
pub fn read_file(sess: &Session, path: &Path) -> io::Result<Option<(Vec<u8>, usize)>> {
6464
if !path.exists() {
6565
return Ok(None);
6666
}
6767

6868
let mut file = File::open(path)?;
69+
let file_size = file.metadata()?.len() as usize;
70+
71+
let mut data = Vec::with_capacity(file_size);
72+
file.read_to_end(&mut data)?;
73+
74+
let mut file = io::Cursor::new(data);
6975

7076
// Check FILE_MAGIC
7177
{
@@ -107,10 +113,8 @@ pub fn read_file(sess: &Session, path: &Path) -> io::Result<Option<Vec<u8>>> {
107113
}
108114
}
109115

110-
let mut data = vec![];
111-
file.read_to_end(&mut data)?;
112-
113-
Ok(Some(data))
116+
let post_header_start_pos = file.position() as usize;
117+
Ok(Some((file.into_inner(), post_header_start_pos)))
114118
}
115119

116120
fn report_format_mismatch(sess: &Session, file: &Path, message: &str) {

src/librustc_incremental/persist/load.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ pub fn dep_graph_tcx_init<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
4242
}
4343

4444
let work_products_path = work_products_path(tcx.sess);
45-
if let Some(work_products_data) = load_data(tcx.sess, &work_products_path) {
45+
if let Some((work_products_data, start_pos)) = load_data(tcx.sess, &work_products_path) {
4646
// Decode the list of work_products
47-
let mut work_product_decoder = Decoder::new(&work_products_data[..], 0);
47+
let mut work_product_decoder = Decoder::new(&work_products_data[..], start_pos);
4848
let work_products: Vec<SerializedWorkProduct> =
4949
RustcDecodable::decode(&mut work_product_decoder).unwrap_or_else(|e| {
5050
let msg = format!("Error decoding `work-products` from incremental \
@@ -77,9 +77,9 @@ pub fn dep_graph_tcx_init<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
7777
}
7878
}
7979

80-
fn load_data(sess: &Session, path: &Path) -> Option<Vec<u8>> {
80+
fn load_data(sess: &Session, path: &Path) -> Option<(Vec<u8>, usize)> {
8181
match file_format::read_file(sess, path) {
82-
Ok(Some(data)) => return Some(data),
82+
Ok(Some(data_and_pos)) => return Some(data_and_pos),
8383
Ok(None) => {
8484
// The file either didn't exist or was produced by an incompatible
8585
// compiler version. Neither is an error.
@@ -126,8 +126,8 @@ pub fn load_prev_metadata_hashes(tcx: TyCtxt) -> DefIdMap<Fingerprint> {
126126

127127
debug!("load_prev_metadata_hashes() - File: {}", file_path.display());
128128

129-
let data = match file_format::read_file(tcx.sess, &file_path) {
130-
Ok(Some(data)) => data,
129+
let (data, start_pos) = match file_format::read_file(tcx.sess, &file_path) {
130+
Ok(Some(data_and_pos)) => data_and_pos,
131131
Ok(None) => {
132132
debug!("load_prev_metadata_hashes() - File produced by incompatible \
133133
compiler version: {}", file_path.display());
@@ -141,7 +141,7 @@ pub fn load_prev_metadata_hashes(tcx: TyCtxt) -> DefIdMap<Fingerprint> {
141141
};
142142

143143
debug!("load_prev_metadata_hashes() - Decoding hashes");
144-
let mut decoder = Decoder::new(&data, 0);
144+
let mut decoder = Decoder::new(&data, start_pos);
145145
let _ = Svh::decode(&mut decoder).unwrap();
146146
let serialized_hashes = SerializedMetadataHashes::decode(&mut decoder).unwrap();
147147

@@ -171,8 +171,8 @@ pub fn load_dep_graph(sess: &Session) -> PreviousDepGraph {
171171
return empty
172172
}
173173

174-
if let Some(bytes) = load_data(sess, &dep_graph_path(sess)) {
175-
let mut decoder = Decoder::new(&bytes, 0);
174+
if let Some((bytes, start_pos)) = load_data(sess, &dep_graph_path(sess)) {
175+
let mut decoder = Decoder::new(&bytes, start_pos);
176176
let prev_commandline_args_hash = u64::decode(&mut decoder)
177177
.expect("Error reading commandline arg hash from cached dep-graph");
178178

@@ -184,6 +184,10 @@ pub fn load_dep_graph(sess: &Session) -> PreviousDepGraph {
184184
// We can't reuse the cache, purge it.
185185
debug!("load_dep_graph_new: differing commandline arg hashes");
186186

187+
delete_all_session_dir_contents(sess)
188+
.expect("Failed to delete invalidated incr. comp. session \
189+
directory contents.");
190+
187191
// No need to do any further work
188192
return empty
189193
}
@@ -202,8 +206,8 @@ pub fn load_query_result_cache<'sess>(sess: &'sess Session) -> OnDiskCache<'sess
202206
return OnDiskCache::new_empty(sess.codemap());
203207
}
204208

205-
if let Some(bytes) = load_data(sess, &query_cache_path(sess)) {
206-
OnDiskCache::new(sess, &bytes[..])
209+
if let Some((bytes, start_pos)) = load_data(sess, &query_cache_path(sess)) {
210+
OnDiskCache::new(sess, &bytes[..], start_pos)
207211
} else {
208212
OnDiskCache::new_empty(sess.codemap())
209213
}

0 commit comments

Comments
 (0)