1
1
use crate :: dep_graph:: DepNodeIndex ;
2
2
3
3
use rustc_data_structures:: fx:: FxHashMap ;
4
- use rustc_data_structures:: sharded;
5
- #[ cfg( parallel_compiler) ]
6
- use rustc_data_structures:: sharded:: Sharded ;
4
+ use rustc_data_structures:: sharded:: { self , Sharded } ;
7
5
use rustc_data_structures:: sync:: Lock ;
8
6
use rustc_index:: { Idx , IndexVec } ;
9
7
use std:: fmt:: Debug ;
@@ -37,10 +35,7 @@ impl<'tcx, K: Eq + Hash, V: 'tcx> CacheSelector<'tcx, V> for DefaultCacheSelecto
37
35
}
38
36
39
37
pub struct DefaultCache < K , V > {
40
- #[ cfg( parallel_compiler) ]
41
38
cache : Sharded < FxHashMap < K , ( V , DepNodeIndex ) > > ,
42
- #[ cfg( not( parallel_compiler) ) ]
43
- cache : Lock < FxHashMap < K , ( V , DepNodeIndex ) > > ,
44
39
}
45
40
46
41
impl < K , V > Default for DefaultCache < K , V > {
@@ -60,40 +55,24 @@ where
60
55
#[ inline( always) ]
61
56
fn lookup ( & self , key : & K ) -> Option < ( V , DepNodeIndex ) > {
62
57
let key_hash = sharded:: make_hash ( key) ;
63
- #[ cfg( parallel_compiler) ]
64
58
let lock = self . cache . get_shard_by_hash ( key_hash) . lock ( ) ;
65
- #[ cfg( not( parallel_compiler) ) ]
66
- let lock = self . cache . lock ( ) ;
67
59
let result = lock. raw_entry ( ) . from_key_hashed_nocheck ( key_hash, key) ;
68
60
69
61
if let Some ( ( _, value) ) = result { Some ( * value) } else { None }
70
62
}
71
63
72
64
#[ inline]
73
65
fn complete ( & self , key : K , value : V , index : DepNodeIndex ) {
74
- #[ cfg( parallel_compiler) ]
75
66
let mut lock = self . cache . get_shard_by_value ( & key) . lock ( ) ;
76
- #[ cfg( not( parallel_compiler) ) ]
77
- let mut lock = self . cache . lock ( ) ;
78
67
// We may be overwriting another value. This is all right, since the dep-graph
79
68
// will check that the fingerprint matches.
80
69
lock. insert ( key, ( value, index) ) ;
81
70
}
82
71
83
72
fn iter ( & self , f : & mut dyn FnMut ( & Self :: Key , & Self :: Value , DepNodeIndex ) ) {
84
- #[ cfg( parallel_compiler) ]
85
- {
86
- let shards = self . cache . lock_shards ( ) ;
87
- for shard in shards. iter ( ) {
88
- for ( k, v) in shard. iter ( ) {
89
- f ( k, & v. 0 , v. 1 ) ;
90
- }
91
- }
92
- }
93
- #[ cfg( not( parallel_compiler) ) ]
94
- {
95
- let map = self . cache . lock ( ) ;
96
- for ( k, v) in map. iter ( ) {
73
+ let shards = self . cache . lock_shards ( ) ;
74
+ for shard in shards. iter ( ) {
75
+ for ( k, v) in shard. iter ( ) {
97
76
f ( k, & v. 0 , v. 1 ) ;
98
77
}
99
78
}
@@ -151,10 +130,7 @@ impl<'tcx, K: Idx, V: 'tcx> CacheSelector<'tcx, V> for VecCacheSelector<K> {
151
130
}
152
131
153
132
pub struct VecCache < K : Idx , V > {
154
- #[ cfg( parallel_compiler) ]
155
133
cache : Sharded < IndexVec < K , Option < ( V , DepNodeIndex ) > > > ,
156
- #[ cfg( not( parallel_compiler) ) ]
157
- cache : Lock < IndexVec < K , Option < ( V , DepNodeIndex ) > > > ,
158
134
}
159
135
160
136
impl < K : Idx , V > Default for VecCache < K , V > {
@@ -173,38 +149,20 @@ where
173
149
174
150
#[ inline( always) ]
175
151
fn lookup ( & self , key : & K ) -> Option < ( V , DepNodeIndex ) > {
176
- #[ cfg( parallel_compiler) ]
177
152
let lock = self . cache . get_shard_by_hash ( key. index ( ) as u64 ) . lock ( ) ;
178
- #[ cfg( not( parallel_compiler) ) ]
179
- let lock = self . cache . lock ( ) ;
180
153
if let Some ( Some ( value) ) = lock. get ( * key) { Some ( * value) } else { None }
181
154
}
182
155
183
156
#[ inline]
184
157
fn complete ( & self , key : K , value : V , index : DepNodeIndex ) {
185
- #[ cfg( parallel_compiler) ]
186
158
let mut lock = self . cache . get_shard_by_hash ( key. index ( ) as u64 ) . lock ( ) ;
187
- #[ cfg( not( parallel_compiler) ) ]
188
- let mut lock = self . cache . lock ( ) ;
189
159
lock. insert ( key, ( value, index) ) ;
190
160
}
191
161
192
162
fn iter ( & self , f : & mut dyn FnMut ( & Self :: Key , & Self :: Value , DepNodeIndex ) ) {
193
- #[ cfg( parallel_compiler) ]
194
- {
195
- let shards = self . cache . lock_shards ( ) ;
196
- for shard in shards. iter ( ) {
197
- for ( k, v) in shard. iter_enumerated ( ) {
198
- if let Some ( v) = v {
199
- f ( & k, & v. 0 , v. 1 ) ;
200
- }
201
- }
202
- }
203
- }
204
- #[ cfg( not( parallel_compiler) ) ]
205
- {
206
- let map = self . cache . lock ( ) ;
207
- for ( k, v) in map. iter_enumerated ( ) {
163
+ let shards = self . cache . lock_shards ( ) ;
164
+ for shard in shards. iter ( ) {
165
+ for ( k, v) in shard. iter_enumerated ( ) {
208
166
if let Some ( v) = v {
209
167
f ( & k, & v. 0 , v. 1 ) ;
210
168
}
0 commit comments