@@ -34,7 +34,7 @@ use rustc_span::def_id::DefId;
3434use rustc_span:: edition:: Edition ;
3535use rustc_span:: { DUMMY_SP , Ident , Span , Symbol , sym} ;
3636use rustc_target:: spec:: { PanicStrategy , Target } ;
37- use tracing:: { debug, info, trace } ;
37+ use tracing:: { debug, info} ;
3838
3939use crate :: diagnostics;
4040use crate :: locator:: { CrateError , CrateLocator , CratePaths , CrateRejections } ;
@@ -69,6 +69,9 @@ pub struct CStore {
6969 /// This crate has a `#[alloc_error_handler]` item.
7070 has_alloc_error_handler : bool ,
7171
72+ /// Cached map from hash to CrateNum, to avoid scanning metas during crate resolution.
73+ hash_to_cnum : UnordMap < Svh , CrateNum > ,
74+
7275 /// Names that were used to load the crates via `extern crate` or paths.
7376 resolved_externs : UnordMap < Symbol , CrateNum > ,
7477
@@ -237,6 +240,7 @@ impl CStore {
237240
238241 fn set_crate_data ( & mut self , cnum : CrateNum , data : CrateMetadata ) {
239242 assert ! ( self . metas[ cnum] . is_none( ) , "Overwriting crate metadata entry" ) ;
243+ self . hash_to_cnum . insert ( data. hash ( ) , cnum) ;
240244 self . metas [ cnum] = Some ( Box :: new ( data) ) ;
241245 }
242246
@@ -546,6 +550,7 @@ impl CStore {
546550 alloc_error_handler_kind : None ,
547551 has_global_allocator : false ,
548552 has_alloc_error_handler : false ,
553+ hash_to_cnum : UnordMap :: default ( ) ,
549554 resolved_externs : UnordMap :: default ( ) ,
550555 unused_externs : Vec :: new ( ) ,
551556 used_extern_options : Default :: default ( ) ,
@@ -555,21 +560,9 @@ impl CStore {
555560
556561 fn existing_match ( & self , name : Symbol , hash : Option < Svh > ) -> Option < CrateNum > {
557562 let hash = hash?;
558-
559- for ( cnum, data) in self . iter_crate_data ( ) {
560- if data. name ( ) != name {
561- trace ! ( "{} did not match {}" , data. name( ) , name) ;
562- continue ;
563- }
564-
565- if hash == data. hash ( ) {
566- return Some ( cnum) ;
567- } else {
568- debug ! ( "actual hash {} did not match expected {}" , hash, data. hash( ) ) ;
569- }
570- }
571-
572- None
563+ let cnum = * self . hash_to_cnum . get ( & hash) ?;
564+ debug_assert_eq ! ( self . get_crate_data( cnum) . name( ) , name) ;
565+ Some ( cnum)
573566 }
574567
575568 /// Determine whether a dependency should be considered private.
0 commit comments