Simple, fast and self-contained structs for tracking whether a newly observed
u64) is greater than the largest previously observed value.
Use cases include message sequence numbers, timestamps, and other situations that present a need to quickly assess whether incoming data is “new”, i.e. its numbering is larger than any previous value.
All of the structs include an
is_new function that returns
true if the
passed value is a new maximum, while simultaneously storing the new value to
check against future values.
Two of the
is_new implementations (
Map) require an
AtomicMap require only
RcIncr’s interior mutability and
AtomicIncr’s thread safe syncrhonization.
The cost of checking a new value is minimal: 0-2ns for the single-threaded
implementations, and ~5-10ns for
AtomicIncr, except in cases of pathological
contention. In a worst-case, nightmare scenrio benchmark for the
it’s possible to induce delays of hundreds of nanoseconds. A more realistic
case of 24 threads contending to increment the atomic but yielding each iteration
resulted in checks in the ~5-10ns range.
Enabling the “nightly” feature (on by default) allows the use of
as the backing storage for
AtomicUsize otherwise). Also,
nightly is required to run the benchmarks.
Enabling the “fnv” feature will use a Fowler-Noll-Vo hash function from the
crate for the
HashMap used by
AtomicMap. FNV is faster than the
default hasher but provides no protection against malicious inputs.
AtomicIncris a threadsafe, yet very fast counter, utilizing compare and swap instructions to provide speed and safety in the same package. There are some cases where 5ns matters. But in many, many other situations, it’s a perfectly good decision to just use the
AtomicIncr, knowing it can handle anything, and move on to other problems.
AtomicMapprovides simple, fast sequence checking by key, but with the thread-safe backing storage of
- A self-contained struct for quickly checking whether a newly observed value is greater than any previously observed value.
- A map interface allowing fast checks of whether a newly observed value is greater than any previously observed value for a given key.
Rc<Cell<_>>backed storage of
RcIncrprovides flexibility in situations where the counter must be shared among several disparate objects/contexts while retaining consistentcy between all the references of the count.
RcIncris not threadsafe, and even in single-threaded code
Rc<Cell<T>>has some tricky edge cases, for instance if a
Cell<T>is used as the key to a hash map and the interior value mutated (fair warning).