refactor: streamline worker count validation

- Introduced a `clamp_worker_count` function to centralize the logic for enforcing the minimum worker count.
- Updated setter methods in `TreeConfig` to utilize the new clamping function, improving code readability and maintainability.
This commit is contained in:
Yong Kang
2025-10-10 11:04:50 +00:00
parent 77e1b1cc46
commit d510b74c43

View File

@@ -12,6 +12,17 @@ pub const DEFAULT_MAX_PROOF_TASK_CONCURRENCY: u64 = 256;
/// Minimum number of workers we allow configuring explicitly.
pub const MIN_WORKER_COUNT: usize = 2;
/// Clamps the worker count to the minimum allowed value.
///
/// Ensures that the worker count is at least [`MIN_WORKER_COUNT`].
const fn clamp_worker_count(count: usize) -> usize {
if count >= MIN_WORKER_COUNT {
count
} else {
MIN_WORKER_COUNT
}
}
/// Returns the default number of storage worker threads based on available parallelism.
fn default_storage_worker_count() -> usize {
#[cfg(feature = "std")]
@@ -499,11 +510,7 @@ impl TreeConfig {
/// Setter for the number of storage proof worker threads.
pub const fn with_storage_worker_count(mut self, storage_worker_count: usize) -> Self {
self.storage_worker_count = if storage_worker_count >= MIN_WORKER_COUNT {
storage_worker_count
} else {
MIN_WORKER_COUNT
};
self.storage_worker_count = clamp_worker_count(storage_worker_count);
self
}
@@ -514,11 +521,7 @@ impl TreeConfig {
/// Setter for the number of account proof worker threads.
pub const fn with_account_worker_count(mut self, account_worker_count: usize) -> Self {
self.account_worker_count = if account_worker_count >= MIN_WORKER_COUNT {
account_worker_count
} else {
MIN_WORKER_COUNT
};
self.account_worker_count = clamp_worker_count(account_worker_count);
self
}
}