Skip to content

Commit 61fc8e3

Browse files
committed
Read incremental files on-demand.
1 parent a92f932 commit 61fc8e3

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

compiler/rustc_incremental/src/persist/file_format.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ pub fn read_file(
5454
report_incremental_info: bool,
5555
path: &Path,
5656
nightly_build: bool,
57-
) -> io::Result<Option<(Vec<u8>, usize)>> {
58-
let data = match fs::read(path) {
59-
Ok(data) => data,
57+
) -> io::Result<Option<io::BufReader<fs::File>>> {
58+
let file = match fs::File::open(path) {
59+
Ok(file) => file,
6060
Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(None),
6161
Err(err) => return Err(err),
6262
};
6363

64-
let mut file = io::Cursor::new(data);
64+
let mut file = io::BufReader::new(file);
6565

6666
// Check FILE_MAGIC
6767
{
@@ -102,8 +102,7 @@ pub fn read_file(
102102
}
103103
}
104104

105-
let post_header_start_pos = file.position() as usize;
106-
Ok(Some((file.into_inner(), post_header_start_pos)))
105+
Ok(Some(file))
107106
}
108107

109108
fn report_format_mismatch(report_incremental_info: bool, file: &Path, message: &str) {

compiler/rustc_incremental/src/persist/load.rs

+14-8
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ use rustc_data_structures::fx::FxHashMap;
44
use rustc_hir::definitions::Definitions;
55
use rustc_middle::dep_graph::{PreviousDepGraph, SerializedDepGraph, WorkProduct, WorkProductId};
66
use rustc_middle::ty::query::OnDiskCache;
7-
use rustc_serialize::opaque::Decoder;
7+
use rustc_serialize::opaque::FileDecoder;
88
use rustc_serialize::Decodable as RustcDecodable;
99
use rustc_session::Session;
10+
use std::fs;
11+
use std::io::{self, Read, Seek};
1012
use std::path::Path;
1113

1214
use super::data::*;
@@ -49,9 +51,9 @@ fn load_data(
4951
report_incremental_info: bool,
5052
path: &Path,
5153
nightly_build: bool,
52-
) -> LoadResult<(Vec<u8>, usize)> {
54+
) -> LoadResult<io::BufReader<fs::File>> {
5355
match file_format::read_file(report_incremental_info, path, nightly_build) {
54-
Ok(Some(data_and_pos)) => LoadResult::Ok { data: data_and_pos },
56+
Ok(Some(file)) => LoadResult::Ok { data: file },
5557
Ok(None) => {
5658
// The file either didn't exist or was produced by an incompatible
5759
// compiler version. Neither is an error.
@@ -116,9 +118,9 @@ pub fn load_dep_graph(sess: &Session) -> DepGraphFuture {
116118
let work_products_path = work_products_path(sess);
117119
let load_result = load_data(report_incremental_info, &work_products_path, nightly_build);
118120

119-
if let LoadResult::Ok { data: (work_products_data, start_pos) } = load_result {
121+
if let LoadResult::Ok { data: file } = load_result {
120122
// Decode the list of work_products
121-
let mut work_product_decoder = Decoder::new(&work_products_data[..], start_pos);
123+
let mut work_product_decoder = FileDecoder::new(file);
122124
let work_products: Vec<SerializedWorkProduct> =
123125
RustcDecodable::decode(&mut work_product_decoder).unwrap_or_else(|e| {
124126
let msg = format!(
@@ -163,8 +165,8 @@ pub fn load_dep_graph(sess: &Session) -> DepGraphFuture {
163165
match load_data(report_incremental_info, &path, nightly_build) {
164166
LoadResult::DataOutOfDate => LoadResult::DataOutOfDate,
165167
LoadResult::Error { message } => LoadResult::Error { message },
166-
LoadResult::Ok { data: (bytes, start_pos) } => {
167-
let mut decoder = Decoder::new(&bytes, start_pos);
168+
LoadResult::Ok { data: file } => {
169+
let mut decoder = FileDecoder::new(file);
168170
let prev_commandline_args_hash = u64::decode(&mut decoder)
169171
.expect("Error reading commandline arg hash from cached dep-graph");
170172

@@ -211,7 +213,11 @@ pub fn load_query_result_cache<'a>(
211213
&query_cache_path(sess),
212214
sess.is_nightly_build(),
213215
) {
214-
LoadResult::Ok { data: (bytes, start_pos) } => {
216+
LoadResult::Ok { data: mut file } => {
217+
let start_pos = file.seek(io::SeekFrom::Current(0)).unwrap() as usize;
218+
file.seek(io::SeekFrom::Start(0)).unwrap();
219+
let mut bytes = Vec::new();
220+
file.read_to_end(&mut bytes).unwrap();
215221
Some(OnDiskCache::new(sess, bytes, start_pos, definitions))
216222
}
217223
_ => Some(OnDiskCache::new_empty(sess.source_map())),

0 commit comments

Comments
 (0)