@@ -4,6 +4,7 @@ use crate::rmeta::*;
44
55use rustc_data_structures:: fingerprint:: Fingerprint ;
66use rustc_data_structures:: fx:: { FxHashMap , FxIndexSet } ;
7+ use rustc_data_structures:: memmap:: Mmap ;
78use rustc_data_structures:: stable_hasher:: { HashStable , StableHasher } ;
89use rustc_data_structures:: sync:: { join, par_iter, Lrc , ParallelIterator } ;
910use rustc_hir as hir;
@@ -27,7 +28,7 @@ use rustc_middle::ty::codec::TyEncoder;
2728use rustc_middle:: ty:: fast_reject:: { self , SimplifiedType , TreatParams } ;
2829use rustc_middle:: ty:: query:: Providers ;
2930use rustc_middle:: ty:: { self , SymbolName , Ty , TyCtxt } ;
30- use rustc_serialize:: { opaque, Encodable , Encoder } ;
31+ use rustc_serialize:: { opaque, Decodable , Decoder , Encodable , Encoder } ;
3132use rustc_session:: config:: CrateType ;
3233use rustc_session:: cstore:: { ForeignModule , LinkagePreference , NativeLib } ;
3334use 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 ) ]
21392139pub struct EncodedMetadata {
2140- raw_data : Vec < u8 > ,
2140+ mmap : Option < Mmap > ,
2141+ decoded : Vec < u8 > ,
21412142}
21422143
21432144impl 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