Skip to content

Commit

Permalink
Replace num_cpus::get with std::thread::available_parallelism (#500)
Browse files Browse the repository at this point in the history
  • Loading branch information
rukai authored Aug 1, 2024
1 parent ca453d3 commit 3b55c14
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
3 changes: 1 addition & 2 deletions metrics-util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ quanta = { version = "0.12", default-features = false, optional = true }
sketches-ddsketch = { version = "0.2", default-features = false, optional = true }
radix_trie = { version = "0.2", default-features = false, optional = true }
ordered-float = { version = "4.2", default-features = false, optional = true }
num_cpus = { version = "1", default-features = false, optional = true }
ahash = { version = "0.8.8", default-features = false, optional = true }
hashbrown = { version = "0.14", default-features = false, optional = true, features = ["ahash"] }

Expand Down Expand Up @@ -90,4 +89,4 @@ layer-filter = ["aho-corasick"]
layer-router = ["radix_trie"]
summary = ["sketches-ddsketch"]
recency = ["registry", "quanta"]
registry = ["crossbeam-epoch", "crossbeam-utils", "handles", "hashbrown", "num_cpus"]
registry = ["crossbeam-epoch", "crossbeam-utils", "handles", "hashbrown"]
8 changes: 6 additions & 2 deletions metrics-util/src/registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,14 @@ where
storage: S,
}

fn shard_count() -> usize {
std::thread::available_parallelism().map(|x| x.get()).unwrap_or(1).next_power_of_two()
}

impl Registry<Key, AtomicStorage> {
/// Creates a new `Registry` using a regular [`Key`] and atomic storage.
pub fn atomic() -> Self {
let shard_count = std::cmp::max(1, num_cpus::get()).next_power_of_two();
let shard_count = shard_count();
let shard_mask = shard_count - 1;
let counters =
repeat(()).take(shard_count).map(|_| RwLock::new(RegistryHashMap::default())).collect();
Expand All @@ -78,7 +82,7 @@ where
{
/// Creates a new `Registry`.
pub fn new(storage: S) -> Self {
let shard_count = std::cmp::max(1, num_cpus::get()).next_power_of_two();
let shard_count = shard_count();
let shard_mask = shard_count - 1;
let counters =
repeat(()).take(shard_count).map(|_| RwLock::new(RegistryHashMap::default())).collect();
Expand Down

0 comments on commit 3b55c14

Please sign in to comment.