HyperLogLogStat
HyperLogLog cardinality estimator with a small-range linear-counting fallback.
Allocates m = 2^precision registers and uses the standard alpha_m * m^2 / Sum 2^-Mj estimator, switching to linear counting on small inputs (rawE <= 2.5*m with at least one empty register) to eliminate the well-known HLL bias near zero. Inputs are run through the hasher (default SplitMix64) before bucketing so callers can pass raw IDs without worrying about hash quality.
Memory: m Longs (registers) plus a counter. Standard error is ~ 1.04/sqrtm (~ 0.81% at the default precision = 14). 64-bit hashing makes the original HLL large-range correction unnecessary.
Use cases: distinct-value estimation under tight memory (count unique users, unique error fingerprints, unique IPs).
Memory: O(m) = O(2^precision) bytes, plus a totalSeen counter.
Update: O(1) per observation; one hash + register CAS-max.
Concurrency: Per-register single-cell CAS-max loop on a striped Long array; totalSeen is a separate atomic add. Lock-free and exact under every Concurrency level; racing writers on the same register preserve the max-over-incoming-rho invariant via CAS retry.
Constructors
HyperLogLogStat
Properties
concurrency
Thread-safety mode fixed at construction. See Concurrency for the shared contract and the implementation's KDoc for its cell and lock strategy.
hasher
Mixer applied to each input before bucketing; defaults to SplitMix64.
precision
Functions
create
Create an independent accumulator at the configured baseline. A null concurrency preserves this accumulator's mode; a non-null value overrides it. Modality subtypes narrow the return type to preserve the input shape.
merge
Fold a compatible snapshot values into this accumulator at timestampNanos. Implementations document whether the combine is exact or approximate and which state the snapshot omits. The source accumulator is unaffected.
Time-dependent statistics, such as windows, place the snapshot at timestampNanos, which must share the clock the caller uses for updates and reads. Time-independent statistics ignore it.
read
Return the current result evaluated at timestampNanos. Time-independent statistics ignore the timestamp. Reading does not add an observation.
Snapshot consistency depends on the implementation's synchronisation. Locks protect coupled state under Concurrency.Strict and Concurrency.HighWrite; independently updated cells and JVM striped adders can still yield a best-effort snapshot. Concurrency.Relaxed permits drift across coupled cells.
reset
Restore the configured baseline, including any prior seed. Preserves the configuration and concurrency.