@@ -10,9 +10,10 @@ use std::mem;
1010// 32 shards is sufficient to reduce contention on an 8-core Ryzen 7 1700,
1111// but this should be tested on higher core count CPUs. How the `Sharded` type gets used
1212// 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 ;
1414
15- pub const SHARDS : usize = 1 << SHARD_BITS ;
15+ #[ cfg( parallel_compiler) ]
16+ const SHARDS : usize = 1 << SHARD_BITS ;
1617
1718/// An array of cache-line aligned inner locked structures with convenience methods.
1819/// A single field is used when the compiler uses only one thread.
@@ -44,8 +45,12 @@ impl<T> Sharded<T> {
4445
4546 /// The shard is selected by hashing `val` with `FxHasher`.
4647 #[ 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+ }
4954 }
5055
5156 #[ inline]
@@ -83,6 +88,16 @@ impl<T> Sharded<T> {
8388 }
8489}
8590
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+
86101pub type ShardedHashMap < K , V > = Sharded < FxHashMap < K , V > > ;
87102
88103impl < K : Eq , V > ShardedHashMap < K , V > {
0 commit comments