@@ -2,7 +2,11 @@ use crate::dep_graph::DepNodeIndex;
2
2
3
3
use rustc_arena:: TypedArena ;
4
4
use rustc_data_structures:: fx:: FxHashMap ;
5
- use rustc_data_structures:: sharded:: { self , Sharded } ;
5
+ use rustc_data_structures:: sharded;
6
+ #[ cfg( parallel_compiler) ]
7
+ use rustc_data_structures:: sharded:: Sharded ;
8
+ #[ cfg( not( parallel_compiler) ) ]
9
+ use rustc_data_structures:: sync:: Lock ;
6
10
use rustc_data_structures:: sync:: WorkerLocal ;
7
11
use std:: default:: Default ;
8
12
use std:: fmt:: Debug ;
@@ -50,12 +54,15 @@ impl<K: Eq + Hash, V: Clone> CacheSelector<K, V> for DefaultCacheSelector {
50
54
}
51
55
52
56
pub struct DefaultCache < K , V > {
53
- shards : Sharded < FxHashMap < K , ( V , DepNodeIndex ) > > ,
57
+ #[ cfg( parallel_compiler) ]
58
+ cache : Sharded < FxHashMap < K , ( V , DepNodeIndex ) > > ,
59
+ #[ cfg( not( parallel_compiler) ) ]
60
+ cache : Lock < FxHashMap < K , ( V , DepNodeIndex ) > > ,
54
61
}
55
62
56
63
impl < K , V > Default for DefaultCache < K , V > {
57
64
fn default ( ) -> Self {
58
- DefaultCache { shards : Default :: default ( ) }
65
+ DefaultCache { cache : Default :: default ( ) }
59
66
}
60
67
}
61
68
83
90
OnHit : FnOnce ( & V , DepNodeIndex ) -> R ,
84
91
{
85
92
let key_hash = sharded:: make_hash ( key) ;
86
- let shard = sharded:: get_shard_index_by_hash ( key_hash) ;
87
- let lock = self . shards . get_shard_by_index ( shard) . lock ( ) ;
93
+ #[ cfg( parallel_compiler) ]
94
+ let lock = self . cache . get_shard_by_hash ( key_hash) . lock ( ) ;
95
+ #[ cfg( not( parallel_compiler) ) ]
96
+ let lock = self . cache . lock ( ) ;
88
97
let result = lock. raw_entry ( ) . from_key_hashed_nocheck ( key_hash, key) ;
89
98
90
99
if let Some ( ( _, value) ) = result {
@@ -97,14 +106,28 @@ where
97
106
98
107
#[ inline]
99
108
fn complete ( & self , key : K , value : V , index : DepNodeIndex ) -> Self :: Stored {
100
- self . shards . get_shard_by_value ( & key) . lock ( ) . insert ( key, ( value. clone ( ) , index) ) ;
109
+ #[ cfg( parallel_compiler) ]
110
+ let mut lock = self . cache . get_shard_by_value ( & key) . lock ( ) ;
111
+ #[ cfg( not( parallel_compiler) ) ]
112
+ let mut lock = self . cache . lock ( ) ;
113
+ lock. insert ( key, ( value. clone ( ) , index) ) ;
101
114
value
102
115
}
103
116
104
117
fn iter ( & self , f : & mut dyn FnMut ( & Self :: Key , & Self :: Value , DepNodeIndex ) ) {
105
- let shards = self . shards . lock_shards ( ) ;
106
- for shard in shards. iter ( ) {
107
- for ( k, v) in shard. iter ( ) {
118
+ #[ cfg( parallel_compiler) ]
119
+ {
120
+ let shards = self . cache . lock_shards ( ) ;
121
+ for shard in shards. iter ( ) {
122
+ for ( k, v) in shard. iter ( ) {
123
+ f ( k, & v. 0 , v. 1 ) ;
124
+ }
125
+ }
126
+ }
127
+ #[ cfg( not( parallel_compiler) ) ]
128
+ {
129
+ let map = self . cache . lock ( ) ;
130
+ for ( k, v) in map. iter ( ) {
108
131
f ( k, & v. 0 , v. 1 ) ;
109
132
}
110
133
}
@@ -119,15 +142,15 @@ impl<'tcx, K: Eq + Hash, V: 'tcx> CacheSelector<K, V> for ArenaCacheSelector<'tc
119
142
120
143
pub struct ArenaCache < ' tcx , K , V > {
121
144
arena : WorkerLocal < TypedArena < ( V , DepNodeIndex ) > > ,
122
- shards : Sharded < FxHashMap < K , & ' tcx ( V , DepNodeIndex ) > > ,
145
+ #[ cfg( parallel_compiler) ]
146
+ cache : Sharded < FxHashMap < K , & ' tcx ( V , DepNodeIndex ) > > ,
147
+ #[ cfg( not( parallel_compiler) ) ]
148
+ cache : Lock < FxHashMap < K , & ' tcx ( V , DepNodeIndex ) > > ,
123
149
}
124
150
125
151
impl < ' tcx , K , V > Default for ArenaCache < ' tcx , K , V > {
126
152
fn default ( ) -> Self {
127
- ArenaCache {
128
- arena : WorkerLocal :: new ( |_| TypedArena :: default ( ) ) ,
129
- shards : Default :: default ( ) ,
130
- }
153
+ ArenaCache { arena : WorkerLocal :: new ( |_| TypedArena :: default ( ) ) , cache : Default :: default ( ) }
131
154
}
132
155
}
133
156
@@ -156,8 +179,10 @@ where
156
179
OnHit : FnOnce ( & & ' tcx V , DepNodeIndex ) -> R ,
157
180
{
158
181
let key_hash = sharded:: make_hash ( key) ;
159
- let shard = sharded:: get_shard_index_by_hash ( key_hash) ;
160
- let lock = self . shards . get_shard_by_index ( shard) . lock ( ) ;
182
+ #[ cfg( parallel_compiler) ]
183
+ let lock = self . cache . get_shard_by_hash ( key_hash) . lock ( ) ;
184
+ #[ cfg( not( parallel_compiler) ) ]
185
+ let lock = self . cache . lock ( ) ;
161
186
let result = lock. raw_entry ( ) . from_key_hashed_nocheck ( key_hash, key) ;
162
187
163
188
if let Some ( ( _, value) ) = result {
@@ -172,14 +197,28 @@ where
172
197
fn complete ( & self , key : K , value : V , index : DepNodeIndex ) -> Self :: Stored {
173
198
let value = self . arena . alloc ( ( value, index) ) ;
174
199
let value = unsafe { & * ( value as * const _ ) } ;
175
- self . shards . get_shard_by_value ( & key) . lock ( ) . insert ( key, value) ;
200
+ #[ cfg( parallel_compiler) ]
201
+ let mut lock = self . cache . get_shard_by_value ( & key) . lock ( ) ;
202
+ #[ cfg( not( parallel_compiler) ) ]
203
+ let mut lock = self . cache . lock ( ) ;
204
+ lock. insert ( key, value) ;
176
205
& value. 0
177
206
}
178
207
179
208
fn iter ( & self , f : & mut dyn FnMut ( & Self :: Key , & Self :: Value , DepNodeIndex ) ) {
180
- let shards = self . shards . lock_shards ( ) ;
181
- for shard in shards. iter ( ) {
182
- for ( k, v) in shard. iter ( ) {
209
+ #[ cfg( parallel_compiler) ]
210
+ {
211
+ let shards = self . cache . lock_shards ( ) ;
212
+ for shard in shards. iter ( ) {
213
+ for ( k, v) in shard. iter ( ) {
214
+ f ( k, & v. 0 , v. 1 ) ;
215
+ }
216
+ }
217
+ }
218
+ #[ cfg( not( parallel_compiler) ) ]
219
+ {
220
+ let map = self . cache . lock ( ) ;
221
+ for ( k, v) in map. iter ( ) {
183
222
f ( k, & v. 0 , v. 1 ) ;
184
223
}
185
224
}
0 commit comments