11use std:: borrow:: Cow ;
22use std:: cmp:: Ordering ;
33use std:: collections:: BTreeSet ;
4- use std:: sync:: Arc ;
4+ use std:: sync:: { Arc , RwLock } ;
55
66use ahash:: { HashMap , HashSet , HashSetExt } ;
77use anyhow:: { Context , anyhow} ;
@@ -181,8 +181,8 @@ impl CrateCollection {
181181 cache : & RustdocGlobalFsCache ,
182182 diagnostic_sink : & DiagnosticSink ,
183183 ) -> ( PackageId , Option < Crate > ) {
184- let cache_key = RustdocCacheKey :: new ( & package_id, & package_graph) ;
185- match cache. get ( & cache_key, & package_graph) {
184+ let cache_key = RustdocCacheKey :: new ( & package_id, package_graph) ;
185+ match cache. get ( & cache_key, package_graph) {
186186 Ok ( None ) => ( package_id, None ) ,
187187 Ok ( Some ( entry) ) => (
188188 package_id. clone ( ) ,
@@ -212,7 +212,7 @@ impl CrateCollection {
212212 let sink = & self . diagnostic_sink ;
213213 let tracing_span = Span :: current ( ) ;
214214 let map_op =
215- move |id| tracing_span. in_scope ( || get_if_cached ( id, & package_graph, cache, & sink) ) ;
215+ move |id| tracing_span. in_scope ( || get_if_cached ( id, package_graph, cache, sink) ) ;
216216
217217 let mut to_be_computed = vec ! [ ] ;
218218
@@ -262,11 +262,11 @@ impl CrateCollection {
262262 . par_iter ( )
263263 . filter_map ( |( package_id, krate, cache_indexes) | {
264264 let data = if * cache_indexes {
265- CacheEntry :: new ( & krate)
265+ CacheEntry :: new ( krate)
266266 } else {
267- CacheEntry :: raw ( & krate)
267+ CacheEntry :: raw ( krate)
268268 } ;
269- let cache_key = RustdocCacheKey :: new ( & package_id, package_graph) ;
269+ let cache_key = RustdocCacheKey :: new ( package_id, package_graph) ;
270270 match data {
271271 Ok ( v) => Some ( ( package_id, ( cache_key, v) ) ) ,
272272 Err ( e) => {
@@ -660,8 +660,15 @@ pub struct Crate {
660660 /// An internal cache to avoid traversing the package graph every time we need to
661661 /// translate a crate id into a package id via [`Self::compute_package_id_for_crate_id`]
662662 /// or [`Self::compute_package_id_for_crate_id_with_hint`].
663- pub ( super ) crate_id2package_id :
664- Arc < std:: sync:: RwLock < HashMap < ( u32 , Option < String > ) , PackageId > > > ,
663+ pub ( super ) crate_id2package_id : Arc < RwLock < HashMap < CrateIdNeedle , PackageId > > > ,
664+ }
665+
666+ /// The information used by [`Self::compute_package_id_for_crate_id_with_hint`] to
667+ /// map a `crate_id` to a `package_id`.
668+ #[ derive( Debug , Clone , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
669+ pub struct CrateIdNeedle {
670+ crate_id : u32 ,
671+ maybe_dependent_crate_name : Option < String > ,
665672}
666673
667674#[ derive( Debug , Clone ) ]
@@ -1350,13 +1357,12 @@ impl Crate {
13501357 collection : & CrateCollection ,
13511358 maybe_dependent_crate_name : Option < & str > ,
13521359 ) -> Result < PackageId , anyhow:: Error > {
1360+ let needle = CrateIdNeedle {
1361+ crate_id,
1362+ maybe_dependent_crate_name : maybe_dependent_crate_name. map ( |s| s. to_owned ( ) ) ,
1363+ } ;
13531364 // Check the cache first.
1354- if let Some ( package_id) = self
1355- . crate_id2package_id
1356- . read ( )
1357- . unwrap ( )
1358- . get ( & ( crate_id, maybe_dependent_crate_name. map ( |s| s. to_owned ( ) ) ) )
1359- {
1365+ if let Some ( package_id) = self . crate_id2package_id . read ( ) . unwrap ( ) . get ( & needle) {
13601366 return Ok ( package_id. to_owned ( ) ) ;
13611367 }
13621368
@@ -1369,10 +1375,10 @@ impl Crate {
13691375
13701376 // If successful, cache the outcome.
13711377 if let Ok ( outcome) = & outcome {
1372- self . crate_id2package_id . write ( ) . unwrap ( ) . insert (
1373- ( crate_id , maybe_dependent_crate_name . map ( |s| s . to_owned ( ) ) ) ,
1374- outcome . to_owned ( ) ,
1375- ) ;
1378+ self . crate_id2package_id
1379+ . write ( )
1380+ . unwrap ( )
1381+ . insert ( needle , outcome . to_owned ( ) ) ;
13761382 }
13771383 outcome
13781384 }
0 commit comments