Skip to content

Commit 4afdeaa

Browse files
committed
Mmap the incremental data instead of reading it.
1 parent 05cccdc commit 4afdeaa

File tree

4 files changed

+21
-12
lines changed

4 files changed

+21
-12
lines changed

compiler/rustc_incremental/src/persist/file_format.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::fs;
1414
use std::io::{self, Read};
1515
use std::path::Path;
1616

17+
use rustc_data_structures::memmap::Mmap;
1718
use rustc_serialize::opaque::{FileEncodeResult, FileEncoder};
1819
use rustc_serialize::Encoder;
1920

@@ -54,14 +55,15 @@ pub fn read_file(
5455
report_incremental_info: bool,
5556
path: &Path,
5657
nightly_build: bool,
57-
) -> io::Result<Option<(Vec<u8>, usize)>> {
58-
let data = match fs::read(path) {
59-
Ok(data) => data,
58+
) -> io::Result<Option<(Mmap, usize)>> {
59+
let file = match fs::File::open(path) {
60+
Ok(file) => file,
6061
Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(None),
6162
Err(err) => return Err(err),
6263
};
64+
let mmap = unsafe { Mmap::map(file) }?;
6365

64-
let mut file = io::Cursor::new(data);
66+
let mut file = io::Cursor::new(&*mmap);
6567

6668
// Check FILE_MAGIC
6769
{
@@ -103,7 +105,7 @@ pub fn read_file(
103105
}
104106

105107
let post_header_start_pos = file.position() as usize;
106-
Ok(Some((file.into_inner(), post_header_start_pos)))
108+
Ok(Some((mmap, post_header_start_pos)))
107109
}
108110

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

compiler/rustc_incremental/src/persist/load.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Code to save/load the dep-graph from files.
22
33
use rustc_data_structures::fx::FxHashMap;
4+
use rustc_data_structures::memmap::Mmap;
45
use rustc_middle::dep_graph::{SerializedDepGraph, WorkProduct, WorkProductId};
56
use rustc_middle::ty::OnDiskCache;
67
use rustc_serialize::opaque::Decoder;
@@ -48,7 +49,7 @@ fn load_data(
4849
report_incremental_info: bool,
4950
path: &Path,
5051
nightly_build: bool,
51-
) -> LoadResult<(Vec<u8>, usize)> {
52+
) -> LoadResult<(Mmap, usize)> {
5253
match file_format::read_file(report_incremental_info, path, nightly_build) {
5354
Ok(Some(data_and_pos)) => LoadResult::Ok { data: data_and_pos },
5455
Ok(None) => {

compiler/rustc_middle/src/ty/context.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use crate::ty::{
2727
use rustc_ast as ast;
2828
use rustc_attr as attr;
2929
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
30+
use rustc_data_structures::memmap::Mmap;
3031
use rustc_data_structures::profiling::SelfProfilerRef;
3132
use rustc_data_structures::sharded::{IntoPointer, ShardedHashMap};
3233
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
@@ -71,7 +72,7 @@ use std::sync::Arc;
7172

7273
pub trait OnDiskCache<'tcx>: rustc_data_structures::sync::Sync {
7374
/// Creates a new `OnDiskCache` instance from the serialized data in `data`.
74-
fn new(sess: &'tcx Session, data: Vec<u8>, start_pos: usize) -> Self
75+
fn new(sess: &'tcx Session, data: Mmap, start_pos: usize) -> Self
7576
where
7677
Self: Sized;
7778

compiler/rustc_query_impl/src/on_disk_cache.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::QueryCtxt;
22
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet};
3+
use rustc_data_structures::memmap::Mmap;
34
use rustc_data_structures::sync::{HashMapExt, Lock, Lrc, OnceCell};
45
use rustc_data_structures::unhash::UnhashMap;
56
use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, StableCrateId, LOCAL_CRATE};
@@ -42,7 +43,7 @@ const TAG_EXPN_DATA: u8 = 1;
4243
/// any side effects that have been emitted during a query.
4344
pub struct OnDiskCache<'sess> {
4445
// The complete cache data in serialized form.
45-
serialized_data: Vec<u8>,
46+
serialized_data: Option<Mmap>,
4647

4748
// Collects all `QuerySideEffects` created during the current compilation
4849
// session.
@@ -182,7 +183,8 @@ impl EncodedSourceFileId {
182183
}
183184

184185
impl<'sess> rustc_middle::ty::OnDiskCache<'sess> for OnDiskCache<'sess> {
185-
fn new(sess: &'sess Session, data: Vec<u8>, start_pos: usize) -> Self {
186+
/// Creates a new `OnDiskCache` instance from the serialized data in `data`.
187+
fn new(sess: &'sess Session, data: Mmap, start_pos: usize) -> Self {
186188
debug_assert!(sess.opts.incremental.is_some());
187189

188190
// Wrap in a scope so we can borrow `data`.
@@ -204,7 +206,7 @@ impl<'sess> rustc_middle::ty::OnDiskCache<'sess> for OnDiskCache<'sess> {
204206
};
205207

206208
Self {
207-
serialized_data: data,
209+
serialized_data: Some(data),
208210
file_index_to_stable_id: footer.file_index_to_stable_id,
209211
file_index_to_file: Default::default(),
210212
cnum_map: OnceCell::new(),
@@ -225,7 +227,7 @@ impl<'sess> rustc_middle::ty::OnDiskCache<'sess> for OnDiskCache<'sess> {
225227

226228
fn new_empty(source_map: &'sess SourceMap) -> Self {
227229
Self {
228-
serialized_data: Vec::new(),
230+
serialized_data: None,
229231
file_index_to_stable_id: Default::default(),
230232
file_index_to_file: Default::default(),
231233
cnum_map: OnceCell::new(),
@@ -577,7 +579,10 @@ impl<'sess> OnDiskCache<'sess> {
577579

578580
let mut decoder = CacheDecoder {
579581
tcx,
580-
opaque: opaque::Decoder::new(&self.serialized_data[..], pos.to_usize()),
582+
opaque: opaque::Decoder::new(
583+
self.serialized_data.as_deref().unwrap_or(&[]),
584+
pos.to_usize(),
585+
),
581586
source_map: self.source_map,
582587
cnum_map,
583588
file_index_to_file: &self.file_index_to_file,

0 commit comments

Comments
 (0)