diff --git a/Cargo.toml b/Cargo.toml index 0b24d0c7..e65650af 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "moka" version = "0.11.2" edition = "2018" -# Rust 1.65 was released on November 3rd, 2022, is supporting 2021 edition. +# Rust 1.65 was released on Nov 3, 2022. rust-version = "1.65" description = "A fast and concurrent cache library inspired by Java Caffeine" license = "MIT OR Apache-2.0" @@ -51,7 +51,6 @@ _core = [ "crossbeam-channel", "crossbeam-epoch", "crossbeam-utils", - "num_cpus", "once_cell", "parking_lot", "scheduled-thread-pool", @@ -67,9 +66,6 @@ _core = [ # The "_core" dependencies used by "sync" and "future" features. crossbeam-channel = { version = "0.5.5", optional = true } crossbeam-utils = { version = "0.8", optional = true } -# TODO: Check if we can use `std::thread::available_parallelism` instead. (It was -# introduced in Rust 1.59) -num_cpus = { version = "1.13", optional = true } once_cell = { version = "1.7", optional = true } parking_lot = { version = "0.12", optional = true } scheduled-thread-pool = { version = "0.2.7", optional = true } diff --git a/src/cht/segment.rs b/src/cht/segment.rs index 708b81ff..5700c61c 100644 --- a/src/cht/segment.rs +++ b/src/cht/segment.rs @@ -576,10 +576,7 @@ struct Segment { #[cfg(test)] fn default_num_segments() -> usize { - // Some platforms may return 0. In that case, use 1. - // https://github.com/moka-rs/moka/pull/39#issuecomment-916888859 - // https://github.com/seanmonstar/num_cpus/issues/69 - num_cpus::get().max(1) * 2 + crate::common::available_parallelism() * 2 } #[cfg(test)] diff --git a/src/common.rs b/src/common.rs index d2a7e39f..df8c8f62 100644 --- a/src/common.rs +++ b/src/common.rs @@ -51,3 +51,8 @@ impl PartialEq for CacheRegion { pub(crate) fn sketch_capacity(max_capacity: u64) -> u32 { max_capacity.try_into().unwrap_or(u32::MAX).max(128) } + +pub(crate) fn available_parallelism() -> usize { + use std::{num::NonZeroUsize, thread::available_parallelism}; + dbg!(available_parallelism().map(NonZeroUsize::get).unwrap_or(1)) +} diff --git a/src/common/concurrent/thread_pool.rs b/src/common/concurrent/thread_pool.rs index 4d035ab4..e14a424d 100644 --- a/src/common/concurrent/thread_pool.rs +++ b/src/common/concurrent/thread_pool.rs @@ -69,15 +69,7 @@ impl ThreadPoolRegistry { // and insert a new pool. let mut pools = REGISTRY.pools.write(); pools.entry(name).or_insert_with(|| { - // TODO: When we upgrade the MSRV to 1.59 (2022-02-24) or newer, - // replace num_cpus crate with `thread::available_parallelism` in - // std. - // - // NOTE: On some platforms, `num_cpus::get` may return 0. In that - // case, use 1. - // https://github.com/moka-rs/moka/pull/39#issuecomment-916888859 - // https://github.com/seanmonstar/num_cpus/issues/69 - let num_threads = num_cpus::get().max(1); + let num_threads = crate::common::available_parallelism(); let pool = ThreadPool::new(name, num_threads); Arc::new(pool) });