Struct ConcurrentCounter

Source
pub struct ConcurrentCounter { /* private fields */ }
Expand description

A sharded atomic counter

ConcurrentCounter shards cacheline aligned AtomicIsizes across a vector for faster updates in a high contention scenarios.

Implementations§

Source§

impl ConcurrentCounter

Source

pub fn new(count: usize) -> Self

Creates a new ConcurrentCounter with a minimum of the count cells. Concurrent counter will align the count to the next power of two for better speed when doing the modulus.

§Examples
use fast_counter::ConcurrentCounter;

let counter = ConcurrentCounter::new(10);
Source

pub fn add(&self, value: isize)

Adds the value to the counter, internally with is using add_with_ordering with a Ordering::Relaxed and is mainly for convenience.

ConcurrentCounter will identify a cell to add the value too with using a thread_local which will try to aleviate the contention on a single number

§Examples
use fast_counter::ConcurrentCounter;

let counter = ConcurrentCounter::new(10);
counter.add(1);
counter.add(-1);
Source

pub fn add_with_ordering(&self, value: isize, ordering: Ordering)

ConcurrentCounter will identify a cell to add the value too with using a thread_local which will try to aleviate the contention on a single number. The cell will be updated atomically using the ordering provided in ordering

§Examples
use fast_counter::ConcurrentCounter;
use std::sync::atomic::Ordering;

let counter = ConcurrentCounter::new(10);
counter.add_with_ordering(1, Ordering::SeqCst);
counter.add_with_ordering(-1, Ordering::Relaxed);
Source

pub fn sum(&self) -> isize

This will fetch the sum of the concurrent counter be iterating through each of the cells and loading the values. Internally this uses sum_with_ordering with a Relaxed ordering.

Due to the fact the cells are sharded and the concurrent nature of the library this sum may be slightly inaccurate. For example if used in a concurrent map and using ConcurrentCounter to track the length, depending on the ordering the length may be returned as a negative value.

§Examples
use fast_counter::ConcurrentCounter;

let counter = ConcurrentCounter::new(10);

counter.add(1);

let sum = counter.sum();

assert_eq!(sum, 1);
Source

pub fn sum_with_ordering(&self, ordering: Ordering) -> isize

This will fetch the sum of the concurrent counter be iterating through each of the cells and loading the values with the ordering defined by ordering.

Due to the fact the cells are sharded and the concurrent nature of the library this sum may be slightly inaccurate. For example if used in a concurrent map and using ConcurrentCounter to track the length, depending on the ordering the length may be returned as a negative value.

§Examples
use std::sync::atomic::Ordering;
use fast_counter::ConcurrentCounter;

let counter = ConcurrentCounter::new(10);

counter.add(1);

let sum = counter.sum_with_ordering(Ordering::SeqCst);

assert_eq!(sum, 1);

Trait Implementations§

Source§

impl Debug for ConcurrentCounter

Source§

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

Formats the value using the given formatter. Read more

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> 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, 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.