Mmap the incremental data instead of reading it.

This commit is contained in:
Camille GILLOT 2021-03-16 21:39:03 +01:00
parent 05cccdc9b3
commit 4afdeaaabd
4 changed files with 21 additions and 12 deletions

View file

@ -14,6 +14,7 @@ use std::fs;
use std::io::{self, Read}; use std::io::{self, Read};
use std::path::Path; use std::path::Path;
use rustc_data_structures::memmap::Mmap;
use rustc_serialize::opaque::{FileEncodeResult, FileEncoder}; use rustc_serialize::opaque::{FileEncodeResult, FileEncoder};
use rustc_serialize::Encoder; use rustc_serialize::Encoder;
@ -54,14 +55,15 @@ pub fn read_file(
report_incremental_info: bool, report_incremental_info: bool,
path: &Path, path: &Path,
nightly_build: bool, nightly_build: bool,
) -> io::Result<Option<(Vec<u8>, usize)>> { ) -> io::Result<Option<(Mmap, usize)>> {
let data = match fs::read(path) { let file = match fs::File::open(path) {
Ok(data) => data, Ok(file) => file,
Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(None), Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(None),
Err(err) => return Err(err), Err(err) => return Err(err),
}; };
let mmap = unsafe { Mmap::map(file) }?;
let mut file = io::Cursor::new(data); let mut file = io::Cursor::new(&*mmap);
// Check FILE_MAGIC // Check FILE_MAGIC
{ {
@ -103,7 +105,7 @@ pub fn read_file(
} }
let post_header_start_pos = file.position() as usize; let post_header_start_pos = file.position() as usize;
Ok(Some((file.into_inner(), post_header_start_pos))) Ok(Some((mmap, post_header_start_pos)))
} }
fn report_format_mismatch(report_incremental_info: bool, file: &Path, message: &str) { fn report_format_mismatch(report_incremental_info: bool, file: &Path, message: &str) {

View file

@ -1,6 +1,7 @@
//! Code to save/load the dep-graph from files. //! Code to save/load the dep-graph from files.
use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::memmap::Mmap;
use rustc_middle::dep_graph::{SerializedDepGraph, WorkProduct, WorkProductId}; use rustc_middle::dep_graph::{SerializedDepGraph, WorkProduct, WorkProductId};
use rustc_middle::ty::OnDiskCache; use rustc_middle::ty::OnDiskCache;
use rustc_serialize::opaque::Decoder; use rustc_serialize::opaque::Decoder;
@ -48,7 +49,7 @@ fn load_data(
report_incremental_info: bool, report_incremental_info: bool,
path: &Path, path: &Path,
nightly_build: bool, nightly_build: bool,
) -> LoadResult<(Vec<u8>, usize)> { ) -> LoadResult<(Mmap, usize)> {
match file_format::read_file(report_incremental_info, path, nightly_build) { match file_format::read_file(report_incremental_info, path, nightly_build) {
Ok(Some(data_and_pos)) => LoadResult::Ok { data: data_and_pos }, Ok(Some(data_and_pos)) => LoadResult::Ok { data: data_and_pos },
Ok(None) => { Ok(None) => {

View file

@ -27,6 +27,7 @@ use crate::ty::{
use rustc_ast as ast; use rustc_ast as ast;
use rustc_attr as attr; use rustc_attr as attr;
use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::memmap::Mmap;
use rustc_data_structures::profiling::SelfProfilerRef; use rustc_data_structures::profiling::SelfProfilerRef;
use rustc_data_structures::sharded::{IntoPointer, ShardedHashMap}; use rustc_data_structures::sharded::{IntoPointer, ShardedHashMap};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
@ -71,7 +72,7 @@ use std::sync::Arc;
pub trait OnDiskCache<'tcx>: rustc_data_structures::sync::Sync { pub trait OnDiskCache<'tcx>: rustc_data_structures::sync::Sync {
/// Creates a new `OnDiskCache` instance from the serialized data in `data`. /// Creates a new `OnDiskCache` instance from the serialized data in `data`.
fn new(sess: &'tcx Session, data: Vec<u8>, start_pos: usize) -> Self fn new(sess: &'tcx Session, data: Mmap, start_pos: usize) -> Self
where where
Self: Sized; Self: Sized;

View file

@ -1,5 +1,6 @@
use crate::QueryCtxt; use crate::QueryCtxt;
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet}; use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet};
use rustc_data_structures::memmap::Mmap;
use rustc_data_structures::sync::{HashMapExt, Lock, Lrc, OnceCell}; use rustc_data_structures::sync::{HashMapExt, Lock, Lrc, OnceCell};
use rustc_data_structures::unhash::UnhashMap; use rustc_data_structures::unhash::UnhashMap;
use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, StableCrateId, LOCAL_CRATE}; use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, StableCrateId, LOCAL_CRATE};
@ -42,7 +43,7 @@ const TAG_EXPN_DATA: u8 = 1;
/// any side effects that have been emitted during a query. /// any side effects that have been emitted during a query.
pub struct OnDiskCache<'sess> { pub struct OnDiskCache<'sess> {
// The complete cache data in serialized form. // The complete cache data in serialized form.
serialized_data: Vec<u8>, serialized_data: Option<Mmap>,
// Collects all `QuerySideEffects` created during the current compilation // Collects all `QuerySideEffects` created during the current compilation
// session. // session.
@ -182,7 +183,8 @@ impl EncodedSourceFileId {
} }
impl<'sess> rustc_middle::ty::OnDiskCache<'sess> for OnDiskCache<'sess> { impl<'sess> rustc_middle::ty::OnDiskCache<'sess> for OnDiskCache<'sess> {
fn new(sess: &'sess Session, data: Vec<u8>, start_pos: usize) -> Self { /// Creates a new `OnDiskCache` instance from the serialized data in `data`.
fn new(sess: &'sess Session, data: Mmap, start_pos: usize) -> Self {
debug_assert!(sess.opts.incremental.is_some()); debug_assert!(sess.opts.incremental.is_some());
// Wrap in a scope so we can borrow `data`. // Wrap in a scope so we can borrow `data`.
@ -204,7 +206,7 @@ impl<'sess> rustc_middle::ty::OnDiskCache<'sess> for OnDiskCache<'sess> {
}; };
Self { Self {
serialized_data: data, serialized_data: Some(data),
file_index_to_stable_id: footer.file_index_to_stable_id, file_index_to_stable_id: footer.file_index_to_stable_id,
file_index_to_file: Default::default(), file_index_to_file: Default::default(),
cnum_map: OnceCell::new(), cnum_map: OnceCell::new(),
@ -225,7 +227,7 @@ impl<'sess> rustc_middle::ty::OnDiskCache<'sess> for OnDiskCache<'sess> {
fn new_empty(source_map: &'sess SourceMap) -> Self { fn new_empty(source_map: &'sess SourceMap) -> Self {
Self { Self {
serialized_data: Vec::new(), serialized_data: None,
file_index_to_stable_id: Default::default(), file_index_to_stable_id: Default::default(),
file_index_to_file: Default::default(), file_index_to_file: Default::default(),
cnum_map: OnceCell::new(), cnum_map: OnceCell::new(),
@ -577,7 +579,10 @@ impl<'sess> OnDiskCache<'sess> {
let mut decoder = CacheDecoder { let mut decoder = CacheDecoder {
tcx, tcx,
opaque: opaque::Decoder::new(&self.serialized_data[..], pos.to_usize()), opaque: opaque::Decoder::new(
self.serialized_data.as_deref().unwrap_or(&[]),
pos.to_usize(),
),
source_map: self.source_map, source_map: self.source_map,
cnum_map, cnum_map,
file_index_to_file: &self.file_index_to_file, file_index_to_file: &self.file_index_to_file,