Crate hypercounter

Crate hypercounter 

Source
Expand description

An atomic, lock-free, hash map-like counter structure.

It uses papaya::HashMap under the hood to provide concurrent access to multiple keys at once, allowing for efficient counting without the need for locks.

§Notes Before Use

  • Operations on atomics are always wrapping on overflow.

§Getting Started

To install this library, run the following command:

cargo add hypercounter

That’s it! To start using it, create a new HyperCounter instance:

use std::sync::atomic::{AtomicUsize, Ordering};
use hypercounter::HyperCounter;

let counter: HyperCounter<String, AtomicUsize> = HyperCounter::new();

counter.fetch_add("example_key".to_string(), 1, Ordering::Relaxed);
counter.fetch_sub("example_key".to_string(), 1, Ordering::Relaxed);

Keys are automatically removed when their associated counter reaches zero. Neither inserts nor removals are needed explicitly. If you want to remove a key manually, however, you can do so using HyperCounter::swap() to swap the value with 0.

let previous_value = counter.swap("example_key".to_string(), 0, Ordering::Relaxed);

§Supported Operations

The following atomic operations are supported:

§Benchmarking

There’s a simple benchmark example included in the examples directory. You can run it using:

cargo run --example bench

This will execute a series of single-threaded benchmarks and print the operations per second for various scenarios.

Structs§

HyperCounter