hn.today

Counting large numbers of events in small floating point registers

dotat.at3 points0 comments
Screenshot of Counting large numbers of events in small floating point registers

This describes compact probabilistic counters for tracking large event totals when storing an exact count would use too much memory. Robert Morris’s 1978 method stores an approximate logarithm in a small register and randomly advances it, with the chance of an advance decreasing as the estimated count grows. Its logarithm base lets programmers trade range against precision, and a precomputed probability table avoids expensive calculations during updates. The account then presents Miklós Csűrös’s 2009 alternative: encode the estimate as a tiny binary floating-point number, with exponent and mantissa bits. For a typical configuration, the exponent determines the scale of the represented count, while small values remain exact through denormal representations.

The implementation unpacks this compact value into an ordinary integer by restoring the implicit leading bit for normal values and shifting according to the exponent. To update it, the algorithm draws a machine word of random bits and uses a bit-scan operation to decide whether to increment: the probability falls by half for each increase in exponent. Incrementing the packed value naturally carries an overflowing mantissa into the exponent, so no separate carry logic is needed. The approach is short and fast, and its cost grows little for wider counters. Its main limitation is that available ranges and precisions come in coarse powers-of-two steps, unlike Morris’s smoothly adjustable base. Neither method is presented as universally superior; the floating-point version is appealing especially for its compact, elegant bit manipulation.

Read on dotat.at0 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Programming

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.