Skip to content

Commit

Permalink
Remove num_cpus crate from the dependency
Browse files Browse the repository at this point in the history
- Replace `num_cpus::get()` with `std::thread::available_concurrency()` introduced
  in Rust 1.59.0.
- NOTE: `available_concurrency()` requires Rust 1.64.0 or newer to work reliably in
  some Linux container environment. #257
  • Loading branch information
tatsuya6502 committed Jun 6, 2023
1 parent e5690f8 commit 2bafd08
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 18 deletions.
6 changes: 1 addition & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -51,7 +51,6 @@ _core = [
"crossbeam-channel",
"crossbeam-epoch",
"crossbeam-utils",
"num_cpus",
"once_cell",
"parking_lot",
"scheduled-thread-pool",
Expand All @@ -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 }
Expand Down
5 changes: 1 addition & 4 deletions src/cht/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,10 +576,7 @@ struct Segment<K, V> {

#[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)]
Expand Down
5 changes: 5 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,8 @@ impl PartialEq<usize> 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))
}
10 changes: 1 addition & 9 deletions src/common/concurrent/thread_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
});
Expand Down

0 comments on commit 2bafd08

Please sign in to comment.