@@ -703,6 +703,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
703
703
triple: tcx. sess. opts. target_triple. clone( ) ,
704
704
hash: tcx. crate_hash( LOCAL_CRATE ) ,
705
705
is_proc_macro_crate: proc_macro_data. is_some( ) ,
706
+ is_stub: false ,
706
707
} ,
707
708
extra_filename: tcx. sess. opts. cg. extra_filename. clone( ) ,
708
709
stable_crate_id: tcx. def_path_hash( LOCAL_CRATE . as_def_id( ) ) . stable_crate_id( ) ,
@@ -2234,54 +2235,75 @@ fn prefetch_mir(tcx: TyCtxt<'_>) {
2234
2235
// generated regardless of trailing bytes that end up in it.
2235
2236
2236
2237
pub struct EncodedMetadata {
2237
- // The declaration order matters because `mmap` should be dropped before `_temp_dir`.
2238
- mmap : Option < Mmap > ,
2238
+ // The declaration order matters because `full_metadata` should be dropped
2239
+ // before `_temp_dir`.
2240
+ full_metadata : Option < Mmap > ,
2241
+ // This is an optional stub metadata containing only the crate header.
2242
+ // The header should be very small, so we load it directly into memory.
2243
+ stub_metadata : Option < Vec < u8 > > ,
2239
2244
// We need to carry MaybeTempDir to avoid deleting the temporary
2240
2245
// directory while accessing the Mmap.
2241
2246
_temp_dir : Option < MaybeTempDir > ,
2242
2247
}
2243
2248
2244
2249
impl EncodedMetadata {
2245
2250
#[ inline]
2246
- pub fn from_path ( path : PathBuf , temp_dir : Option < MaybeTempDir > ) -> std:: io:: Result < Self > {
2251
+ pub fn from_path (
2252
+ path : PathBuf ,
2253
+ stub_path : Option < PathBuf > ,
2254
+ temp_dir : Option < MaybeTempDir > ,
2255
+ ) -> std:: io:: Result < Self > {
2247
2256
let file = std:: fs:: File :: open ( & path) ?;
2248
2257
let file_metadata = file. metadata ( ) ?;
2249
2258
if file_metadata. len ( ) == 0 {
2250
- return Ok ( Self { mmap : None , _temp_dir : None } ) ;
2259
+ return Ok ( Self { full_metadata : None , stub_metadata : None , _temp_dir : None } ) ;
2251
2260
}
2252
- let mmap = unsafe { Some ( Mmap :: map ( file) ?) } ;
2253
- Ok ( Self { mmap, _temp_dir : temp_dir } )
2261
+ let full_mmap = unsafe { Some ( Mmap :: map ( file) ?) } ;
2262
+
2263
+ let stub =
2264
+ if let Some ( stub_path) = stub_path { Some ( std:: fs:: read ( stub_path) ?) } else { None } ;
2265
+
2266
+ Ok ( Self { full_metadata : full_mmap, stub_metadata : stub, _temp_dir : temp_dir } )
2267
+ }
2268
+
2269
+ #[ inline]
2270
+ pub fn full ( & self ) -> & [ u8 ] {
2271
+ & self . full_metadata . as_deref ( ) . unwrap_or_default ( )
2254
2272
}
2255
2273
2256
2274
#[ inline]
2257
- pub fn raw_data ( & self ) -> & [ u8 ] {
2258
- self . mmap . as_deref ( ) . unwrap_or_default ( )
2275
+ pub fn stub_or_full ( & self ) -> & [ u8 ] {
2276
+ self . stub_metadata . as_deref ( ) . unwrap_or ( self . full ( ) )
2259
2277
}
2260
2278
}
2261
2279
2262
2280
impl < S : Encoder > Encodable < S > for EncodedMetadata {
2263
2281
fn encode ( & self , s : & mut S ) {
2264
- let slice = self . raw_data ( ) ;
2282
+ self . stub_metadata . encode ( s) ;
2283
+
2284
+ let slice = self . full ( ) ;
2265
2285
slice. encode ( s)
2266
2286
}
2267
2287
}
2268
2288
2269
2289
impl < D : Decoder > Decodable < D > for EncodedMetadata {
2270
2290
fn decode ( d : & mut D ) -> Self {
2291
+ let stub = <Option < Vec < u8 > > >:: decode ( d) ;
2292
+
2271
2293
let len = d. read_usize ( ) ;
2272
- let mmap = if len > 0 {
2294
+ let full_metadata = if len > 0 {
2273
2295
let mut mmap = MmapMut :: map_anon ( len) . unwrap ( ) ;
2274
2296
mmap. copy_from_slice ( d. read_raw_bytes ( len) ) ;
2275
2297
Some ( mmap. make_read_only ( ) . unwrap ( ) )
2276
2298
} else {
2277
2299
None
2278
2300
} ;
2279
2301
2280
- Self { mmap , _temp_dir : None }
2302
+ Self { full_metadata , stub_metadata : stub , _temp_dir : None }
2281
2303
}
2282
2304
}
2283
2305
2284
- pub fn encode_metadata ( tcx : TyCtxt < ' _ > , path : & Path ) {
2306
+ pub fn encode_metadata ( tcx : TyCtxt < ' _ > , path : & Path , ref_path : Option < & Path > ) {
2285
2307
let _prof_timer = tcx. prof . verbose_generic_activity ( "generate_crate_metadata" ) ;
2286
2308
2287
2309
// Since encoding metadata is not in a query, and nothing is cached,
@@ -2295,6 +2317,42 @@ pub fn encode_metadata(tcx: TyCtxt<'_>, path: &Path) {
2295
2317
join ( || prefetch_mir ( tcx) , || tcx. exported_symbols ( LOCAL_CRATE ) ) ;
2296
2318
}
2297
2319
2320
+ with_encode_metadata_header ( tcx, path, |ecx| {
2321
+ // Encode all the entries and extra information in the crate,
2322
+ // culminating in the `CrateRoot` which points to all of it.
2323
+ let root = ecx. encode_crate_root ( ) ;
2324
+
2325
+ // Flush buffer to ensure backing file has the correct size.
2326
+ ecx. opaque . flush ( ) ;
2327
+ // Record metadata size for self-profiling
2328
+ tcx. prof . artifact_size (
2329
+ "crate_metadata" ,
2330
+ "crate_metadata" ,
2331
+ ecx. opaque . file ( ) . metadata ( ) . unwrap ( ) . len ( ) ,
2332
+ ) ;
2333
+
2334
+ root. position . get ( )
2335
+ } ) ;
2336
+
2337
+ if let Some ( ref_path) = ref_path {
2338
+ with_encode_metadata_header ( tcx, ref_path, |ecx| {
2339
+ let header: LazyValue < CrateHeader > = ecx. lazy ( CrateHeader {
2340
+ name : tcx. crate_name ( LOCAL_CRATE ) ,
2341
+ triple : tcx. sess . opts . target_triple . clone ( ) ,
2342
+ hash : tcx. crate_hash ( LOCAL_CRATE ) ,
2343
+ is_proc_macro_crate : false ,
2344
+ is_stub : true ,
2345
+ } ) ;
2346
+ header. position . get ( )
2347
+ } ) ;
2348
+ }
2349
+ }
2350
+
2351
+ fn with_encode_metadata_header (
2352
+ tcx : TyCtxt < ' _ > ,
2353
+ path : & Path ,
2354
+ f : impl FnOnce ( & mut EncodeContext < ' _ , ' _ > ) -> usize ,
2355
+ ) {
2298
2356
let mut encoder = opaque:: FileEncoder :: new ( path)
2299
2357
. unwrap_or_else ( |err| tcx. dcx ( ) . emit_fatal ( FailCreateFileEncoder { err } ) ) ;
2300
2358
encoder. emit_raw_bytes ( METADATA_HEADER ) ;
@@ -2329,9 +2387,7 @@ pub fn encode_metadata(tcx: TyCtxt<'_>, path: &Path) {
2329
2387
// Encode the rustc version string in a predictable location.
2330
2388
rustc_version ( tcx. sess . cfg_version ) . encode ( & mut ecx) ;
2331
2389
2332
- // Encode all the entries and extra information in the crate,
2333
- // culminating in the `CrateRoot` which points to all of it.
2334
- let root = ecx. encode_crate_root ( ) ;
2390
+ let root_position = f ( & mut ecx) ;
2335
2391
2336
2392
// Make sure we report any errors from writing to the file.
2337
2393
// If we forget this, compilation can succeed with an incomplete rmeta file,
@@ -2341,12 +2397,9 @@ pub fn encode_metadata(tcx: TyCtxt<'_>, path: &Path) {
2341
2397
}
2342
2398
2343
2399
let file = ecx. opaque . file ( ) ;
2344
- if let Err ( err) = encode_root_position ( file, root . position . get ( ) ) {
2400
+ if let Err ( err) = encode_root_position ( file, root_position ) {
2345
2401
tcx. dcx ( ) . emit_fatal ( FailWriteFile { path : ecx. opaque . path ( ) , err } ) ;
2346
2402
}
2347
-
2348
- // Record metadata size for self-profiling
2349
- tcx. prof . artifact_size ( "crate_metadata" , "crate_metadata" , file. metadata ( ) . unwrap ( ) . len ( ) ) ;
2350
2403
}
2351
2404
2352
2405
fn encode_root_position ( mut file : & File , pos : usize ) -> Result < ( ) , std:: io:: Error > {
0 commit comments