Skip to content

Commit b51974f

Browse files
committed
Use raw encoder for work-product index.
1 parent 5f21be5 commit b51974f

File tree

3 files changed

+60
-5
lines changed

3 files changed

+60
-5
lines changed

compiler/rustc_incremental/src/persist/file_format.rs

+55
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,61 @@ where
122122
debug!("save: data written to disk successfully");
123123
}
124124

125+
pub(crate) fn save_in_raw<F>(sess: &Session, path_buf: PathBuf, name: &str, encode: F)
126+
where
127+
F: FnOnce(&mut raw::FileEncoder) -> raw::FileEncodeResult,
128+
{
129+
debug!("save: storing data in {}", path_buf.display());
130+
131+
// Delete the old file, if any.
132+
// Note: It's important that we actually delete the old file and not just
133+
// truncate and overwrite it, since it might be a shared hard-link, the
134+
// underlying data of which we don't want to modify.
135+
//
136+
// We have to ensure we have dropped the memory maps to this file
137+
// before performing this removal.
138+
match fs::remove_file(&path_buf) {
139+
Ok(()) => {
140+
debug!("save: remove old file");
141+
}
142+
Err(err) if err.kind() == io::ErrorKind::NotFound => (),
143+
Err(err) => {
144+
sess.err(&format!(
145+
"unable to delete old {} at `{}`: {}",
146+
name,
147+
path_buf.display(),
148+
err
149+
));
150+
return;
151+
}
152+
}
153+
154+
let mut encoder = match raw::FileEncoder::new(&path_buf) {
155+
Ok(encoder) => encoder,
156+
Err(err) => {
157+
sess.err(&format!("failed to create {} at `{}`: {}", name, path_buf.display(), err));
158+
return;
159+
}
160+
};
161+
162+
if let Err(err) = write_file_header_raw(&mut encoder, sess.is_nightly_build()) {
163+
sess.err(&format!("failed to write {} header to `{}`: {}", name, path_buf.display(), err));
164+
return;
165+
}
166+
167+
if let Err(err) = encode(&mut encoder) {
168+
sess.err(&format!("failed to write {} to `{}`: {}", name, path_buf.display(), err));
169+
return;
170+
}
171+
172+
if let Err(err) = encoder.flush() {
173+
sess.err(&format!("failed to flush {} to `{}`: {}", name, path_buf.display(), err));
174+
return;
175+
}
176+
177+
debug!("save: data written to disk successfully");
178+
}
179+
125180
/// Reads the contents of a file with a file header as defined in this module.
126181
///
127182
/// - Returns `Ok(Some(data, pos))` if the file existed and was generated by a

compiler/rustc_incremental/src/persist/load.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_data_structures::fx::FxHashMap;
44
use rustc_data_structures::memmap::Mmap;
55
use rustc_middle::dep_graph::{SerializedDepGraph, WorkProduct, WorkProductId};
66
use rustc_middle::ty::OnDiskCache;
7-
use rustc_serialize::{opaque, raw, Decodable};
7+
use rustc_serialize::{raw, Decodable};
88
use rustc_session::config::IncrementalStateAssertion;
99
use rustc_session::Session;
1010
use std::path::Path;
@@ -155,7 +155,7 @@ pub fn load_dep_graph(sess: &Session) -> DepGraphFuture {
155155

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

compiler/rustc_incremental/src/persist/save.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ pub fn save_work_product_index(
9797
debug!("save_work_product_index()");
9898
dep_graph.assert_ignored();
9999
let path = work_products_path(sess);
100-
file_format::save_in(sess, path, "work product index", |e| {
100+
file_format::save_in_raw(sess, path, "work product index", |e| {
101101
encode_work_product_index(&new_work_products, e)
102102
});
103103

@@ -128,8 +128,8 @@ pub fn save_work_product_index(
128128

129129
fn encode_work_product_index(
130130
work_products: &FxHashMap<WorkProductId, WorkProduct>,
131-
encoder: &mut opaque::FileEncoder,
132-
) -> opaque::FileEncodeResult {
131+
encoder: &mut raw::FileEncoder,
132+
) -> raw::FileEncodeResult {
133133
let serialized_products: Vec<_> = work_products
134134
.iter()
135135
.map(|(id, work_product)| SerializedWorkProduct {

0 commit comments

Comments
 (0)