kumulant

HyperLogLogStat

class HyperLogLogStat(val precision: Int = StatDefaults.HYPER_LOG_LOG_PRECISION, val hasher: LongHasher = SplitMix64, val concurrency: Concurrency = Concurrency.None) : DiscreteStat<HyperLogLogResult> (source)

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

constructor(precision: Int = StatDefaults.HYPER_LOG_LOG_PRECISION, hasher: LongHasher = SplitMix64, concurrency: Concurrency = Concurrency.None)(source)

Properties

concurrency

open override val concurrency: Concurrency(source)

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

Number of register-index bits; memory is 2^precision bytes.

Functions

create

open override fun create(concurrency: Concurrency? = null): HyperLogLogStat(source)

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

open override fun merge(values: HyperLogLogResult, timestampNanos: Long = currentTimeNanos())(source)

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

open override fun read(timestampNanos: Long = currentTimeNanos()): HyperLogLogResult(source)

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

open override fun reset()(source)

Restore the configured baseline, including any prior seed. Preserves the configuration and concurrency.

validateMerge

open override fun validateMerge(values: HyperLogLogResult)(source)

Validate values for merge without changing accumulator state. Containers validate every child before merging any of them. Successful validation checks compatibility; it does not make a concurrent merge transactional.

Link copied to clipboard
open fun update(value: Long, weight: Double = 1.0)

Record value with weight at the current monotonic time.

open override fun update(value: Long, timestampNanos: Long, weight: Double = 1.0)

Record value with weight at timestampNanos.

update

open override fun update(value: Long, timestampNanos: Long, weight: Double = 1.0)(source)

Record value with weight at timestampNanos.