1
Fork 0

hold Mmap in EncodedMetadata

This commit is contained in:
Yoshiki Matsuda 2022-04-29 12:24:58 +09:00
parent c26c461c0c
commit 8cfa7caac9
2 changed files with 34 additions and 13 deletions

View file

@ -94,8 +94,10 @@ pub fn encode_and_write_metadata(
} else { } else {
metadata_filename metadata_filename
}; };
let raw_data = std::fs::read(metadata_filename).unwrap(); let file = std::fs::File::open(metadata_filename).unwrap();
let metadata = EncodedMetadata::from_raw_data(raw_data); let metadata = EncodedMetadata::from_file(file).unwrap_or_else(|e| {
tcx.sess.fatal(&format!("failed to create encoded metadata from file: {}", e))
});
let need_metadata_module = metadata_kind == MetadataKind::Compressed; let need_metadata_module = metadata_kind == MetadataKind::Compressed;

View file

@ -4,6 +4,7 @@ use crate::rmeta::*;
use rustc_data_structures::fingerprint::Fingerprint; use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::fx::{FxHashMap, FxIndexSet}; use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
use rustc_data_structures::memmap::Mmap;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_data_structures::sync::{join, par_iter, Lrc, ParallelIterator}; use rustc_data_structures::sync::{join, par_iter, Lrc, ParallelIterator};
use rustc_hir as hir; use rustc_hir as hir;
@ -27,7 +28,7 @@ use rustc_middle::ty::codec::TyEncoder;
use rustc_middle::ty::fast_reject::{self, SimplifiedType, TreatParams}; use rustc_middle::ty::fast_reject::{self, SimplifiedType, TreatParams};
use rustc_middle::ty::query::Providers; use rustc_middle::ty::query::Providers;
use rustc_middle::ty::{self, SymbolName, Ty, TyCtxt}; use rustc_middle::ty::{self, SymbolName, Ty, TyCtxt};
use rustc_serialize::{opaque, Encodable, Encoder}; use rustc_serialize::{opaque, Decodable, Decoder, Encodable, Encoder};
use rustc_session::config::CrateType; use rustc_session::config::CrateType;
use rustc_session::cstore::{ForeignModule, LinkagePreference, NativeLib}; use rustc_session::cstore::{ForeignModule, LinkagePreference, NativeLib};
use rustc_span::hygiene::{ExpnIndex, HygieneEncodeContext, MacroKind}; use rustc_span::hygiene::{ExpnIndex, HygieneEncodeContext, MacroKind};
@ -2135,25 +2136,43 @@ fn prefetch_mir(tcx: TyCtxt<'_>) {
// will allow us to slice the metadata to the precise length that we just // will allow us to slice the metadata to the precise length that we just
// generated regardless of trailing bytes that end up in it. // generated regardless of trailing bytes that end up in it.
#[derive(Encodable, Decodable)]
pub struct EncodedMetadata { pub struct EncodedMetadata {
raw_data: Vec<u8>, mmap: Option<Mmap>,
decoded: Vec<u8>,
} }
impl EncodedMetadata { impl EncodedMetadata {
#[inline] #[inline]
pub fn new() -> EncodedMetadata { pub fn from_file(file: std::fs::File) -> std::io::Result<Self> {
EncodedMetadata { raw_data: Vec::new() } let file_metadata = file.metadata()?;
} if file_metadata.len() == 0 {
return Ok(Self { mmap: None, decoded: Vec::new() });
#[inline] }
pub fn from_raw_data(raw_data: Vec<u8>) -> Self { let mmap = unsafe { Some(Mmap::map(file)?) };
Self { raw_data } Ok(Self { mmap, decoded: Vec::new() })
} }
#[inline] #[inline]
pub fn raw_data(&self) -> &[u8] { pub fn raw_data(&self) -> &[u8] {
&self.raw_data if !self.decoded.is_empty() {
return &self.decoded;
}
self.mmap.as_ref().map(|mmap| mmap.as_ref()).unwrap_or_default()
}
}
impl<S: Encoder> Encodable<S> for EncodedMetadata {
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
let slice = self.raw_data();
slice.encode(s)
}
}
impl<D: Decoder> Decodable<D> for EncodedMetadata {
fn decode(d: &mut D) -> Self {
// FIXME: Write decorded data to a file and map to Mmap.
let decoded = Decodable::decode(d);
EncodedMetadata { mmap: None, decoded }
} }
} }