@@ -703,6 +703,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
703703 triple: tcx. sess. opts. target_triple. clone( ) ,
704704 hash: tcx. crate_hash( LOCAL_CRATE ) ,
705705 is_proc_macro_crate: proc_macro_data. is_some( ) ,
706+ is_stub: false ,
706707 } ,
707708 extra_filename: tcx. sess. opts. cg. extra_filename. clone( ) ,
708709 stable_crate_id: tcx. def_path_hash( LOCAL_CRATE . as_def_id( ) ) . stable_crate_id( ) ,
@@ -2234,54 +2235,75 @@ fn prefetch_mir(tcx: TyCtxt<'_>) {
22342235// generated regardless of trailing bytes that end up in it.
22352236
22362237pub 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 > > ,
22392244 // We need to carry MaybeTempDir to avoid deleting the temporary
22402245 // directory while accessing the Mmap.
22412246 _temp_dir : Option < MaybeTempDir > ,
22422247}
22432248
22442249impl EncodedMetadata {
22452250 #[ 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 > {
22472256 let file = std:: fs:: File :: open ( & path) ?;
22482257 let file_metadata = file. metadata ( ) ?;
22492258 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 } ) ;
22512260 }
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 ( )
22542272 }
22552273
22562274 #[ 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 ( ) )
22592277 }
22602278}
22612279
22622280impl < S : Encoder > Encodable < S > for EncodedMetadata {
22632281 fn encode ( & self , s : & mut S ) {
2264- let slice = self . raw_data ( ) ;
2282+ self . stub_metadata . encode ( s) ;
2283+
2284+ let slice = self . full ( ) ;
22652285 slice. encode ( s)
22662286 }
22672287}
22682288
22692289impl < D : Decoder > Decodable < D > for EncodedMetadata {
22702290 fn decode ( d : & mut D ) -> Self {
2291+ let stub = <Option < Vec < u8 > > >:: decode ( d) ;
2292+
22712293 let len = d. read_usize ( ) ;
2272- let mmap = if len > 0 {
2294+ let full_metadata = if len > 0 {
22732295 let mut mmap = MmapMut :: map_anon ( len) . unwrap ( ) ;
22742296 mmap. copy_from_slice ( d. read_raw_bytes ( len) ) ;
22752297 Some ( mmap. make_read_only ( ) . unwrap ( ) )
22762298 } else {
22772299 None
22782300 } ;
22792301
2280- Self { mmap , _temp_dir : None }
2302+ Self { full_metadata , stub_metadata : stub , _temp_dir : None }
22812303 }
22822304}
22832305
2284- pub fn encode_metadata ( tcx : TyCtxt < ' _ > , path : & Path ) {
2306+ pub fn encode_metadata ( tcx : TyCtxt < ' _ > , path : & Path , ref_path : Option < & Path > ) {
22852307 let _prof_timer = tcx. prof . verbose_generic_activity ( "generate_crate_metadata" ) ;
22862308
22872309 // Since encoding metadata is not in a query, and nothing is cached,
@@ -2295,6 +2317,42 @@ pub fn encode_metadata(tcx: TyCtxt<'_>, path: &Path) {
22952317 join ( || prefetch_mir ( tcx) , || tcx. exported_symbols ( LOCAL_CRATE ) ) ;
22962318 }
22972319
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+ ) {
22982356 let mut encoder = opaque:: FileEncoder :: new ( path)
22992357 . unwrap_or_else ( |err| tcx. dcx ( ) . emit_fatal ( FailCreateFileEncoder { err } ) ) ;
23002358 encoder. emit_raw_bytes ( METADATA_HEADER ) ;
@@ -2329,9 +2387,7 @@ pub fn encode_metadata(tcx: TyCtxt<'_>, path: &Path) {
23292387 // Encode the rustc version string in a predictable location.
23302388 rustc_version ( tcx. sess . cfg_version ) . encode ( & mut ecx) ;
23312389
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) ;
23352391
23362392 // Make sure we report any errors from writing to the file.
23372393 // If we forget this, compilation can succeed with an incomplete rmeta file,
@@ -2341,12 +2397,9 @@ pub fn encode_metadata(tcx: TyCtxt<'_>, path: &Path) {
23412397 }
23422398
23432399 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 ) {
23452401 tcx. dcx ( ) . emit_fatal ( FailWriteFile { path : ecx. opaque . path ( ) , err } ) ;
23462402 }
2347-
2348- // Record metadata size for self-profiling
2349- tcx. prof . artifact_size ( "crate_metadata" , "crate_metadata" , file. metadata ( ) . unwrap ( ) . len ( ) ) ;
23502403}
23512404
23522405fn encode_root_position ( mut file : & File , pos : usize ) -> Result < ( ) , std:: io:: Error > {
0 commit comments