We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 4f40c71 commit ba2dbb7Copy full SHA for ba2dbb7
1 file changed
src/raw/utils/counter.rs
@@ -1,4 +1,7 @@
1
-use std::sync::atomic::{AtomicIsize, Ordering};
+use std::sync::{
2
+ atomic::{AtomicIsize, Ordering},
3
+ OnceLock,
4
+};
5
6
use super::CachePadded;
7
@@ -14,9 +17,13 @@ pub struct Counter(Box<[CachePadded<AtomicIsize>]>);
14
17
impl Default for Counter {
15
18
/// Create a new `Counter`.
16
19
fn default() -> Counter {
- let num_cpus = std::thread::available_parallelism()
- .map(usize::from)
- .unwrap_or(1);
20
+ // available_parallelism is quite slow (microseconds).
21
+ static CPUS: OnceLock<usize> = OnceLock::new();
22
+ let num_cpus = *CPUS.get_or_init(|| {
23
+ std::thread::available_parallelism()
24
+ .map(Into::into)
25
+ .unwrap_or(1)
26
+ });
27
28
// Round up to the next power-of-two for fast modulo.
29
let shards = (0..num_cpus.next_power_of_two())
0 commit comments