NewsCrawler.org

Probabilistic data structures in Go

Probabilistic data structures give approximate answers about the data they hold: whether a set contains an item, how many unique items there are, or which items appear most often. The answers may be inexact, but in return memory usage can be drastically reduced. The article introduces unsure, a Go package that implements these structures; the core logic of each one is 100–300 lines of code, it is usually several times faster than other implementations, and it has no dependencies beyond the standard library. A Bloom filter[1] can say that an item is definitely not in the set or that it probably is, and the second answer may be a false positive[2]; the filter does not store the items. For 1,000,000 distinct 8-byte strings, a regular set takes about 64 MB, while a Bloom filter with a 1% false positive rate uses 1.24 MB, which is 50 times smaller. unsure uses a blocked Bloom filter, which places each item's bits in one 512-bit block that matches the size of a CPU cache line[3], so each add or check touches a single cache line; at a 1% false positive rate it uses about 3% more memory than a standard filter. In benchmarks, unsure is about 4 times faster than bits-and-blooms, a classic Bloom filter, and 2 times faster than gloom, a blocked one.

A count-min sketch[4] is a frequency table that trades accuracy for space: it estimates how many times each item appeared, and the estimate is never less than the actual count. For the same 1,000,000 8-byte strings, with estimates within 0.01% of the total count at 99% probability, it uses only 0.55 MB, which is 116 times smaller than a regular map. The size of the sketch depends on the accuracy you choose, not on the number of distinct items or their size.

Read the original antonz.orgDiscussion on GeekNews

Discussion

Commenters point to alternatives: one suggests the CVM algorithm as a simpler option than HyperLogLog for counting unique items, and another finds a log-linear histogram simpler to implement and operate than a T-digest for quantiles. One reader notes that the comments in the Bloom filter example seem swapped: alice should be true (or, rarely, false) and carol should be false.

Sources

Published: First seen: Читать по-русски

Open as a page