Skip to content

Commit 39e8d48

Browse files
committed
Swap out num_cpus for std::thread::available_parallelism (bevyengine#4970)
# Objective As of Rust 1.59, `std::thread::available_parallelism` has been stabilized. As of Rust 1.61, the API matches `num_cpus::get` by properly handling Linux's cgroups and other sandboxing mechanisms. As bevy does not have an established MSRV, we can replace `num_cpus` in `bevy_tasks` and reduce our dependency tree by one dep. ## Solution Replace `num_cpus` with `std::thread::available_parallelism`. Wrap it to have a fallback in the case it errors out and have it operate in the same manner as `num_cpus` did. This however removes `physical_core_count` from the API, though we are currently not using it in any way in first-party crates. --- ## Changelog Changed: `bevy_tasks::logical_core_count` -> `bevy_tasks::available_parallelism`. Removed: `bevy_tasks::physical_core_count`. ## Migration Guide `bevy_tasks::logical_core_count` and `bevy_tasks::physical_core_count` have been removed. `logical_core_count` has been replaced with `bevy_tasks::available_parallelism`, which works identically. If `bevy_tasks::physical_core_count` is required, the `num_cpus` crate can be used directly, as these two were just aliases for `num_cpus` APIs.
1 parent bf5d2d1 commit 39e8d48

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

crates/bevy_core/src/task_pool_options.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ impl DefaultTaskPoolOptions {
9494

9595
/// Inserts the default thread pools into the given resource map based on the configured values
9696
pub fn create_default_pools(&self) {
97-
let total_threads =
98-
bevy_tasks::logical_core_count().clamp(self.min_total_threads, self.max_total_threads);
97+
let total_threads = bevy_tasks::available_parallelism()
98+
.clamp(self.min_total_threads, self.max_total_threads);
9999
trace!("Assigning {} cores to default task pools", total_threads);
100100

101101
let mut remaining_threads = total_threads;

crates/bevy_tasks/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ keywords = ["bevy"]
1212
futures-lite = "1.4.0"
1313
async-executor = "1.3.0"
1414
async-channel = "1.4.2"
15-
num_cpus = "1"
1615
once_cell = "1.7"
1716

1817
[target.'cfg(target_arch = "wasm32")'.dependencies]

crates/bevy_tasks/src/lib.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,16 @@ pub mod prelude {
3333
};
3434
}
3535

36-
pub use num_cpus::get as logical_core_count;
37-
pub use num_cpus::get_physical as physical_core_count;
36+
use std::num::NonZeroUsize;
37+
38+
/// Gets the logical CPU core count available to the current process.
39+
///
40+
/// This is identical to [`std::thread::available_parallelism`], except
41+
/// it will return a default value of 1 if it internally errors out.
42+
///
43+
/// This will always return at least 1.
44+
pub fn available_parallelism() -> usize {
45+
std::thread::available_parallelism()
46+
.map(NonZeroUsize::get)
47+
.unwrap_or(1)
48+
}

crates/bevy_tasks/src/task_pool.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl TaskPool {
9595

9696
let executor = Arc::new(async_executor::Executor::new());
9797

98-
let num_threads = num_threads.unwrap_or_else(num_cpus::get);
98+
let num_threads = num_threads.unwrap_or_else(crate::available_parallelism);
9999

100100
let threads = (0..num_threads)
101101
.map(|i| {

0 commit comments

Comments
 (0)