Probabilistic data structures in Go, part 2

本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。

Probabilistic data structures give you approximate answers to questions about the data they handle. Does the set contain a certain item? How many unique items are in the collection? What is the 99th percentile in a stream of numbers? How similar are two sets? Unlike deterministic structures, the answers might not be exact, but in return, you can save a lot of memory.

概率数据结构可以为您处理的数据相关问题提供近似答案。该集合是否包含特定项?集合中有多少唯一项?数字流中的第 99 百分位数是多少?两个集合有多相似?与确定性结构不同,其答案可能并不精确,但作为回报,您可以节省大量内存。

Previously, we reviewed the following data structures: Bloom filter (for checking if an item is in a set), Count-min sketch (for counting items), HyperLogLog (for counting unique items), T-digest (for finding quantiles), and Top-k (for finding the most frequent items). In this post, we’ll talk about other probabilistic structures and compare them to the ones we’ve already covered. We’ll look at how they work and what kinds of guarantees they provide.

此前,我们回顾了以下数据结构:布隆过滤器(用于检查某项是否在集合中)、计数-最小草图(用于计数)、HyperLogLog(用于统计唯一项)、T-digest(用于查找分位数)以及 Top-k(用于查找最频繁项)。在本文中,我们将讨论其他概率结构,并将它们与我们已经介绍过的内容进行比较。我们将探讨它们的工作原理以及它们提供何种保证。

All examples use unsure — a package that implements these structures in Go. Binary fuse filter • UltraLogLog • Log-linear histogram • MinHash

所有示例均使用 unsure——一个在 Go 中实现这些结构的包。二进制熔断过滤器 (Binary fuse filter) • UltraLogLog • 对数线性直方图 (Log-linear histogram) • MinHash

Binary fuse filter: A binary fuse filter is a set that trades certainty for space, like a Bloom filter. It can tell you that an item is definitely not in the set, or that it probably is (which might be a false positive). The filter doesn’t store the items, so it’s much more compact than a regular set.

二进制熔断过滤器:二进制熔断过滤器是一种以确定性换取空间的集合,类似于布隆过滤器。它可以告诉您某项肯定不在集合中,或者可能在集合中(这可能是误报)。该过滤器不存储具体项,因此比常规集合紧凑得多。

Unlike a Bloom filter, a binary fuse filter is static: you build it once from all the items, and you can’t add more later. In exchange, it uses less memory than a Bloom filter with the same false positive rate. For example, a fuse filter for 1,000,000 items with a 0.39% false positive rate takes 1.13 MB (9 bits per item), while a Bloom filter takes 1.51 MB (12 bits per item).

与布隆过滤器不同,二进制熔断过滤器是静态的:您根据所有项构建一次,之后无法再添加。作为交换,在相同的误报率下,它比布隆过滤器使用更少的内存。例如,针对 1,000,000 个项且误报率为 0.39% 的熔断过滤器占用 1.13 MB(每项 9 位),而布隆过滤器则占用 1.51 MB(每项 12 位)。

This makes binary fuse filters a good fit for data that is written once and read many times, like the key set of an immutable file in a database (to skip files that don’t have the key), or a list of known bad items, such as leaked passwords or malicious URLs.

这使得二进制熔断过滤器非常适合“一次写入、多次读取”的数据,例如数据库中不可变文件的键集(用于跳过不包含该键的文件),或已知不良项的列表,如泄露的密码或恶意 URL。

UltraLogLog is a unique items counter that trades accuracy for space, like HyperLogLog. It tells you approximately how many distinct items it has seen, no matter how many times each one appeared. UltraLogLog doesn’t store the items, so it’s much more compact than a regular set.

UltraLogLog 是一种唯一项计数器,它像 HyperLogLog 一样以准确性换取空间。无论每个项出现了多少次,它都能告诉您大约见过多少个不同的项。UltraLogLog 不存储这些项,因此比常规集合紧凑得多。

UltraLogLog is a newer design (Otmar Ertl, 2024) that uses the space more efficiently: for the same error, it needs about 43% less memory than HyperLogLog. For example, a HyperLogLog sketch with 0.81% standard error takes 16 KB, while an UltraLogLog sketch with 0.86% error takes 8 KB. However, adding, counting, and merging are slower than with HyperLogLog.

UltraLogLog 是一种较新的设计(Otmar Ertl, 2024),它更有效地利用了空间:在相同的误差下,它比 HyperLogLog 节省约 43% 的内存。例如,标准误差为 0.81% 的 HyperLogLog 草图占用 16 KB,而误差为 0.86% 的 UltraLogLog 草图仅占用 8 KB。然而,其添加、计数和合并操作比 HyperLogLog 更慢。