#[repr(align(64))]pub struct Counter { /* private fields */ }
Expand description
Ultra-fast atomic counter
Optimized for maximum throughput with minimal memory overhead. Cache-line aligned to prevent false sharing.
Implementations§
Source§impl Counter
impl Counter
Sourcepub fn with_value(initial: u64) -> Self
pub fn with_value(initial: u64) -> Self
Create counter with initial value
Sourcepub fn inc(&self)
pub fn inc(&self)
Increment by 1 - THE FASTEST PATH
This is optimized to be as fast as physically possible:
- Single atomic fetch_add instruction
- Relaxed memory ordering for maximum speed
- Inlined for zero function call overhead
Sourcepub fn add(&self, amount: u64)
pub fn add(&self, amount: u64)
Add arbitrary amount - also blazingly fast
Zero branch optimization - if amount is 0, still does the atomic operation to maintain consistent performance characteristics
Sourcepub fn reset(&self)
pub fn reset(&self)
Reset to zero - use sparingly
Note: This uses SeqCst ordering to ensure all threads see the reset
Sourcepub fn set(&self, value: u64)
pub fn set(&self, value: u64)
Set to specific value - use sparingly
Note: This uses SeqCst ordering for consistency
Sourcepub fn compare_and_swap(&self, expected: u64, new: u64) -> Result<u64, u64>
pub fn compare_and_swap(&self, expected: u64, new: u64) -> Result<u64, u64>
Atomic compare-and-swap
Returns Ok(previous_value) if successful, Err(current_value) if failed
Sourcepub fn add_and_get(&self, amount: u64) -> u64
pub fn add_and_get(&self, amount: u64) -> u64
Add amount and return new value
Sourcepub fn inc_and_get(&self) -> u64
pub fn inc_and_get(&self) -> u64
Increment and return new value
Sourcepub fn stats(&self) -> CounterStats
pub fn stats(&self) -> CounterStats
Get comprehensive statistics
Sourcepub fn rate_per_second(&self) -> f64
pub fn rate_per_second(&self) -> f64
Get rate per second since creation
Sourcepub fn saturating_add(&self, amount: u64)
pub fn saturating_add(&self, amount: u64)
Saturating add - won’t overflow past u64::MAX
Source§impl Counter
Batch counter operations for even better performance
impl Counter
Batch counter operations for even better performance