@@ -4,6 +4,7 @@ use crate::rmeta::*;
4
4
5
5
use rustc_data_structures:: fingerprint:: Fingerprint ;
6
6
use rustc_data_structures:: fx:: { FxHashMap , FxIndexSet } ;
7
+ use rustc_data_structures:: memmap:: Mmap ;
7
8
use rustc_data_structures:: stable_hasher:: { HashStable , StableHasher } ;
8
9
use rustc_data_structures:: sync:: { join, par_iter, Lrc , ParallelIterator } ;
9
10
use rustc_hir as hir;
@@ -27,7 +28,7 @@ use rustc_middle::ty::codec::TyEncoder;
27
28
use rustc_middle:: ty:: fast_reject:: { self , SimplifiedType , TreatParams } ;
28
29
use rustc_middle:: ty:: query:: Providers ;
29
30
use rustc_middle:: ty:: { self , SymbolName , Ty , TyCtxt } ;
30
- use rustc_serialize:: { opaque, Encodable , Encoder } ;
31
+ use rustc_serialize:: { opaque, Decodable , Decoder , Encodable , Encoder } ;
31
32
use rustc_session:: config:: CrateType ;
32
33
use rustc_session:: cstore:: { ForeignModule , LinkagePreference , NativeLib } ;
33
34
use rustc_span:: hygiene:: { ExpnIndex , HygieneEncodeContext , MacroKind } ;
@@ -2135,25 +2136,43 @@ fn prefetch_mir(tcx: TyCtxt<'_>) {
2135
2136
// will allow us to slice the metadata to the precise length that we just
2136
2137
// generated regardless of trailing bytes that end up in it.
2137
2138
2138
- #[ derive( Encodable , Decodable ) ]
2139
2139
pub struct EncodedMetadata {
2140
- raw_data : Vec < u8 > ,
2140
+ mmap : Option < Mmap > ,
2141
+ decoded : Vec < u8 > ,
2141
2142
}
2142
2143
2143
2144
impl EncodedMetadata {
2144
2145
#[ 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 ( ) } )
2147
2153
}
2148
2154
2149
2155
#[ 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 ( )
2152
2161
}
2162
+ }
2153
2163
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 }
2157
2176
}
2158
2177
}
2159
2178
0 commit comments