use std::cell::Cell;
use std::sync::atomic::AtomicU64;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
use std::sync::LazyLock;
use std::sync::OnceLock;
const MAX_CELLS: usize = 64;
static CELL_COUNT: LazyLock<usize> = LazyLock::new(|| {
std::thread::available_parallelism()
.map_or(4, usize::from)
.next_power_of_two()
.min(MAX_CELLS)
});
static NEXT_THREAD_SLOT: AtomicUsize = AtomicUsize::new(0);
thread_local! {
static THREAD_SLOT: Cell<Option<usize>> = const { Cell::new(None) };
}
#[inline]
fn thread_slot() -> usize {
THREAD_SLOT.with(|slot| match slot.get() {
Some(index) => index,
None => {
let index = NEXT_THREAD_SLOT.fetch_add(1, Ordering::Relaxed);
slot.set(Some(index));
index
}
})
}
#[repr(align(128))]
#[derive(Default)]
struct PaddedCounter(AtomicU64);
#[derive(Default)]
pub(crate) struct StripedCounter {
base: AtomicU64,
cells: OnceLock<Box<[PaddedCounter]>>,
}
impl StripedCounter {
#[inline]
pub(crate) fn add(&self, delta: u64) {
if delta == 0 {
return;
}
if let Some(cells) = self.cells.get() {
Self::add_to_cell(cells, delta);
return;
}
let current = self.base.load(Ordering::Relaxed);
if self
.base
.compare_exchange(
current,
current.wrapping_add(delta),
Ordering::Relaxed,
Ordering::Relaxed,
)
.is_err()
{
let cells = self
.cells
.get_or_init(|| (0..*CELL_COUNT).map(|_| PaddedCounter::default()).collect());
Self::add_to_cell(cells, delta);
}
}
#[inline]
fn add_to_cell(cells: &[PaddedCounter], delta: u64) {
cells[thread_slot() & (cells.len() - 1)]
.0
.fetch_add(delta, Ordering::Relaxed);
}
pub(crate) fn sum(&self) -> u64 {
let base = self.base.load(Ordering::Relaxed);
self.cells.get().map_or(base, |cells| {
cells
.iter()
.fold(base, |total, cell| total.wrapping_add(cell.0.load(Ordering::Relaxed)))
})
}
#[cfg(test)]
fn is_striped(&self) -> bool {
self.cells.get().is_some()
}
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use std::sync::Barrier;
use super::*;
#[test]
fn uncontended_additions_stay_in_the_base_word() {
let counter = StripedCounter::default();
counter.add(5);
counter.add(0);
counter.add(7);
assert_eq!(counter.sum(), 12);
assert!(!counter.is_striped());
}
#[test]
fn concurrent_additions_are_all_counted() {
const THREADS: usize = 8;
const ADDS: u64 = 20_000;
let counter = Arc::new(StripedCounter::default());
let start = Arc::new(Barrier::new(THREADS));
let workers = (0..THREADS)
.map(|_| {
let counter = Arc::clone(&counter);
let start = Arc::clone(&start);
std::thread::spawn(move || {
start.wait();
for _ in 0..ADDS {
counter.add(3);
}
})
})
.collect::<Vec<_>>();
for worker in workers {
worker.join().expect("counter worker");
}
assert_eq!(counter.sum(), THREADS as u64 * ADDS * 3);
}
#[test]
fn striped_sum_includes_the_base_and_every_cell() {
let counter = StripedCounter::default();
counter.add(10);
let cells = counter
.cells
.get_or_init(|| (0..*CELL_COUNT).map(|_| PaddedCounter::default()).collect());
cells[0].0.fetch_add(4, Ordering::Relaxed);
counter.add(6);
assert!(counter.is_striped());
assert_eq!(counter.sum(), 20);
}
#[test]
fn sum_wraps_like_the_previous_atomic() {
let counter = StripedCounter::default();
counter.add(u64::MAX);
counter.add(2);
assert_eq!(counter.sum(), 1);
}
}