From 9bc953ecd844bcd7edb655650dbd5332a9264af9 Mon Sep 17 00:00:00 2001 From: tison Date: Tue, 8 Apr 2025 08:16:00 +0800 Subject: [PATCH] chore: replace num_cpu with available_parallelism Signed-off-by: tison --- futures-executor/Cargo.toml | 3 +-- futures-executor/src/thread_pool.rs | 10 ++-------- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/futures-executor/Cargo.toml b/futures-executor/Cargo.toml index 0b9730bea7..72625d16d2 100644 --- a/futures-executor/Cargo.toml +++ b/futures-executor/Cargo.toml @@ -13,13 +13,12 @@ Executors for asynchronous tasks based on the futures-rs library. [features] default = ["std"] std = ["futures-core/std", "futures-task/std", "futures-util/std"] -thread-pool = ["std", "num_cpus"] +thread-pool = ["std"] [dependencies] futures-core = { path = "../futures-core", version = "=1.0.0-alpha.0", default-features = false } futures-task = { path = "../futures-task", version = "=0.4.0-alpha.0", default-features = false } futures-util = { path = "../futures-util", version = "=0.4.0-alpha.0", default-features = false } -num_cpus = { version = "1.8.0", optional = true } [dev-dependencies] futures = { path = "../futures", features = ["thread-pool"] } diff --git a/futures-executor/src/thread_pool.rs b/futures-executor/src/thread_pool.rs index c4442e4eeb..57e550fae4 100644 --- a/futures-executor/src/thread_pool.rs +++ b/futures-executor/src/thread_pool.rs @@ -6,7 +6,6 @@ use futures_task::{waker_ref, ArcWake}; use futures_task::{FutureObj, Spawn, SpawnError}; use futures_util::future::FutureExt; use std::boxed::Box; -use std::cmp; use std::fmt; use std::format; use std::io; @@ -190,13 +189,8 @@ impl ThreadPoolBuilder { /// /// See the other methods on this type for details on the defaults. pub fn new() -> Self { - Self { - pool_size: cmp::max(1, num_cpus::get()), - stack_size: 0, - name_prefix: None, - after_start: None, - before_stop: None, - } + let pool_size = thread::available_parallelism().map_or(1, |p| p.get()); + Self { pool_size, stack_size: 0, name_prefix: None, after_start: None, before_stop: None } } /// Set size of a future ThreadPool