Probabilistic data structures in Go

本文为原文前 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? Which items appear most often? Unlike deterministic structures like sets and frequency tables, the answers might not be exact, but in return, you can drastically reduce memory usage.

概率数据结构可以为你处理的数据相关问题提供近似答案。集合中是否包含某个特定项?集合中有多少唯一项?哪些项出现的频率最高?与集合和频率表等确定性结构不同,这些答案可能并不精确,但作为回报,你可以大幅减少内存占用。

In this post, we’ll talk about what probabilistic structures are, how they work, and what kinds of guarantees they provide. I’ll also introduce unsure — a package that implements these structures in Go. It’s simple (the core logic for each data structure is 100–300 LOC), fast (usually several times faster than other implementations), and has no dependencies except for the standard library.

在这篇文章中,我们将讨论什么是概率结构、它们如何工作以及它们提供什么样的保证。我还将介绍 unsure,这是一个在 Go 中实现这些结构的包。它非常简单(每个数据结构的核心逻辑仅为 100–300 行代码)、速度快(通常比其他实现快几倍),并且除了标准库之外没有任何依赖项。

Bloom filter • Count-min sketch • HyperLogLog • T-digest • Top-k

布隆过滤器 (Bloom filter) • 计数-最小草图 (Count-min sketch) • HyperLogLog • T-digest • Top-k

Bloom filter

布隆过滤器

A Bloom filter is a set that trades certainty for space. 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 actually store the items, so it’s much more compact than a regular set.

布隆过滤器是一种以确定性换取空间的集合。它可以告诉你某个项肯定不在集合中,或者它可能在集合中(这可能是误报)。过滤器实际上并不存储这些项,因此它比常规集合紧凑得多。

For example, a regular set with 1,000,000 distinct 8-byte strings takes about 64 MB. A Bloom filter for the same strings, with a 1% false positive rate, uses only 1.24 MB (about 10 bits per item) — that’s 50 times smaller. The size of the filter doesn’t depend on the item size, so the difference gets even bigger with longer strings.

例如,一个包含 1,000,000 个不同 8 字节字符串的常规集合大约占用 64 MB。对于相同的字符串,一个误报率为 1% 的布隆过滤器仅占用 1.24 MB(每个项约 10 位)——这比前者小了 50 倍。过滤器的大小不依赖于项的大小,因此对于更长的字符串,这种差异会变得更大。

A Bloom filter often resides in memory and helps minimize expensive I/O operations such as disk reads or network calls. You check if an item is in the filter, and if the answer is “no”, you skip the costly lookup. If the answer is “yes”, you still have to perform the lookup because it might be a false positive.

布隆过滤器通常驻留在内存中,有助于最大限度地减少昂贵的 I/O 操作,例如磁盘读取或网络调用。你检查某个项是否在过滤器中,如果答案是“否”,则跳过昂贵的查找过程。如果答案是“是”,你仍然必须执行查找,因为它可能是误报。

// A filter for 1000 items with a 1% false positive rate. filter := bloom.New(1000, 0.01) filter.Add([]byte(“alice”)) filter.Add([]byte(“bob”)) filter.Contains([]byte(“alice”)) // true filter.Contains([]byte(“carol”)) // false (or, rarely, true)

// 为 1000 个项设置 1% 误报率的过滤器。 filter := bloom.New(1000, 0.01) filter.Add([]byte(“alice”)) filter.Add([]byte(“bob”)) filter.Contains([]byte(“alice”)) // true filter.Contains([]byte(“carol”)) // false(或极少数情况下为 true)

The filter is sized for the given number of items with a given false positive rate. You can add more, but the false positive rate will go up. You can add an item to a Bloom filter and check if an item is in the filter, but you cannot delete an item, count the number of items, or iterate over them.

过滤器的大小是根据给定的项数和给定的误报率来设定的。你可以添加更多项,但误报率会随之上升。你可以向布隆过滤器添加项并检查项是否在过滤器中,但你不能删除项、统计项的数量或遍历它们。

