@@ -205,6 +205,7 @@ where
205205 let mut codegen_units = UnordMap :: default ( ) ;
206206 let is_incremental_build = cx. tcx . sess . opts . incremental . is_some ( ) ;
207207 let mut internalization_candidates = UnordSet :: default ( ) ;
208+ let mut cgu_def_id_cache = UnordMap :: default ( ) ;
208209
209210 // Determine if monomorphizations instantiated in this crate will be made
210211 // available to downstream crates. This depends on whether we are in
@@ -234,13 +235,44 @@ where
234235 let is_volatile = is_incremental_build && mono_item. is_generic_fn ( ) ;
235236
236237 let cgu_name = match characteristic_def_id {
237- Some ( def_id) => compute_codegen_unit_name (
238- cx. tcx ,
239- cgu_name_builder,
240- def_id,
241- is_volatile,
242- cgu_name_cache,
243- ) ,
238+ Some ( def_id) => {
239+ let cgu_def_id = * cgu_def_id_cache. entry ( def_id) . or_insert_with ( || {
240+ // Find the innermost module that is not nested within a function.
241+ let mut current_def_id = def_id;
242+ let mut cgu_def_id = None ;
243+ // Walk backwards from the item we want to find the module for.
244+ loop {
245+ if current_def_id. is_crate_root ( ) {
246+ if cgu_def_id. is_none ( ) {
247+ // If we have not found a module yet, take the crate root.
248+ cgu_def_id = Some ( def_id. krate . as_def_id ( ) ) ;
249+ }
250+ break ;
251+ } else if cx. tcx . def_kind ( current_def_id) == DefKind :: Mod {
252+ if cgu_def_id. is_none ( ) {
253+ cgu_def_id = Some ( current_def_id) ;
254+ }
255+ } else {
256+ // If we encounter something that is not a module, throw away
257+ // any module that we've found so far because we now know that
258+ // it is nested within something else.
259+ cgu_def_id = None ;
260+ }
261+
262+ current_def_id = cx. tcx . parent ( current_def_id) ;
263+ }
264+
265+ cgu_def_id. unwrap ( )
266+ } ) ;
267+
268+ compute_codegen_unit_name (
269+ cx. tcx ,
270+ cgu_name_builder,
271+ cgu_def_id,
272+ is_volatile,
273+ cgu_name_cache,
274+ )
275+ }
244276 None => fallback_cgu_name ( cgu_name_builder) ,
245277 } ;
246278
@@ -693,37 +725,10 @@ fn characteristic_def_id_of_mono_item<'tcx>(
693725fn compute_codegen_unit_name (
694726 tcx : TyCtxt < ' _ > ,
695727 name_builder : & mut CodegenUnitNameBuilder < ' _ > ,
696- def_id : DefId ,
728+ cgu_def_id : DefId ,
697729 volatile : bool ,
698730 cache : & mut CguNameCache ,
699731) -> Symbol {
700- // Find the innermost module that is not nested within a function.
701- let mut current_def_id = def_id;
702- let mut cgu_def_id = None ;
703- // Walk backwards from the item we want to find the module for.
704- loop {
705- if current_def_id. is_crate_root ( ) {
706- if cgu_def_id. is_none ( ) {
707- // If we have not found a module yet, take the crate root.
708- cgu_def_id = Some ( def_id. krate . as_def_id ( ) ) ;
709- }
710- break ;
711- } else if tcx. def_kind ( current_def_id) == DefKind :: Mod {
712- if cgu_def_id. is_none ( ) {
713- cgu_def_id = Some ( current_def_id) ;
714- }
715- } else {
716- // If we encounter something that is not a module, throw away
717- // any module that we've found so far because we now know that
718- // it is nested within something else.
719- cgu_def_id = None ;
720- }
721-
722- current_def_id = tcx. parent ( current_def_id) ;
723- }
724-
725- let cgu_def_id = cgu_def_id. unwrap ( ) ;
726-
727732 * cache. entry ( ( cgu_def_id, volatile) ) . or_insert_with ( || {
728733 let def_path = tcx. def_path ( cgu_def_id) ;
729734
0 commit comments