A lock-free HyperLogLog on :atomics.
Estimates the number of distinct keys added with a relative standard error
of about 1.04 / sqrt(2^precision) (1.6% at the default precision of 12)
in 2^precision bytes: registers are 8 bits wide and packed eight to a
64-bit atomic, updated with compare-and-swap.
Estimates use Otmar Ertl's improved estimator, which keeps that error
across the whole range. The original estimator errs about twice as much
around 2.5 × 2^precision keys, where it switches to counting empty
registers.
Adding a key costs one hash and usually one atomic read; estimating scans every register, so estimates belong in background processes, not on the request path.
Summary
Functions
Adds key.
Estimates the number of distinct keys added to any of sketches.
Memory used by the registers, in bytes.
Creates a sketch with 2^precision registers.
Clears every register. Not atomic with respect to concurrent updates.
Types
@type t() :: %Limen.Sketch.HyperLogLog{precision: 4..16, ref: :atomics.atomics_ref()}
Functions
Adds key.
@spec cardinality(t() | [t(), ...]) :: non_neg_integer()
Estimates the number of distinct keys added to any of sketches.
Passing several sketches of the same precision estimates their union.
@spec memory(t()) :: non_neg_integer()
Memory used by the registers, in bytes.
@spec new(4..16) :: t()
Creates a sketch with 2^precision registers.
@spec reset(t()) :: :ok
Clears every register. Not atomic with respect to concurrent updates.