Skip to content

Commit 5435068

Browse files
Sansh2356hulxv
andauthored
feat: fetching available cores instead of default capacity of ThreadPool (citadel-tech#73)
Co-authored-by: hulxv <hulxxv@gmail.com>
1 parent 3e8c540 commit 5435068

File tree

2 files changed

+10
-16
lines changed

2 files changed

+10
-16
lines changed

src/lib.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,12 @@ pub mod object_pool;
101101
pub mod poll;
102102
pub mod reactor;
103103
pub mod thread_pool;
104+
104105
pub use handler::EventHandler;
105106
pub use mio::event::Event;
106107
pub use object_pool::{ObjectPool, PooledObject};
107108

108-
use crate::{
109-
error::Result,
110-
reactor::{DEFAULT_EVENTS_CAPACITY, DEFAULT_POLL_TIMEOUT_MS},
111-
thread_pool::DEFAULT_POOL_CAPACITY,
112-
};
109+
use crate::error::Result;
113110

114111
/// A convenient prelude module that re-exports commonly used types and traits.
115112
///
@@ -186,20 +183,15 @@ impl Default for EventLoop {
186183
/// Creates a new `EventLoop` with default configuration.
187184
///
188185
/// The default configuration uses:
189-
/// - 4 worker threads ([`DEFAULT_POOL_CAPACITY`])
190-
/// - 1024 events capacity ([`DEFAULT_EVENTS_CAPACITY`])
191-
/// - 100ms poll timeout ([`DEFAULT_POLL_TIMEOUT_MS`])
186+
/// - Number of worker threads equal to available CPU cores, falling back to 4 threads if CPU detection fails ([`thread_pool::DEFAULT_POOL_CAPACITY`])
187+
/// - 1024 events capacity ([`reactor::DEFAULT_EVENTS_CAPACITY`])
188+
/// - 150ms poll timeout ([`reactor::DEFAULT_POLL_TIMEOUT_MS`])
192189
///
193190
/// # Panics
194191
///
195192
/// Panics if the reactor cannot be initialized with default settings.
196193
fn default() -> Self {
197-
let reactor = reactor::Reactor::new(
198-
DEFAULT_POOL_CAPACITY,
199-
DEFAULT_EVENTS_CAPACITY,
200-
DEFAULT_POLL_TIMEOUT_MS,
201-
)
202-
.unwrap();
194+
let reactor = reactor::Reactor::default();
203195
Self { reactor }
204196
}
205197
}

src/thread_pool.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ type ChannelReceiver = channel::Receiver<WorkerMessage>;
2727

2828
impl Default for ThreadPool {
2929
fn default() -> Self {
30-
Self::new(DEFAULT_POOL_CAPACITY)
30+
let default_capacity = std::thread::available_parallelism()
31+
.map(|n| n.get())
32+
.unwrap_or(DEFAULT_POOL_CAPACITY);
33+
Self::new(default_capacity)
3134
}
3235
}
3336

@@ -135,7 +138,6 @@ mod tests {
135138
std::thread::sleep(Duration::from_millis(100));
136139
assert_eq!(counter.load(Ordering::SeqCst), 1);
137140
}
138-
139141
#[test]
140142
fn test_multiple_tasks() {
141143
let pool = ThreadPool::new(4);

0 commit comments

Comments
 (0)