WorkerCount

Struct WorkerCount 

Source
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: WorkerCount cannot 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: WorkerCount newtype wrapper with full optimization
  • Go: WorkerCount struct with equivalent interface
  • JSON: Numeric representation for API compatibility
  • Database: INTEGER column with validation constraints

Implementations§

Source§

impl WorkerCount

Source

pub const MIN_WORKERS: usize = 1usize

Minimum number of workers (always at least 1)

Source

pub const MAX_WORKERS: usize = 32usize

Maximum number of workers (prevent resource exhaustion)

Source

pub const DEFAULT_WORKERS: usize = 4usize

Default worker count for fallback scenarios

Source

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
Source

pub fn count(&self) -> usize

Returns the number of workers

Source

pub fn value(&self) -> usize

Returns the number of workers (alias for test framework compatibility)

Source

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
Source

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 bytes
  • available_cores - Number of available CPU cores
§Returns

Optimal WorkerCount considering both file size and system resources

Source

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 bytes
  • available_cores - Number of available CPU cores
  • is_cpu_intensive - Whether the processing is CPU-intensive
§Returns

Optimal WorkerCount considering processing complexity

Source

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

Source

pub fn is_suitable_for_file_size(&self, file_size: u64) -> bool

Checks if this worker count is suitable for the given file size

§Arguments
  • file_size - Size of the file in bytes
§Returns

True if the worker count is reasonable for the file size

Source

pub fn strategy_description(file_size: u64) -> &'static str

Returns a description of the worker count strategy for the given file size

§Arguments
  • file_size - Size of the file in bytes
§Returns

Human-readable description of the strategy

Source

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 count
  • available_cores - Number of available CPU cores
  • file_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

Source§

fn clone(&self) -> WorkerCount

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for WorkerCount

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for WorkerCount

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for WorkerCount

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for WorkerCount

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<WorkerCount> for usize

Source§

fn from(worker_count: WorkerCount) -> Self

Converts to this type from the input type.
Source§

impl From<usize> for WorkerCount

Source§

fn from(count: usize) -> Self

Converts to this type from the input type.
Source§

impl Hash for WorkerCount

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for WorkerCount

Source§

fn eq(&self, other: &WorkerCount) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for WorkerCount

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Copy for WorkerCount

Source§

impl Eq for WorkerCount

Source§

impl StructuralPartialEq for WorkerCount

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,