pub struct WorkerCount { /* private fields */ }Expand description
Worker count value object for adaptive parallel processing optimization
This value object provides adaptive parallel processing optimization with resource-aware worker allocation, performance-optimized concurrency management, and empirically-validated optimization strategies. It implements Domain-Driven Design (DDD) value object patterns with comprehensive parallel processing support.
§Key Features
- Adaptive Optimization: Dynamic worker allocation based on file characteristics and system resources
- Resource-Aware Processing: System resource consideration for optimal performance
- Performance-Optimized: Empirically-validated strategies for different workload types
- Cross-Platform: Consistent representation across languages and storage systems
- Validation: Comprehensive worker count validation and constraint enforcement
- Serialization: Full serialization support for storage and API integration
§Benefits Over Raw Numbers
- Type Safety:
WorkerCountcannot be confused with other numeric types - Domain Semantics: Clear intent in function signatures and parallel processing business logic
- Optimization Logic: Comprehensive optimization strategies and validation rules
- Future Evolution: Extensible for worker-specific methods and performance features
§Design Principles
- Adaptive: Adjusts based on file characteristics and system resources
- Resource-Aware: Considers system capabilities and prevents resource exhaustion
- Performance-Optimized: Balances throughput vs coordination overhead
- Bounded: Enforces minimum and maximum limits for reliable operation
§Optimization Strategies
Based on comprehensive benchmark results across file sizes from 5MB to 2GB:
- Small Files (≤50MB): Increased worker allocation by 2-3x (up to 102% performance gain)
- Medium Files (100-500MB): Balanced approach with slight adjustments
- Large Files (≥2GB): Reduced worker allocation by 70% (up to 76% performance gain)
§Use Cases
- Parallel Processing Optimization: Optimize worker allocation for parallel processing tasks
- Resource Management: Manage system resources with optimal worker allocation
- Performance Tuning: Fine-tune performance with empirically-validated strategies
- Workload Adaptation: Adapt worker allocation to different workload characteristics
§Usage Examples
§Cross-Language Mapping
- Rust:
WorkerCountnewtype wrapper with full optimization - Go:
WorkerCountstruct with equivalent interface - JSON: Numeric representation for API compatibility
- Database: INTEGER column with validation constraints
Implementations§
Source§impl WorkerCount
impl WorkerCount
Sourcepub const MIN_WORKERS: usize = 1usize
pub const MIN_WORKERS: usize = 1usize
Minimum number of workers (always at least 1)
Sourcepub const MAX_WORKERS: usize = 32usize
pub const MAX_WORKERS: usize = 32usize
Maximum number of workers (prevent resource exhaustion)
Sourcepub const DEFAULT_WORKERS: usize = 4usize
pub const DEFAULT_WORKERS: usize = 4usize
Default worker count for fallback scenarios
Sourcepub fn new(count: usize) -> Self
pub fn new(count: usize) -> Self
Creates a new WorkerCount with the specified number of workers
§Purpose
Creates a type-safe worker count with automatic bounds checking. Ensures worker count is always within valid operational range.
§Why
Bounded worker counts provide:
- Prevention of resource exhaustion (max limit)
- Guaranteed minimum parallelism (min limit)
- Type-safe concurrency configuration
- Consistent operational behavior
§Arguments
count- Number of workers (will be clamped to 1-32 range)
§Returns
WorkerCount with value clamped to [MIN_WORKERS, MAX_WORKERS]
§Examples
Sourcepub fn value(&self) -> usize
pub fn value(&self) -> usize
Returns the number of workers (alias for test framework compatibility)
Sourcepub fn optimal_for_file_size(file_size: u64) -> Self
pub fn optimal_for_file_size(file_size: u64) -> Self
Calculates the optimal worker count based on file size
§Purpose
Provides empirically-optimized worker allocation based on comprehensive benchmark results. Maximizes throughput while minimizing coordination overhead.
§Why
File size-based optimization provides:
- Up to 102% performance improvement for small files (5-10MB)
- Up to 76% performance improvement for large files (2GB+)
- Reduced coordination overhead for optimal throughput
- Evidence-based strategy from real benchmark data
§Empirical Optimization Results
Based on comprehensive benchmarks across 5MB to 2GB files:
- Small files (≤50MB): Increased worker allocation by 2-3x (up to 102% performance gain)
- Medium files (100-500MB): Balanced approach with slight adjustments
- Large files (≥2GB): Reduced worker allocation by 70% (up to 76% performance gain)
§Strategy (Benchmark-Optimized)
- Tiny files (< 1MB): 1-2 workers (minimize overhead)
- Small files (1-50MB): 6-14 workers (aggressive parallelism)
- Medium files (50-500MB): 5-12 workers (balanced approach)
- Large files (500MB-2GB): 8-12 workers (moderate parallelism)
- Huge files (> 2GB): 3-6 workers (conservative strategy)
§Arguments
file_size- Size of the file in bytes
§Returns
Optimal WorkerCount for the given file size (empirically validated)
§Examples
Sourcepub fn optimal_for_file_and_system(
file_size: u64,
available_cores: usize,
) -> Self
pub fn optimal_for_file_and_system( file_size: u64, available_cores: usize, ) -> Self
Calculates optimal worker count considering both file size and system resources
This method combines file size optimization with system resource awareness to prevent over-subscription of CPU cores.
§Arguments
file_size- Size of the file in bytesavailable_cores- Number of available CPU cores
§Returns
Optimal WorkerCount considering both file size and system resources
Sourcepub fn optimal_for_processing_type(
file_size: u64,
available_cores: usize,
is_cpu_intensive: bool,
) -> Self
pub fn optimal_for_processing_type( file_size: u64, available_cores: usize, is_cpu_intensive: bool, ) -> Self
Calculates optimal worker count with processing complexity consideration
Different processing types have different CPU intensity:
- Compression: CPU-intensive, benefits from more workers
- Encryption: CPU-intensive, benefits from more workers
- I/O operations: Less CPU-intensive, fewer workers needed
§Arguments
file_size- Size of the file in bytesavailable_cores- Number of available CPU coresis_cpu_intensive- Whether the processing is CPU-intensive
§Returns
Optimal WorkerCount considering processing complexity
Sourcepub fn default_for_system() -> Self
pub fn default_for_system() -> Self
Returns the default worker count based on system capabilities
This is used as a fallback when file size or other parameters are unknown.
§Returns
Default WorkerCount based on available CPU cores
Sourcepub fn is_suitable_for_file_size(&self, file_size: u64) -> bool
pub fn is_suitable_for_file_size(&self, file_size: u64) -> bool
Sourcepub fn strategy_description(file_size: u64) -> &'static str
pub fn strategy_description(file_size: u64) -> &'static str
Sourcepub fn validate_user_input(
user_count: usize,
available_cores: usize,
file_size: u64,
) -> Result<usize, String>
pub fn validate_user_input( user_count: usize, available_cores: usize, file_size: u64, ) -> Result<usize, String>
Validates user-provided worker count with sanity checks
§Arguments
user_count- User-specified worker countavailable_cores- Number of available CPU coresfile_size- Size of file being processed
§Returns
Ok(usize)- Validated worker count (may be adjusted)Err(String)- Error message explaining why input is invalid
Trait Implementations§
Source§impl Clone for WorkerCount
impl Clone for WorkerCount
Source§fn clone(&self) -> WorkerCount
fn clone(&self) -> WorkerCount
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for WorkerCount
impl Debug for WorkerCount
Source§impl Default for WorkerCount
impl Default for WorkerCount
Source§impl<'de> Deserialize<'de> for WorkerCount
impl<'de> Deserialize<'de> for WorkerCount
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl Display for WorkerCount
impl Display for WorkerCount
Source§impl From<WorkerCount> for usize
impl From<WorkerCount> for usize
Source§fn from(worker_count: WorkerCount) -> Self
fn from(worker_count: WorkerCount) -> Self
Source§impl From<usize> for WorkerCount
impl From<usize> for WorkerCount
Source§impl Hash for WorkerCount
impl Hash for WorkerCount
Source§impl PartialEq for WorkerCount
impl PartialEq for WorkerCount
Source§impl Serialize for WorkerCount
impl Serialize for WorkerCount
impl Copy for WorkerCount
impl Eq for WorkerCount
impl StructuralPartialEq for WorkerCount
Auto Trait Implementations§
impl Freeze for WorkerCount
impl RefUnwindSafe for WorkerCount
impl Send for WorkerCount
impl Sync for WorkerCount
impl Unpin for WorkerCount
impl UnwindSafe for WorkerCount
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more