11use crate :: dep_graph:: DepNodeIndex ;
2- use crate :: query:: plumbing:: { QueryCacheStore , QueryLookup } ;
2+ use crate :: query:: plumbing:: QueryLookup ;
33
44use rustc_arena:: TypedArena ;
55use rustc_data_structures:: fx:: FxHashMap ;
6- use rustc_data_structures:: sharded:: Sharded ;
6+ use rustc_data_structures:: sharded:: { self , Sharded } ;
77use rustc_data_structures:: sync:: WorkerLocal ;
88use std:: default:: Default ;
99use std:: fmt:: Debug ;
@@ -25,35 +25,23 @@ pub trait QueryStorage {
2525
2626pub trait QueryCache : QueryStorage + Sized {
2727 type Key : Hash + Eq + Clone + Debug ;
28- type Sharded : Default ;
2928
3029 /// Checks if the query is already computed and in the cache.
3130 /// It returns the shard index and a lock guard to the shard,
3231 /// which will be used if the query is not in the cache and we need
3332 /// to compute it.
34- fn lookup < ' s , R , OnHit > (
33+ fn lookup < R , OnHit > (
3534 & self ,
36- state : & ' s QueryCacheStore < Self > ,
3735 key : & Self :: Key ,
3836 // `on_hit` can be called while holding a lock to the query state shard.
3937 on_hit : OnHit ,
4038 ) -> Result < R , QueryLookup >
4139 where
4240 OnHit : FnOnce ( & Self :: Stored , DepNodeIndex ) -> R ;
4341
44- fn complete (
45- & self ,
46- lock_sharded_storage : & mut Self :: Sharded ,
47- key : Self :: Key ,
48- value : Self :: Value ,
49- index : DepNodeIndex ,
50- ) -> Self :: Stored ;
42+ fn complete ( & self , key : Self :: Key , value : Self :: Value , index : DepNodeIndex ) -> Self :: Stored ;
5143
52- fn iter (
53- & self ,
54- shards : & Sharded < Self :: Sharded > ,
55- f : & mut dyn FnMut ( & Self :: Key , & Self :: Value , DepNodeIndex ) ,
56- ) ;
44+ fn iter ( & self , f : & mut dyn FnMut ( & Self :: Key , & Self :: Value , DepNodeIndex ) ) ;
5745}
5846
5947pub struct DefaultCacheSelector ;
@@ -62,11 +50,13 @@ impl<K: Eq + Hash, V: Clone> CacheSelector<K, V> for DefaultCacheSelector {
6250 type Cache = DefaultCache < K , V > ;
6351}
6452
65- pub struct DefaultCache < K , V > ( PhantomData < ( K , V ) > ) ;
53+ pub struct DefaultCache < K , V > {
54+ shards : Sharded < FxHashMap < K , ( V , DepNodeIndex ) > > ,
55+ }
6656
6757impl < K , V > Default for DefaultCache < K , V > {
6858 fn default ( ) -> Self {
69- DefaultCache ( PhantomData )
59+ DefaultCache { shards : Default :: default ( ) }
7060 }
7161}
7262
@@ -87,19 +77,16 @@ where
8777 V : Clone + Debug ,
8878{
8979 type Key = K ;
90- type Sharded = FxHashMap < K , ( V , DepNodeIndex ) > ;
9180
9281 #[ inline( always) ]
93- fn lookup < ' s , R , OnHit > (
94- & self ,
95- state : & ' s QueryCacheStore < Self > ,
96- key : & K ,
97- on_hit : OnHit ,
98- ) -> Result < R , QueryLookup >
82+ fn lookup < R , OnHit > ( & self , key : & K , on_hit : OnHit ) -> Result < R , QueryLookup >
9983 where
10084 OnHit : FnOnce ( & V , DepNodeIndex ) -> R ,
10185 {
102- let ( lookup, lock) = state. get_lookup ( key) ;
86+ let key_hash = sharded:: make_hash ( key) ;
87+ let shard = sharded:: get_shard_index_by_hash ( key_hash) ;
88+ let lock = self . shards . get_shard_by_index ( shard) . lock ( ) ;
89+ let lookup = QueryLookup { key_hash, shard } ;
10390 let result = lock. raw_entry ( ) . from_key_hashed_nocheck ( lookup. key_hash , key) ;
10491
10592 if let Some ( ( _, value) ) = result {
@@ -111,23 +98,13 @@ where
11198 }
11299
113100 #[ inline]
114- fn complete (
115- & self ,
116- lock_sharded_storage : & mut Self :: Sharded ,
117- key : K ,
118- value : V ,
119- index : DepNodeIndex ,
120- ) -> Self :: Stored {
121- lock_sharded_storage. insert ( key, ( value. clone ( ) , index) ) ;
101+ fn complete ( & self , key : K , value : V , index : DepNodeIndex ) -> Self :: Stored {
102+ self . shards . get_shard_by_value ( & key) . lock ( ) . insert ( key, ( value. clone ( ) , index) ) ;
122103 value
123104 }
124105
125- fn iter (
126- & self ,
127- shards : & Sharded < Self :: Sharded > ,
128- f : & mut dyn FnMut ( & Self :: Key , & Self :: Value , DepNodeIndex ) ,
129- ) {
130- let shards = shards. lock_shards ( ) ;
106+ fn iter ( & self , f : & mut dyn FnMut ( & Self :: Key , & Self :: Value , DepNodeIndex ) ) {
107+ let shards = self . shards . lock_shards ( ) ;
131108 for shard in shards. iter ( ) {
132109 for ( k, v) in shard. iter ( ) {
133110 f ( k, & v. 0 , v. 1 ) ;
@@ -144,12 +121,15 @@ impl<'tcx, K: Eq + Hash, V: 'tcx> CacheSelector<K, V> for ArenaCacheSelector<'tc
144121
145122pub struct ArenaCache < ' tcx , K , V > {
146123 arena : WorkerLocal < TypedArena < ( V , DepNodeIndex ) > > ,
147- phantom : PhantomData < ( K , & ' tcx V ) > ,
124+ shards : Sharded < FxHashMap < K , & ' tcx ( V , DepNodeIndex ) > > ,
148125}
149126
150127impl < ' tcx , K , V > Default for ArenaCache < ' tcx , K , V > {
151128 fn default ( ) -> Self {
152- ArenaCache { arena : WorkerLocal :: new ( |_| TypedArena :: default ( ) ) , phantom : PhantomData }
129+ ArenaCache {
130+ arena : WorkerLocal :: new ( |_| TypedArena :: default ( ) ) ,
131+ shards : Default :: default ( ) ,
132+ }
153133 }
154134}
155135
@@ -171,19 +151,16 @@ where
171151 V : Debug ,
172152{
173153 type Key = K ;
174- type Sharded = FxHashMap < K , & ' tcx ( V , DepNodeIndex ) > ;
175154
176155 #[ inline( always) ]
177- fn lookup < ' s , R , OnHit > (
178- & self ,
179- state : & ' s QueryCacheStore < Self > ,
180- key : & K ,
181- on_hit : OnHit ,
182- ) -> Result < R , QueryLookup >
156+ fn lookup < R , OnHit > ( & self , key : & K , on_hit : OnHit ) -> Result < R , QueryLookup >
183157 where
184158 OnHit : FnOnce ( & & ' tcx V , DepNodeIndex ) -> R ,
185159 {
186- let ( lookup, lock) = state. get_lookup ( key) ;
160+ let key_hash = sharded:: make_hash ( key) ;
161+ let shard = sharded:: get_shard_index_by_hash ( key_hash) ;
162+ let lock = self . shards . get_shard_by_index ( shard) . lock ( ) ;
163+ let lookup = QueryLookup { key_hash, shard } ;
187164 let result = lock. raw_entry ( ) . from_key_hashed_nocheck ( lookup. key_hash , key) ;
188165
189166 if let Some ( ( _, value) ) = result {
@@ -195,25 +172,15 @@ where
195172 }
196173
197174 #[ inline]
198- fn complete (
199- & self ,
200- lock_sharded_storage : & mut Self :: Sharded ,
201- key : K ,
202- value : V ,
203- index : DepNodeIndex ,
204- ) -> Self :: Stored {
175+ fn complete ( & self , key : K , value : V , index : DepNodeIndex ) -> Self :: Stored {
205176 let value = self . arena . alloc ( ( value, index) ) ;
206177 let value = unsafe { & * ( value as * const _ ) } ;
207- lock_sharded_storage . insert ( key, value) ;
178+ self . shards . get_shard_by_value ( & key ) . lock ( ) . insert ( key, value) ;
208179 & value. 0
209180 }
210181
211- fn iter (
212- & self ,
213- shards : & Sharded < Self :: Sharded > ,
214- f : & mut dyn FnMut ( & Self :: Key , & Self :: Value , DepNodeIndex ) ,
215- ) {
216- let shards = shards. lock_shards ( ) ;
182+ fn iter ( & self , f : & mut dyn FnMut ( & Self :: Key , & Self :: Value , DepNodeIndex ) ) {
183+ let shards = self . shards . lock_shards ( ) ;
217184 for shard in shards. iter ( ) {
218185 for ( k, v) in shard. iter ( ) {
219186 f ( k, & v. 0 , v. 1 ) ;
0 commit comments