The Power of Hashing with Mersenne Primes

Thomas Dybdahl Ahle, Jakob Bæk Tejs Knudsen, Mikkel ThoruparXiv 2020

Introduction

In high-speed data streaming and large-scale data analysis, hashing is often the primary computational bottleneck. Estimating properties of a data stream, such as the number of unique elements or the "second moment," typically relies on "sketches" like the Count Sketch. These sketches require $k$-universal hashing, which traditionally involves calculating high-degree polynomials over a prime field $\mathbb{Z}_p$.

This paper demonstrates that hashing with Mersenne primes ($p = 2^b - 1$) offers significant advantages beyond simple computational speed. The authors introduce a "Two-for-one" trick that allows a single hash value to be split into multiple independent components—such as an index and a sign bit—while maintaining rigorous theoretical guarantees. Furthermore, they provide a new, branch-free algorithm for division and modulus that is 2–3x faster than previous state-of-the-art methods.

Methodology

The researchers propose several novel algorithmic and analytical approaches to optimize hashing performance and accuracy.

$k$-Universal Hashing over Mersenne Primes

The core of the approach involves evaluating polynomials over the field $\mathbb{Z}_p$. Algorithm 1 illustrates the efficient computation of $y = h_{\vec{a}}(x) = \sum_{i \in [k]} a_i x^i \mod p$, which forms the basis for $k$-universal hashing. Notably, it utilizes the structure of Mersenne primes to perform modulus operations using simple bit-shifts and additions.

Algorithm 1: Efficient polynomial evaluation for k-universal hashing over a Mersenne prime field.

The "Two-for-one" Trick

A key innovation is the "Two-for-one" trick. The authors show that a single $b$-bit hash value from a Mersenne prime field can be split: the lower bits can serve as a bucket index, while the highest bit acts as a sign bit ($\pm 1$). Despite not being perfectly independent, these components satisfy a property called "Sign Near Cancellation," which is sufficient for algorithms like Count Sketch.

Algorithm 3: The "Two-for-one" trick for extracting an index and a sign bit from a single hash value.

Branch-free Division and Most-Uniform Mapping

Standard division is computationally expensive. The authors introduce a branch-free algorithm that uses bit-shifts and additions to compute quotients and remainders, avoiding CPU stalls caused by branch mispredictions. Additionally, they provide a "Most-Uniform Mapping" formula, $\mu(v) = ((v + 1)r) \gg b$, to map hash values into an arbitrary number of buckets $r$ with maximum uniformity.

Evaluation and Results

The methods were evaluated using synthetic benchmarks consisting of $10^7$ random 64-bit keys across multiple CPU architectures, including Intel Core i7 and Xeon processors.

Performance Gains

The new branch-free division algorithm proved to be 2–3 times faster than the modified Crandall algorithm, the previous state-of-the-art. For 4-universal and 8-universal hashing, the Mersenne prime methods outperformed hardware-specific carry-less multiplication (CLMUL) on all tested CPUs. On an Intel Xeon, 8-universal hashing was nearly 10% faster than the hardware-accelerated alternative.

Theoretical and Accuracy Insights

The authors provide the first rigorous proof that splitting a Mersenne hash value preserves the statistical properties required for second-moment estimation. Interestingly, their analysis shows that the relative error in Count Sketches using this approach is $n/p^2$ rather than the expected $n/p$. This allows for the use of smaller primes, saving memory and computation time without sacrificing accuracy.

Discussion and Future Work

The unique structure of Mersenne primes ($p = 2^b - 1$)—essentially a $b$-bit string missing only the "all 1s" value—is what enables these optimizations. These tricks do not translate well to other prime types, such as Fermat primes, where bit-splitting introduces significant bias.

While the algorithms offer substantial speedups "near the metal," some advantages can be obscured by the overhead of high-level libraries like GMP. Future work may involve generalizing the "most uniform map" to other sketch types, potentially leading to a broader class of "Mersenne-optimized" streaming algorithms.

Summary updated

BibTeX
@misc{ahle2020power,
  author = {Thomas Dybdahl Ahle and Jakob Bæk Tejs Knudsen and Mikkel Thorup},
  title = {{The Power of Hashing with Mersenne Primes}},
  year = {2020},
  eprint = {2008.08654},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2008.08654},
}
Figure