Skip to content

Commit 33e2ea4

Browse files
committed
cache available_parallelism
1 parent 30aa091 commit 33e2ea4

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/collector.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::raw::{self, membarrier, Thread};
22
use crate::{LocalGuard, OwnedGuard};
33

44
use std::fmt;
5+
use std::sync::OnceLock;
56

67
/// A concurrent garbage collector.
78
///
@@ -35,9 +36,13 @@ impl Collector {
3536
// operating-system strong barrier APIs.
3637
membarrier::detect();
3738

38-
let cpus = std::thread::available_parallelism()
39-
.map(Into::into)
40-
.unwrap_or(1);
39+
// available_parallelism is quite slow (microseconds).
40+
static CPUS: OnceLock<usize> = OnceLock::new();
41+
let cpus = *CPUS.get_or_init(|| {
42+
std::thread::available_parallelism()
43+
.map(Into::into)
44+
.unwrap_or(1)
45+
});
4146

4247
// Ensure every batch accumulates at least as many entries
4348
// as there are threads on the system.

0 commit comments

Comments
 (0)