@@ -692,7 +692,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
692692 let stable_order_of_exportable_impls =
693693 stat ! ( "exportable-items" , || self . encode_stable_order_of_exportable_impls( ) ) ;
694694
695- // Encode exported symbols info. This is prefetched in `encode_metadata`.
695+ // Encode exported symbols info. This is prefetched in `encode_metadata` so we encode
696+ // this as late as possible to give the prefetching as much time as possible to complete.
696697 let ( exported_non_generic_symbols, exported_generic_symbols) =
697698 stat ! ( "exported-symbols" , || {
698699 (
@@ -2362,6 +2363,28 @@ pub fn encode_metadata(tcx: TyCtxt<'_>, path: &Path, ref_path: Option<&Path>) {
23622363 // there's no need to do dep-graph tracking for any of it.
23632364 tcx. dep_graph . assert_ignored ( ) ;
23642365
2366+ join (
2367+ || encode_metadata_impl ( tcx, path, ref_path) ,
2368+ || {
2369+ if tcx. sess . threads ( ) == 1 {
2370+ return ;
2371+ }
2372+
2373+ // Prefetch some queries used by metadata encoding.
2374+ // This is not necessary for correctness, but is only done for performance reasons.
2375+ // It can be removed if it turns out to cause trouble or be detrimental to performance.
2376+ join (
2377+ || prefetch_mir ( tcx) ,
2378+ || {
2379+ let _ = tcx. exported_non_generic_symbols ( LOCAL_CRATE ) ;
2380+ let _ = tcx. exported_generic_symbols ( LOCAL_CRATE ) ;
2381+ } ,
2382+ ) ;
2383+ } ,
2384+ ) ;
2385+ }
2386+
2387+ fn encode_metadata_impl ( tcx : TyCtxt < ' _ > , path : & Path , ref_path : Option < & Path > ) {
23652388 // Generate the metadata stub manually, as that is a small file compared to full metadata.
23662389 if let Some ( ref_path) = ref_path {
23672390 let _prof_timer = tcx. prof . verbose_generic_activity ( "generate_crate_metadata_stub" ) ;
@@ -2399,19 +2422,6 @@ pub fn encode_metadata(tcx: TyCtxt<'_>, path: &Path, ref_path: Option<&Path>) {
23992422 return ;
24002423 } ;
24012424
2402- if tcx. sess . threads ( ) != 1 {
2403- // Prefetch some queries used by metadata encoding.
2404- // This is not necessary for correctness, but is only done for performance reasons.
2405- // It can be removed if it turns out to cause trouble or be detrimental to performance.
2406- join (
2407- || prefetch_mir ( tcx) ,
2408- || {
2409- let _ = tcx. exported_non_generic_symbols ( LOCAL_CRATE ) ;
2410- let _ = tcx. exported_generic_symbols ( LOCAL_CRATE ) ;
2411- } ,
2412- ) ;
2413- }
2414-
24152425 // Perform metadata encoding inside a task, so the dep-graph can check if any encoded
24162426 // information changes, and maybe reuse the work product.
24172427 tcx. dep_graph . with_task (
0 commit comments