11use crate :: dep_graph:: DepNodeIndex ;
22
33use 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 } ;
75use rustc_data_structures:: sync:: Lock ;
86use rustc_index:: { Idx , IndexVec } ;
97use std:: fmt:: Debug ;
@@ -37,10 +35,7 @@ impl<'tcx, K: Eq + Hash, V: 'tcx> CacheSelector<'tcx, V> for DefaultCacheSelecto
3735}
3836
3937pub struct DefaultCache < K , V > {
40- #[ cfg( parallel_compiler) ]
4138 cache : Sharded < FxHashMap < K , ( V , DepNodeIndex ) > > ,
42- #[ cfg( not( parallel_compiler) ) ]
43- cache : Lock < FxHashMap < K , ( V , DepNodeIndex ) > > ,
4439}
4540
4641impl < K , V > Default for DefaultCache < K , V > {
@@ -60,40 +55,24 @@ where
6055 #[ inline( always) ]
6156 fn lookup ( & self , key : & K ) -> Option < ( V , DepNodeIndex ) > {
6257 let key_hash = sharded:: make_hash ( key) ;
63- #[ cfg( parallel_compiler) ]
6458 let lock = self . cache . get_shard_by_hash ( key_hash) . lock ( ) ;
65- #[ cfg( not( parallel_compiler) ) ]
66- let lock = self . cache . lock ( ) ;
6759 let result = lock. raw_entry ( ) . from_key_hashed_nocheck ( key_hash, key) ;
6860
6961 if let Some ( ( _, value) ) = result { Some ( * value) } else { None }
7062 }
7163
7264 #[ inline]
7365 fn complete ( & self , key : K , value : V , index : DepNodeIndex ) {
74- #[ cfg( parallel_compiler) ]
7566 let mut lock = self . cache . get_shard_by_value ( & key) . lock ( ) ;
76- #[ cfg( not( parallel_compiler) ) ]
77- let mut lock = self . cache . lock ( ) ;
7867 // We may be overwriting another value. This is all right, since the dep-graph
7968 // will check that the fingerprint matches.
8069 lock. insert ( key, ( value, index) ) ;
8170 }
8271
8372 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 ( ) {
9776 f ( k, & v. 0 , v. 1 ) ;
9877 }
9978 }
@@ -151,10 +130,7 @@ impl<'tcx, K: Idx, V: 'tcx> CacheSelector<'tcx, V> for VecCacheSelector<K> {
151130}
152131
153132pub struct VecCache < K : Idx , V > {
154- #[ cfg( parallel_compiler) ]
155133 cache : Sharded < IndexVec < K , Option < ( V , DepNodeIndex ) > > > ,
156- #[ cfg( not( parallel_compiler) ) ]
157- cache : Lock < IndexVec < K , Option < ( V , DepNodeIndex ) > > > ,
158134}
159135
160136impl < K : Idx , V > Default for VecCache < K , V > {
@@ -173,38 +149,20 @@ where
173149
174150 #[ inline( always) ]
175151 fn lookup ( & self , key : & K ) -> Option < ( V , DepNodeIndex ) > {
176- #[ cfg( parallel_compiler) ]
177152 let lock = self . cache . get_shard_by_hash ( key. index ( ) as u64 ) . lock ( ) ;
178- #[ cfg( not( parallel_compiler) ) ]
179- let lock = self . cache . lock ( ) ;
180153 if let Some ( Some ( value) ) = lock. get ( * key) { Some ( * value) } else { None }
181154 }
182155
183156 #[ inline]
184157 fn complete ( & self , key : K , value : V , index : DepNodeIndex ) {
185- #[ cfg( parallel_compiler) ]
186158 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 ( ) ;
189159 lock. insert ( key, ( value, index) ) ;
190160 }
191161
192162 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 ( ) {
208166 if let Some ( v) = v {
209167 f ( & k, & v. 0 , v. 1 ) ;
210168 }
0 commit comments