@@ -7,6 +7,7 @@ use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
7
7
use rustc_data_structures:: memmap:: Mmap ;
8
8
use rustc_data_structures:: stable_hasher:: { HashStable , StableHasher } ;
9
9
use rustc_data_structures:: sync:: { join, par_iter, Lrc , ParallelIterator } ;
10
+ use rustc_data_structures:: temp_dir:: MaybeTempDir ;
10
11
use rustc_hir as hir;
11
12
use rustc_hir:: def:: DefKind ;
12
13
use rustc_hir:: def_id:: {
@@ -39,9 +40,11 @@ use rustc_span::{
39
40
use rustc_target:: abi:: VariantIdx ;
40
41
use std:: borrow:: Borrow ;
41
42
use std:: hash:: Hash ;
43
+ use std:: io:: Write ;
42
44
use std:: iter;
43
45
use std:: num:: NonZeroUsize ;
44
- use std:: path:: Path ;
46
+ use std:: path:: { Path , PathBuf } ;
47
+ use tempfile:: Builder as TempFileBuilder ;
45
48
use tracing:: { debug, trace} ;
46
49
47
50
pub ( super ) struct EncodeContext < ' a , ' tcx > {
@@ -2138,25 +2141,25 @@ fn prefetch_mir(tcx: TyCtxt<'_>) {
2138
2141
2139
2142
pub struct EncodedMetadata {
2140
2143
mmap : Option < Mmap > ,
2141
- decoded : Vec < u8 > ,
2144
+ // We need to carry MaybeTempDir to avoid deleting the temporary
2145
+ // directory while accessing the Mmap.
2146
+ _temp_dir : Option < MaybeTempDir > ,
2142
2147
}
2143
2148
2144
2149
impl EncodedMetadata {
2145
2150
#[ inline]
2146
- pub fn from_file ( file : std:: fs:: File ) -> std:: io:: Result < Self > {
2151
+ pub fn from_path ( path : PathBuf , temp_dir : Option < MaybeTempDir > ) -> std:: io:: Result < Self > {
2152
+ let file = std:: fs:: File :: open ( & path) ?;
2147
2153
let file_metadata = file. metadata ( ) ?;
2148
2154
if file_metadata. len ( ) == 0 {
2149
- return Ok ( Self { mmap : None , decoded : Vec :: new ( ) } ) ;
2155
+ return Ok ( Self { mmap : None , _temp_dir : temp_dir } ) ;
2150
2156
}
2151
2157
let mmap = unsafe { Some ( Mmap :: map ( file) ?) } ;
2152
- Ok ( Self { mmap, decoded : Vec :: new ( ) } )
2158
+ Ok ( Self { mmap, _temp_dir : temp_dir } )
2153
2159
}
2154
2160
2155
2161
#[ inline]
2156
2162
pub fn raw_data ( & self ) -> & [ u8 ] {
2157
- if !self . decoded . is_empty ( ) {
2158
- return & self . decoded ;
2159
- }
2160
2163
self . mmap . as_ref ( ) . map ( |mmap| mmap. as_ref ( ) ) . unwrap_or_default ( )
2161
2164
}
2162
2165
}
@@ -2170,9 +2173,19 @@ impl<S: Encoder> Encodable<S> for EncodedMetadata {
2170
2173
2171
2174
impl < D : Decoder > Decodable < D > for EncodedMetadata {
2172
2175
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 }
2176
+ let temp_dir = TempFileBuilder :: new ( ) . prefix ( "decoded" ) . tempdir ( ) . unwrap ( ) ;
2177
+ let temp_dir = MaybeTempDir :: new ( temp_dir, false ) ;
2178
+ let filename = temp_dir. as_ref ( ) . join ( "decoded" ) ;
2179
+ let file = std:: fs:: File :: create ( & filename) . unwrap ( ) ;
2180
+ let mut file = std:: io:: BufWriter :: new ( file) ;
2181
+
2182
+ let len = d. read_usize ( ) ;
2183
+ for _ in 0 ..len {
2184
+ file. write ( & [ d. read_u8 ( ) ] ) . unwrap ( ) ;
2185
+ }
2186
+ file. flush ( ) . unwrap ( ) ;
2187
+
2188
+ Self :: from_path ( filename, Some ( temp_dir) ) . unwrap ( )
2176
2189
}
2177
2190
}
2178
2191
@@ -2269,5 +2282,5 @@ pub fn provide(providers: &mut Providers) {
2269
2282
} ,
2270
2283
2271
2284
..* providers
2272
- } ;
2285
+ }
2273
2286
}
0 commit comments