@@ -10,9 +10,10 @@ use std::mem;
10
10
// 32 shards is sufficient to reduce contention on an 8-core Ryzen 7 1700,
11
11
// but this should be tested on higher core count CPUs. How the `Sharded` type gets used
12
12
// may also affect the ideal number of shards.
13
- const SHARD_BITS : usize = if cfg ! ( parallel_compiler ) { 5 } else { 0 } ;
13
+ const SHARD_BITS : usize = 5 ;
14
14
15
- pub const SHARDS : usize = 1 << SHARD_BITS ;
15
+ #[ cfg( parallel_compiler) ]
16
+ const SHARDS : usize = 1 << SHARD_BITS ;
16
17
17
18
/// An array of cache-line aligned inner locked structures with convenience methods.
18
19
/// A single field is used when the compiler uses only one thread.
@@ -44,8 +45,12 @@ impl<T> Sharded<T> {
44
45
45
46
/// The shard is selected by hashing `val` with `FxHasher`.
46
47
#[ inline]
47
- pub fn get_shard_by_value < K : Hash + ?Sized > ( & self , val : & K ) -> & Lock < T > {
48
- self . get_shard_by_hash ( if SHARDS == 1 { 0 } else { make_hash ( val) } )
48
+ pub fn get_shard_by_value < K : Hash + ?Sized > ( & self , _val : & K ) -> & Lock < T > {
49
+ match self {
50
+ Self :: Single ( single) => & single,
51
+ #[ cfg( parallel_compiler) ]
52
+ Self :: Shards ( shards) => self . get_shard_by_hash ( make_hash ( _val) ) ,
53
+ }
49
54
}
50
55
51
56
#[ inline]
@@ -83,6 +88,16 @@ impl<T> Sharded<T> {
83
88
}
84
89
}
85
90
91
+ #[ inline]
92
+ pub fn shards ( ) -> usize {
93
+ #[ cfg( parallel_compiler) ]
94
+ if is_dyn_thread_safe ( ) {
95
+ return SHARDS ;
96
+ }
97
+
98
+ 1
99
+ }
100
+
86
101
pub type ShardedHashMap < K , V > = Sharded < FxHashMap < K , V > > ;
87
102
88
103
impl < K : Eq , V > ShardedHashMap < K , V > {
0 commit comments