@@ -2,10 +2,12 @@ use crate::dep_graph::DepNodeIndex;
22
33use rustc_data_structures:: fx:: FxHashMap ;
44use rustc_data_structures:: sharded;
5- #[ cfg( parallel_compiler) ]
65use rustc_data_structures:: sharded:: Sharded ;
76use rustc_data_structures:: sync:: Lock ;
87use rustc_index:: { Idx , IndexVec } ;
8+ use rustc_span:: def_id:: CrateNum ;
9+ use rustc_span:: def_id:: DefId ;
10+ use rustc_span:: def_id:: DefIndex ;
911use std:: fmt:: Debug ;
1012use std:: hash:: Hash ;
1113use std:: marker:: PhantomData ;
@@ -212,3 +214,60 @@ where
212214 }
213215 }
214216}
217+
218+ pub struct DefIdCacheSelector ;
219+
220+ impl < ' tcx , V : ' tcx > CacheSelector < ' tcx , V > for DefIdCacheSelector {
221+ type Cache = DefIdCache < V >
222+ where
223+ V : Copy ;
224+ }
225+
226+ pub struct DefIdCache < V > {
227+ cache : Sharded < IndexVec < CrateNum , FxHashMap < DefIndex , ( V , DepNodeIndex ) > > > ,
228+ }
229+
230+ impl < V > Default for DefIdCache < V > {
231+ fn default ( ) -> Self {
232+ Self { cache : Default :: default ( ) }
233+ }
234+ }
235+
236+ impl < V > QueryCache for DefIdCache < V >
237+ where
238+ V : Copy ,
239+ {
240+ type Key = DefId ;
241+ type Value = V ;
242+
243+ #[ inline( always) ]
244+ fn lookup ( & self , key : & DefId ) -> Option < ( V , DepNodeIndex ) > {
245+ let key_hash = sharded:: make_hash ( & key. index ) ;
246+ let lock = self . cache . get_shard_by_hash ( key_hash) . lock ( ) ;
247+ if let Some ( defs) = lock. get ( key. krate ) {
248+ let result = defs. raw_entry ( ) . from_key_hashed_nocheck ( key_hash, & key. index ) ;
249+ if let Some ( ( _, value) ) = result { Some ( * value) } else { None }
250+ } else {
251+ None
252+ }
253+ }
254+
255+ #[ inline]
256+ fn complete ( & self , key : DefId , value : V , index : DepNodeIndex ) {
257+ let key_hash = sharded:: make_hash ( & key. index ) ;
258+ let mut lock = self . cache . get_shard_by_hash ( key_hash) . lock ( ) ;
259+ lock. ensure_contains_elem ( key. krate , || FxHashMap :: default ( ) ) ;
260+ lock[ key. krate ] . insert ( key. index , ( value, index) ) ;
261+ }
262+
263+ fn iter ( & self , f : & mut dyn FnMut ( & Self :: Key , & Self :: Value , DepNodeIndex ) ) {
264+ let shards = self . cache . lock_shards ( ) ;
265+ for shard in shards. iter ( ) {
266+ for ( k, v) in shard. iter_enumerated ( ) {
267+ for ( & index, v) in v. iter ( ) {
268+ f ( & DefId { krate : k, index } , & v. 0 , v. 1 ) ;
269+ }
270+ }
271+ }
272+ }
273+ }
0 commit comments