@@ -4,6 +4,9 @@ use rustc_data_structures::fx::FxHashMap;
4
4
use rustc_data_structures:: sharded:: { self , Sharded } ;
5
5
use rustc_data_structures:: sync:: OnceLock ;
6
6
use rustc_index:: { Idx , IndexVec } ;
7
+ use rustc_span:: def_id:: CrateNum ;
8
+ use rustc_span:: def_id:: DefId ;
9
+ use rustc_span:: def_id:: DefIndex ;
7
10
use std:: fmt:: Debug ;
8
11
use std:: hash:: Hash ;
9
12
use std:: marker:: PhantomData ;
@@ -148,6 +151,8 @@ where
148
151
149
152
#[ inline( always) ]
150
153
fn lookup ( & self , key : & K ) -> Option < ( V , DepNodeIndex ) > {
154
+ // BUG: the "hash" passed here is a terrible hash and sharding is basically useless, I
155
+ // think?
151
156
let lock = self . cache . lock_shard_by_hash ( key. index ( ) as u64 ) ;
152
157
if let Some ( Some ( value) ) = lock. get ( * key) { Some ( * value) } else { None }
153
158
}
@@ -168,3 +173,56 @@ where
168
173
}
169
174
}
170
175
}
176
+
177
+ pub struct DefIdCacheSelector ;
178
+
179
+ impl < ' tcx , V : ' tcx > CacheSelector < ' tcx , V > for DefIdCacheSelector {
180
+ type Cache = DefIdCache < V >
181
+ where
182
+ V : Copy ;
183
+ }
184
+
185
+ pub struct DefIdCache < V > {
186
+ cache : rustc_data_structures:: sync:: Lock <
187
+ IndexVec < CrateNum , IndexVec < DefIndex , Option < ( V , DepNodeIndex ) > > > ,
188
+ > ,
189
+ }
190
+
191
+ impl < V > Default for DefIdCache < V > {
192
+ fn default ( ) -> Self {
193
+ DefIdCache { cache : Default :: default ( ) }
194
+ }
195
+ }
196
+
197
+ impl < V > QueryCache for DefIdCache < V >
198
+ where
199
+ V : Copy ,
200
+ {
201
+ type Key = DefId ;
202
+ type Value = V ;
203
+
204
+ #[ inline( always) ]
205
+ fn lookup ( & self , key : & DefId ) -> Option < ( V , DepNodeIndex ) > {
206
+ let cache = self . cache . lock ( ) ;
207
+ let krate_cache = cache. get ( key. krate ) ?;
208
+ krate_cache. get ( key. index ) . and_then ( |v| * v)
209
+ }
210
+
211
+ #[ inline]
212
+ fn complete ( & self , key : DefId , value : V , index : DepNodeIndex ) {
213
+ let mut cache = self . cache . lock ( ) ;
214
+ let krate_cache = cache. ensure_contains_elem ( key. krate , Default :: default) ;
215
+ * krate_cache. ensure_contains_elem ( key. index , Default :: default) = Some ( ( value, index) ) ;
216
+ }
217
+
218
+ fn iter ( & self , f : & mut dyn FnMut ( & Self :: Key , & Self :: Value , DepNodeIndex ) ) {
219
+ let guard = self . cache . lock ( ) ;
220
+ for ( krate, v) in guard. iter_enumerated ( ) {
221
+ for ( idx, v) in v. iter_enumerated ( ) {
222
+ if let Some ( v) = v {
223
+ f ( & DefId { krate, index : idx } , & v. 0 , v. 1 ) ;
224
+ }
225
+ }
226
+ }
227
+ }
228
+ }
0 commit comments