Sub topic 10
HyperLogLog++: what production implementations change
This chapter assumes you have read the earlier chapters and can follow the HyperLogLog class from the implementation chapter: registers, rank, harmonic mean, and linear counting. It covers what Google's HyperLogLog++ paper (Heule, Nunkesser and Hall, 2013) changed, and why those changes matter in real systems.
Why the original needs adjusting
The original HyperLogLog paper assumed a 32-bit hash. It had three weak spots:
- Large counts. Near 2^32 distinct items, hash collisions bias the estimate, so the paper added a large-range correction.
- Mid-range counts. Just above the linear-counting cutoff of 2.5·m, the raw harmonic-mean estimate is noticeably biased upward.
- Small counts. A dense array of m registers costs the full memory even when the sketch holds ten items. That hurts when you keep millions of sketches, for example one per page or per day.
HyperLogLog++ addresses each one.
The three changes
| Change | Problem it solves | Effect |
|---|---|---|
| 64-bit hash | Collisions near 2^32 items | The large-range correction is no longer needed |
| Empirical bias correction | Upward bias of the raw estimate for roughly n < 5m | Lower error in the mid range |
| Sparse representation | Wasted memory for small sets | Small sketches use far less than m registers |
The HyperLogLog class you built already uses a 64-bit hash, so it needs no large-range correction.
Bias correction
The authors ran many simulations for each precision. They recorded the average gap between the raw estimate and the true count. At query time the implementation subtracts an interpolated bias from the raw estimate. The paper also replaces the fixed 2.5·m rule with a measured threshold per precision for choosing between linear counting and the corrected estimate.
The correction tables are specific to each precision and hash function. You cannot derive them from a formula. Some systems skip the tables. Redis, for example, uses an improved estimator by Otmar Ertl that needs no lookup data.
Sparse mode
While a sketch holds few items, most registers are zero. Instead of the dense array, HyperLogLog++ stores a compact list of (index, rank) pairs. It uses a higher precision p′ (25 in the paper), so the sparse estimate is more accurate than the dense one would be. Once the list grows past the size of the dense array, the sketch converts to dense registers at the normal precision p.
Conversion needs one detail. A sparse pair was recorded at precision p′, and the dense array uses the smaller p. The extra p′ − p index bits belong to the "rest" of the hash at precision p, so the rank must be recomputed from them:
import random
def index_and_rank(x, p):
bits = 64 - p
rest = x & ((1 << bits) - 1)
return x >> bits, bits - rest.bit_length() + 1
def to_dense(index_hi, rank_hi, p_hi, p):
extra = p_hi - p
low = index_hi & ((1 << extra) - 1) # index bits that now belong to the rank
if low:
rank = extra - low.bit_length() + 1
else:
rank = extra + rank_hi
return index_hi >> extra, rank
random.seed(1)
for _ in range(100_000):
x = random.getrandbits(64)
hi_index, hi_rank = index_and_rank(x, 25)
assert to_dense(hi_index, hi_rank, 25, 12) == index_and_rank(x, 12)
print("conversion matches direct computation")The assertion confirms that converting a sparse pair gives exactly the register update the dense sketch would have made. No information is lost in the conversion.
What this means in practice
Redis combines these ideas. Its HyperLogLog key has 16,384 registers (p = 14) at 6 bits each, so a dense sketch takes about 12 KB and has a standard error of 0.81%. Small keys use a sparse encoding and convert to dense automatically. The conversion also happens if a sparse value exceeds a size limit.
Choosing between plain and enhanced versions
- If you hold a few large sketches, the basic algorithm with a 64-bit hash is nearly as good, and simpler to audit.
- If you hold many sketches, most of them small, sparse mode saves much of the memory.
- If your counts often sit between about m and 5m, bias correction is worth having, since that is where the plain estimator is weakest.
Exercise
A sparse pair was recorded at p′ = 14 as index 0b10110100110101 with rank 3. The sketch is converted to dense at p = 12. What are the new index, and the new rank?
Show answer
The index has 14 bits, so extra = 2. The new index is the top 12 bits: 0b101101001101, which is 2893. The low two bits are 0b01, which is nonzero. Its bit_length is 1, so rank = extra − 1 + 1 = 2. The original rank of 3 is not used, because the first 1-bit now falls inside the former index bits.