1
1
use crate :: dep_graph:: DepNodeIndex ;
2
- use crate :: query:: plumbing:: { QueryCacheStore , QueryLookup } ;
2
+ use crate :: query:: plumbing:: QueryLookup ;
3
3
4
4
use rustc_arena:: TypedArena ;
5
5
use rustc_data_structures:: fx:: FxHashMap ;
6
- use rustc_data_structures:: sharded:: Sharded ;
6
+ use rustc_data_structures:: sharded:: { self , Sharded } ;
7
7
use rustc_data_structures:: sync:: WorkerLocal ;
8
8
use std:: default:: Default ;
9
9
use std:: fmt:: Debug ;
@@ -25,35 +25,23 @@ pub trait QueryStorage {
25
25
26
26
pub trait QueryCache : QueryStorage + Sized {
27
27
type Key : Hash + Eq + Clone + Debug ;
28
- type Sharded : Default ;
29
28
30
29
/// Checks if the query is already computed and in the cache.
31
30
/// It returns the shard index and a lock guard to the shard,
32
31
/// which will be used if the query is not in the cache and we need
33
32
/// to compute it.
34
- fn lookup < ' s , R , OnHit > (
33
+ fn lookup < R , OnHit > (
35
34
& self ,
36
- state : & ' s QueryCacheStore < Self > ,
37
35
key : & Self :: Key ,
38
36
// `on_hit` can be called while holding a lock to the query state shard.
39
37
on_hit : OnHit ,
40
38
) -> Result < R , QueryLookup >
41
39
where
42
40
OnHit : FnOnce ( & Self :: Stored , DepNodeIndex ) -> R ;
43
41
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 ;
51
43
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 ) ) ;
57
45
}
58
46
59
47
pub struct DefaultCacheSelector ;
@@ -62,11 +50,13 @@ impl<K: Eq + Hash, V: Clone> CacheSelector<K, V> for DefaultCacheSelector {
62
50
type Cache = DefaultCache < K , V > ;
63
51
}
64
52
65
- pub struct DefaultCache < K , V > ( PhantomData < ( K , V ) > ) ;
53
+ pub struct DefaultCache < K , V > {
54
+ shards : Sharded < FxHashMap < K , ( V , DepNodeIndex ) > > ,
55
+ }
66
56
67
57
impl < K , V > Default for DefaultCache < K , V > {
68
58
fn default ( ) -> Self {
69
- DefaultCache ( PhantomData )
59
+ DefaultCache { shards : Default :: default ( ) }
70
60
}
71
61
}
72
62
@@ -87,19 +77,16 @@ where
87
77
V : Clone + Debug ,
88
78
{
89
79
type Key = K ;
90
- type Sharded = FxHashMap < K , ( V , DepNodeIndex ) > ;
91
80
92
81
#[ 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 >
99
83
where
100
84
OnHit : FnOnce ( & V , DepNodeIndex ) -> R ,
101
85
{
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 } ;
103
90
let result = lock. raw_entry ( ) . from_key_hashed_nocheck ( lookup. key_hash , key) ;
104
91
105
92
if let Some ( ( _, value) ) = result {
@@ -111,23 +98,13 @@ where
111
98
}
112
99
113
100
#[ 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) ) ;
122
103
value
123
104
}
124
105
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 ( ) ;
131
108
for shard in shards. iter ( ) {
132
109
for ( k, v) in shard. iter ( ) {
133
110
f ( k, & v. 0 , v. 1 ) ;
@@ -144,12 +121,15 @@ impl<'tcx, K: Eq + Hash, V: 'tcx> CacheSelector<K, V> for ArenaCacheSelector<'tc
144
121
145
122
pub struct ArenaCache < ' tcx , K , V > {
146
123
arena : WorkerLocal < TypedArena < ( V , DepNodeIndex ) > > ,
147
- phantom : PhantomData < ( K , & ' tcx V ) > ,
124
+ shards : Sharded < FxHashMap < K , & ' tcx ( V , DepNodeIndex ) > > ,
148
125
}
149
126
150
127
impl < ' tcx , K , V > Default for ArenaCache < ' tcx , K , V > {
151
128
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
+ }
153
133
}
154
134
}
155
135
@@ -171,19 +151,16 @@ where
171
151
V : Debug ,
172
152
{
173
153
type Key = K ;
174
- type Sharded = FxHashMap < K , & ' tcx ( V , DepNodeIndex ) > ;
175
154
176
155
#[ 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 >
183
157
where
184
158
OnHit : FnOnce ( & & ' tcx V , DepNodeIndex ) -> R ,
185
159
{
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 } ;
187
164
let result = lock. raw_entry ( ) . from_key_hashed_nocheck ( lookup. key_hash , key) ;
188
165
189
166
if let Some ( ( _, value) ) = result {
@@ -195,25 +172,15 @@ where
195
172
}
196
173
197
174
#[ 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 {
205
176
let value = self . arena . alloc ( ( value, index) ) ;
206
177
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) ;
208
179
& value. 0
209
180
}
210
181
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 ( ) ;
217
184
for shard in shards. iter ( ) {
218
185
for ( k, v) in shard. iter ( ) {
219
186
f ( k, & v. 0 , v. 1 ) ;
0 commit comments