Skip to content

[Merged by Bors] - Swap out num_cpus for std::thread::available_parallelism #4970

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/bevy_core/src/task_pool_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ impl DefaultTaskPoolOptions {

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

let mut remaining_threads = total_threads;
Expand Down
1 change: 0 additions & 1 deletion crates/bevy_tasks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ futures-lite = "1.4.0"
event-listener = "2.5.2"
async-executor = "1.3.0"
async-channel = "1.4.2"
num_cpus = "1"
once_cell = "1.7"

[target.'cfg(target_arch = "wasm32")'.dependencies]
Expand Down
15 changes: 13 additions & 2 deletions crates/bevy_tasks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,16 @@ pub mod prelude {
};
}

pub use num_cpus::get as logical_core_count;
pub use num_cpus::get_physical as physical_core_count;
use std::num::NonZeroUsize;

/// Gets the logical CPU core count available to the current process.
///
/// This is identical to [`std::thread::available_parallelism`], except
/// it will return a default value of 1 if it internally errors out.
///
/// This will always return at least 1.
pub fn available_parallelism() -> usize {
std::thread::available_parallelism()
.map(NonZeroUsize::get)
.unwrap_or(1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should log a warning when it fails.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This review comment isn't resolved yet. I'm fine with either adding the warning if it fails or a reasonable argument why it shouldn't be added.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was mostly to keep the behavior the same as it was before (as with num_cpus). Also given that this can be called repeatedly, it could easily spam the logs made.

}
2 changes: 1 addition & 1 deletion crates/bevy_tasks/src/task_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl TaskPool {

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

let num_threads = num_threads.unwrap_or_else(num_cpus::get);
let num_threads = num_threads.unwrap_or_else(crate::available_parallelism);

let threads = (0..num_threads)
.map(|i| {
Expand Down