Skip to content

Commit

Permalink
Use raw encoder for work-product index.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Mar 5, 2022
1 parent 5f21be5 commit b51974f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
55 changes: 55 additions & 0 deletions compiler/rustc_incremental/src/persist/file_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,61 @@ where
debug!("save: data written to disk successfully");
}

pub(crate) fn save_in_raw<F>(sess: &Session, path_buf: PathBuf, name: &str, encode: F)
where
F: FnOnce(&mut raw::FileEncoder) -> raw::FileEncodeResult,
{
debug!("save: storing data in {}", path_buf.display());

// Delete the old file, if any.
// Note: It's important that we actually delete the old file and not just
// truncate and overwrite it, since it might be a shared hard-link, the
// underlying data of which we don't want to modify.
//
// We have to ensure we have dropped the memory maps to this file
// before performing this removal.
match fs::remove_file(&path_buf) {
Ok(()) => {
debug!("save: remove old file");
}
Err(err) if err.kind() == io::ErrorKind::NotFound => (),
Err(err) => {
sess.err(&format!(
"unable to delete old {} at `{}`: {}",
name,
path_buf.display(),
err
));
return;
}
}

let mut encoder = match raw::FileEncoder::new(&path_buf) {
Ok(encoder) => encoder,
Err(err) => {
sess.err(&format!("failed to create {} at `{}`: {}", name, path_buf.display(), err));
return;
}
};

if let Err(err) = write_file_header_raw(&mut encoder, sess.is_nightly_build()) {
sess.err(&format!("failed to write {} header to `{}`: {}", name, path_buf.display(), err));
return;
}

if let Err(err) = encode(&mut encoder) {
sess.err(&format!("failed to write {} to `{}`: {}", name, path_buf.display(), err));
return;
}

if let Err(err) = encoder.flush() {
sess.err(&format!("failed to flush {} to `{}`: {}", name, path_buf.display(), err));
return;
}

debug!("save: data written to disk successfully");
}

/// Reads the contents of a file with a file header as defined in this module.
///
/// - Returns `Ok(Some(data, pos))` if the file existed and was generated by a
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_incremental/src/persist/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::memmap::Mmap;
use rustc_middle::dep_graph::{SerializedDepGraph, WorkProduct, WorkProductId};
use rustc_middle::ty::OnDiskCache;
use rustc_serialize::{opaque, raw, Decodable};
use rustc_serialize::{raw, Decodable};
use rustc_session::config::IncrementalStateAssertion;
use rustc_session::Session;
use std::path::Path;
Expand Down Expand Up @@ -155,7 +155,7 @@ pub fn load_dep_graph(sess: &Session) -> DepGraphFuture {

if let LoadResult::Ok { data: (work_products_data, start_pos) } = load_result {
// Decode the list of work_products
let mut work_product_decoder = opaque::Decoder::new(&work_products_data[..], start_pos);
let mut work_product_decoder = raw::Decoder::new(&work_products_data[..], start_pos);
let work_products: Vec<SerializedWorkProduct> =
Decodable::decode(&mut work_product_decoder);

Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_incremental/src/persist/save.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub fn save_work_product_index(
debug!("save_work_product_index()");
dep_graph.assert_ignored();
let path = work_products_path(sess);
file_format::save_in(sess, path, "work product index", |e| {
file_format::save_in_raw(sess, path, "work product index", |e| {
encode_work_product_index(&new_work_products, e)
});

Expand Down Expand Up @@ -128,8 +128,8 @@ pub fn save_work_product_index(

fn encode_work_product_index(
work_products: &FxHashMap<WorkProductId, WorkProduct>,
encoder: &mut opaque::FileEncoder,
) -> opaque::FileEncodeResult {
encoder: &mut raw::FileEncoder,
) -> raw::FileEncodeResult {
let serialized_products: Vec<_> = work_products
.iter()
.map(|(id, work_product)| SerializedWorkProduct {
Expand Down

0 comments on commit b51974f

Please sign in to comment.