How it works 如何工作

Bloom filter is a large array of m bits, all set to zero at first. To add an item, you compute k hash values for it and set the k bits at those positions. To check if an item is in the filter, you compute the same k hashes and check if all k bits are set. If any of the bits is not set, the item definitely was never added. If all the bits are set, the item was probably added, but it’s not guaranteed (the bits could have been set by other items).

布隆过滤器是一个包含 m 位的巨大数组,最初全部设为零。要添加一个项,你需要计算它的 k 个哈希值,并将这些位置上的 k 位设为 1。要检查一个项是否在过滤器中,你需要计算相同的 k 个哈希值,并检查这 k 位是否都已设置。如果其中任何一位未设置,则该项肯定从未被添加过。如果所有位都已设置,则该项可能已被添加,但这不能保证(这些位可能是由其他项设置的)。

The k bits are randomly distributed throughout the entire array. When the filter is larger than the CPU cache, each bit access could result in a cache miss, so a lookup may wait on memory k times.

这 k 位随机分布在整个数组中。当过滤器大于 CPU 缓存时,每次位访问都可能导致缓存未命中,因此查找操作可能需要等待内存 k 次。

Unsure uses a blocked Bloom filter. The bit array is divided into 512-bit blocks, each matching the size of a CPU cache line. When you add or check an item, it is hashed once: part of the hash selects a block, and the rest selects k bits within that block. Each add or check only accesses one cache line, so the filter stays fast even when it’s very large.

Unsure 使用了分块布隆过滤器。位数组被划分为 512 位的块,每个块的大小与 CPU 缓存行相匹配。当你添加或检查一个项时,它会被哈希一次:哈希的一部分用于选择块,其余部分用于选择该块内的 k 位。每次添加或检查只访问一个缓存行,因此即使过滤器非常大,它也能保持快速。

In a blocked Bloom filter, some blocks end up with more items than others, which increases the false positive rate. To make up for this, the filter needs to be a bit larger. Compared to a standard Bloom filter, it uses about 3% more memory at a 1% false positive rate, 8% more at 0.1%, and 15% more at 0.01%.

在分块布隆过滤器中,某些块最终包含的项比其他块多,这会增加误报率。为了弥补这一点,过滤器需要稍微大一点。与标准布隆过滤器相比,在 1% 的误报率下,它多占用约 3% 的内存;在 0.1% 时多占用 8%;在 0.01% 时多占用 15%。

The filter uses the local wyhash implementation for hashing. It performs roughly the same as xxh3 but has a much simpler implementation, without SIMD or assembly.

该过滤器使用本地的 wyhash 实现进行哈希处理。它的性能与 xxh3 大致相同,但实现要简单得多,且不依赖 SIMD 或汇编。

Benchmarks 基准测试

Unsure is about 4× faster than bits-and-blooms and 2× faster than gloom, using 1–8% more memory than bits-and-blooms and 4–10% less than gloom.

Unsure 比 bits-and-blooms 快约 4 倍,比 gloom 快 2 倍,内存使用量比 bits-and-blooms 多 1–8%,比 gloom 少 4–10%。

Count-min sketch

计数-最小草图

A count-min sketch is a frequency table that trades accuracy for space. It tells you approximately how many times each item has appeared. The estimate is never less than the actual count, but it can be higher. The sketch doesn’t actually store the items, so it’s much more compact than a regular map of per-item counts.

计数-最小草图是一种以准确性换取空间的频率表。它告诉你每个项大约出现了多少次。估计值永远不会小于实际计数,但可能会更高。草图实际上并不存储这些项,因此它比常规的项计数映射要紧凑得多。

For example, a regular map counting 1,000,000 distinct 8-byte strings takes about 64 MB. A count-min sketch for the same strings…

例如,一个统计 1,000,000 个不同 8 字节字符串的常规映射大约占用 64 MB。对于相同的字符串,一个计数-最小草图……