Story Details

  • Fibonacci Hashing: The Optimization That the World Forgot

    Posted: 2025-04-14 01:02:41

    Fibonacci hashing offers a faster alternative to the typical modulo operator (%) for distributing items into hash tables, especially when the table size is a power of two. It leverages the golden ratio's properties by multiplying the hash key by a large constant derived from the golden ratio and then bit-shifting the result, effectively achieving a modulo operation without the expensive division. This method produces a more even distribution compared to modulo with prime table sizes, particularly when dealing with keys exhibiting sequential patterns, thus reducing collisions and improving performance. While theoretically superior, its benefits may be negligible in modern systems due to compiler optimizations and branch prediction for modulo with powers of two.

    Summary of Comments ( 10 )
    https://news.ycombinator.com/item?id=43677122

    HN commenters generally praise the article for clearly explaining Fibonacci hashing and its benefits over modulo. Some point out that the technique is not forgotten, being used in game development and hash table implementations within popular languages like Java. A few commenters discuss the nuances of the golden ratio's properties and its suitability for hashing, with one noting the importance of good hash functions over minor speed differences in the hashing algorithm itself. Others shared alternative hashing methods like "Multiply-with-carry" and "SplitMix64", along with links to resources on hash table performance testing. A recurring theme is that Fibonacci hashing shines with power-of-two table sizes, losing its advantages (and potentially becoming worse) with prime table sizes.