Skip to content

Commit 8cfa7ca

Browse files
committed
hold Mmap in EncodedMetadata
1 parent c26c461 commit 8cfa7ca

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

compiler/rustc_metadata/src/fs.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,10 @@ pub fn encode_and_write_metadata(
9494
} else {
9595
metadata_filename
9696
};
97-
let raw_data = std::fs::read(metadata_filename).unwrap();
98-
let metadata = EncodedMetadata::from_raw_data(raw_data);
97+
let file = std::fs::File::open(metadata_filename).unwrap();
98+
let metadata = EncodedMetadata::from_file(file).unwrap_or_else(|e| {
99+
tcx.sess.fatal(&format!("failed to create encoded metadata from file: {}", e))
100+
});
99101

100102
let need_metadata_module = metadata_kind == MetadataKind::Compressed;
101103

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::rmeta::*;
44

55
use rustc_data_structures::fingerprint::Fingerprint;
66
use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
7+
use rustc_data_structures::memmap::Mmap;
78
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
89
use rustc_data_structures::sync::{join, par_iter, Lrc, ParallelIterator};
910
use rustc_hir as hir;
@@ -27,7 +28,7 @@ use rustc_middle::ty::codec::TyEncoder;
2728
use rustc_middle::ty::fast_reject::{self, SimplifiedType, TreatParams};
2829
use rustc_middle::ty::query::Providers;
2930
use rustc_middle::ty::{self, SymbolName, Ty, TyCtxt};
30-
use rustc_serialize::{opaque, Encodable, Encoder};
31+
use rustc_serialize::{opaque, Decodable, Decoder, Encodable, Encoder};
3132
use rustc_session::config::CrateType;
3233
use rustc_session::cstore::{ForeignModule, LinkagePreference, NativeLib};
3334
use rustc_span::hygiene::{ExpnIndex, HygieneEncodeContext, MacroKind};
@@ -2135,25 +2136,43 @@ fn prefetch_mir(tcx: TyCtxt<'_>) {
21352136
// will allow us to slice the metadata to the precise length that we just
21362137
// generated regardless of trailing bytes that end up in it.
21372138

2138-
#[derive(Encodable, Decodable)]
21392139
pub struct EncodedMetadata {
2140-
raw_data: Vec<u8>,
2140+
mmap: Option<Mmap>,
2141+
decoded: Vec<u8>,
21412142
}
21422143

21432144
impl EncodedMetadata {
21442145
#[inline]
2145-
pub fn new() -> EncodedMetadata {
2146-
EncodedMetadata { raw_data: Vec::new() }
2146+
pub fn from_file(file: std::fs::File) -> std::io::Result<Self> {
2147+
let file_metadata = file.metadata()?;
2148+
if file_metadata.len() == 0 {
2149+
return Ok(Self { mmap: None, decoded: Vec::new() });
2150+
}
2151+
let mmap = unsafe { Some(Mmap::map(file)?) };
2152+
Ok(Self { mmap, decoded: Vec::new() })
21472153
}
21482154

21492155
#[inline]
2150-
pub fn from_raw_data(raw_data: Vec<u8>) -> Self {
2151-
Self { raw_data }
2156+
pub fn raw_data(&self) -> &[u8] {
2157+
if !self.decoded.is_empty() {
2158+
return &self.decoded;
2159+
}
2160+
self.mmap.as_ref().map(|mmap| mmap.as_ref()).unwrap_or_default()
21522161
}
2162+
}
21532163

2154-
#[inline]
2155-
pub fn raw_data(&self) -> &[u8] {
2156-
&self.raw_data
2164+
impl<S: Encoder> Encodable<S> for EncodedMetadata {
2165+
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
2166+
let slice = self.raw_data();
2167+
slice.encode(s)
2168+
}
2169+
}
2170+
2171+
impl<D: Decoder> Decodable<D> for EncodedMetadata {
2172+
fn decode(d: &mut D) -> Self {
2173+
// FIXME: Write decorded data to a file and map to Mmap.
2174+
let decoded = Decodable::decode(d);
2175+
EncodedMetadata { mmap: None, decoded }
21572176
}
21582177
}
21592178

0 commit comments

Comments
 (0)