Locally Uniform Hashing

Ioana O. Bercea, Lorenzo Beretta, Jonas Klausen, Jakob Bæk Tejs Houen, Mikkel Thorup

TL;DR

Tornado tabulation hashing provides a practical bridge between the "unrealistic" ideal of fully random hashing and the fast-but-flawed hash functions used in production. By systematically breaking dependencies through a series of "twisted" table lookups, it offers strong theoretical guarantees—behaving almost exactly like a fully random function for algorithms like linear probing and HyperLogLog—while remaining fast enough for high-volume data streams.

What Problem Does This Paper Address?

In computer science theory, many algorithms (like linear probing for hash tables) are analyzed assuming access to a "fully random" hash function. In reality, such functions are impossible to implement efficiently because they would require astronomical amounts of memory.

Practitioners usually settle for "weak" hash functions, like linear hashing (2-independent). While these are fast, they have a dangerous "dark side": they can fail spectacularly on certain types of structured data (e.g., dense sets or arithmetic sequences), causing performance to plummet from constant time to logarithmic time. Previous attempts to fix this, like "mixed tabulation," often came with theoretical bounds that only worked for massive, impractical character sizes. The authors aim to provide a hash function that is simple, fast, and comes with "unconditional" guarantees that hold even for the small 8-bit characters used in modern CPU caches.

Methods

The authors introduce Tornado Tabulation Hashing. It builds on "simple tabulation," where a key is split into characters (e.g., a 32-bit key into four 8-bit characters), each character is used to look up a random value in a table, and the results are XORed together.

The "Tornado" innovation lies in how it generates derived characters:

  1. The Twist: Instead of just XORing values, the algorithm progressively defines new characters. Each new character is a hash of all preceding characters in the sequence.
  2. Dependency Breaking: This cascading effect (hence "tornado") systematically breaks linear dependencies between input keys.
  3. Local Uniformity: The paper proves that this method achieves "local uniformity." This means that for any small subset of keys that an algorithm actually "sees" at one time (like keys hashing to the same neighborhood in a hash table), the hash values behave as if they were perfectly random.
  4. Tornado-Mix: For cases where you need randomness over very large sets of keys, they propose a hybrid version that uses a few larger tables (outside of fast cache) to maintain independence.

The implementation is remarkably simple—the paper even includes a concise C implementation that uses basic shifts, XORs, and table lookups.

Datasets

As a theoretical algorithms paper, it does not use standard machine learning datasets. Instead, the authors:

  • Reference previous studies [1, 2] that used synthetic data and real-world network traffic data to demonstrate how weak hash functions fail.
  • Focus on mathematical proofs to characterize the performance of the hash function across all possible input sets.
  • Evaluate the computational speed of their C implementation on standard hardware, comparing it to degree-2 polynomials over Mersenne prime fields.

Key Insights and Ideas

  • The Power of "Almost" Random: You don't need a globally fully random function to get the benefits of one. If your hash function is "locally uniform," diverse algorithms like linear probing, HyperLogLog, and one-permutation hashing will perform as if they were using abstract, fully random hashing.
  • Twisting for Free: The "twist" operation (making the $c$-th character depend on the first $c-1$ characters) doesn't increase the number of table lookups but significantly improves the error probability bounds—essentially giving you an extra "level" of randomness for free.
  • Breaking the Exponential Curse: Previous schemes like mixed tabulation had an exponential dependence on the number of characters ($c$), making their theoretical guarantees meaningless for 8-bit or 16-bit characters. Tornado hashing avoids this, providing meaningful error bounds for the table sizes actually used in practice.

Key Results

  • Linear Probing: The paper proves that Tornado hashing yields an expected probe length of $(1 + o(1))(1 + 1/\epsilon^2)/2$. This matches Knuth’s classic 1963 analysis for fully random hashing, meaning Tornado hashing is "provably good" for hash tables.
  • Full Randomness on Fixed Sets: For any fixed set of $n$ keys, if $n \le |\Sigma|/2$ (where $|\Sigma|$ is the alphabet size, e.g., 256), the hash values are completely independent with very high probability.
  • Efficiency: The C implementation is highly competitive, performing at speeds similar to a degree-2 polynomial, which is the gold standard for fast, reasonably strong hashing.
  • The Splitting Trick: They provide a more efficient implementation of the "splitting trick" (used to build complex data structures like succinct dictionaries), reducing the space and time overhead compared to previous methods using Siegel’s highly independent hashing.

Limitations and Future Work

  • Cache Dependence: The speed of tabulation hashing relies heavily on the lookup tables fitting into the CPU's fast cache (L1/L2). If the alphabet size $|\Sigma|$ is too large, performance will degrade due to cache misses.
  • Architecture Sensitivity: The authors note that the relative cost of table lookups vs. multiplication varies by CPU architecture, so the "fastest" method may vary across different hardware.
  • Complexity of Local Uniformity: While the implementation is simple, the definition and proof of local uniformity are mathematically complex. The authors suggest that while they have shown it works for several major algorithms, further work could explore exactly which classes of algorithms benefit from this property.

Summary updated

BibTeX
@inproceedings{bercea2023locally,
  author = {Ioana O. Bercea and Lorenzo Beretta and Jonas Klausen and Jakob Bæk Tejs Houen and Mikkel Thorup},
  title = {{Locally Uniform Hashing}},
  booktitle = {FOCS},
  year = {2023},
  url = {https://openreview.net/forum?id=fRvekzW0eS},
}
Figure