@@ -665,7 +665,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
665665 let debugger_visualizers =
666666 stat ! ( "debugger-visualizers" , || self . encode_debugger_visualizers( ) ) ;
667667
668- // Encode exported symbols info. This is prefetched in `encode_metadata`.
668+ // Encode exported symbols info. This is prefetched in `encode_metadata` so we encode
669+ // this as late as possible to give the prefetching as much time as possible to complete.
669670 let exported_symbols = stat ! ( "exported-symbols" , || {
670671 self . encode_exported_symbols( tcx. exported_symbols( LOCAL_CRATE ) )
671672 } ) ;
@@ -2203,13 +2204,21 @@ pub fn encode_metadata(tcx: TyCtxt<'_>, path: &Path) {
22032204 // there's no need to do dep-graph tracking for any of it.
22042205 tcx. dep_graph . assert_ignored ( ) ;
22052206
2206- if tcx. sess . threads ( ) != 1 {
2207- // Prefetch some queries used by metadata encoding.
2208- // This is not necessary for correctness, but is only done for performance reasons.
2209- // It can be removed if it turns out to cause trouble or be detrimental to performance.
2210- join ( || prefetch_mir ( tcx) , || tcx. exported_symbols ( LOCAL_CRATE ) ) ;
2211- }
2207+ join (
2208+ || encode_metadata_impl ( tcx, path) ,
2209+ || {
2210+ if tcx. sess . threads ( ) == 1 {
2211+ return ;
2212+ }
2213+ // Prefetch some queries used by metadata encoding.
2214+ // This is not necessary for correctness, but is only done for performance reasons.
2215+ // It can be removed if it turns out to cause trouble or be detrimental to performance.
2216+ join ( || prefetch_mir ( tcx) , || tcx. exported_symbols ( LOCAL_CRATE ) ) ;
2217+ } ,
2218+ ) ;
2219+ }
22122220
2221+ fn encode_metadata_impl ( tcx : TyCtxt < ' _ > , path : & Path ) {
22132222 let mut encoder = opaque:: FileEncoder :: new ( path)
22142223 . unwrap_or_else ( |err| tcx. dcx ( ) . emit_fatal ( FailCreateFileEncoder { err } ) ) ;
22152224 encoder. emit_raw_bytes ( METADATA_HEADER ) ;
0 commit comments