A lock-free Count-Min Sketch on :atomics.
Estimates how many times each key was counted using a fixed depth × width
grid of counters, whatever the number of distinct keys. Estimates never
undercount; with N total increments they overcount by at most
e / width × N with probability 1 - e^-depth.
Row indexes are derived from two :erlang.phash2/2 hashes with the
Kirsch–Mitzenmacher construction, so an update costs
two hashes and depth atomic increments regardless of depth.
Summary
Functions
Adds count to key and returns its new estimate.
Returns the estimated count for key.
Memory used by the counters, in bytes.
Creates a sketch with width columns and depth rows.
Creates a sketch sized for an error of at most epsilon × N with
probability 1 - delta.
Resets every counter to zero.
Types
@type t() :: %Limen.Sketch.CountMin{ depth: pos_integer(), ref: :atomics.atomics_ref(), width: pos_integer() }
Functions
@spec add(t(), term(), pos_integer()) :: non_neg_integer()
Adds count to key and returns its new estimate.
@spec estimate(t(), term()) :: non_neg_integer()
Returns the estimated count for key.
@spec memory(t()) :: non_neg_integer()
Memory used by the counters, in bytes.
@spec new(pos_integer(), pos_integer()) :: t()
Creates a sketch with width columns and depth rows.
Creates a sketch sized for an error of at most epsilon × N with
probability 1 - delta.
@spec reset(t()) :: :ok
Resets every counter to zero.
Runs in O(width × depth) and is not atomic: concurrent updates may land on
either side of the reset. Limen only resets sketches of windows no request
writes to any more.