FxHash Explained: The Case for the Compiler’s Favourite Hash

FxHash explained in plain terms means understanding the hash that compiles Rust itself, a design from 2015 that turns any key into a bucket number with a rotate, an xor and a multiply. The central question is why a compiler would use a homespun hash when famous designs exist, and the answer is that a compiler hashes trusted keys millions of times a second, so FxHash gives rustc a hash that is fast enough to disappear, and that invisibility is the whole point of the design.

FxHash is a speedy hash algorithm used inside the Rust compiler, built on the same algorithm Firefox uses, and shipped as the rustc-hash crate. It turns a key into a 64-bit value by rotating the running state left by five, xoring in the next word and multiplying by a golden constant, and it is built on the idea that a hash should be as simple as its hottest loop, which means the design trades attack resistance for raw speed, and that trade is what makes it unlike the hashes of its era.

This article makes the case for FxHash, explains what it is, how it works, and where its ideas still earn their keep.

Key takeaways

  • FxHash is a fast hash from 2015, used inside rustc.
  • It shares its algorithm with Firefox’s hash.
  • It mixes with a rotate, an xor and a multiply.
  • It works on up to eight bytes at a time.
  • It is not DoS-resistant and never pretends to be.

What FxHash is

FxHash is a hash that turns a key into a 64-bit value with three cheap steps, and that smallness is what makes it unusual among hashes. Each word of input is rotated, xored and multiplied into the running state, which means the hash spreads bits with almost no work, and because the steps are so cheap, the hash runs faster than the table that uses it.

Its signature feature is its Firefox heritage. Most hashes are designed from scratch, but FxHash copies the homespun algorithm Firefox built for its own tables, which means the design was proven in a browser before it ever met a compiler, and because the algorithm was already battle-tested, rustc could adopt it with confidence.

The design was a reaction to the cost of the default. Rust’s standard HashMap used SipHash, which is safe against attackers but slow, so the compiler team wanted a hasher that was fast for trusted keys, because a compiler hashes identifiers on every lookup, and that practicality is what made FxHash worth extracting into its own crate.

It became one of the best-known fast hashes in the Rust world, and it is named in every discussion of the field. FxHash runs inside rustc, it ships as the rustc-hash crate for any Rust program, and it is still the first example engineers meet when they learn that a compiler can have its own hash, which means the design survives as a tool even though it is not the standard library default.

Its most famous property is its speed on integers. FxHash hashes small integers and short strings faster than almost any rival, which means it suits the keys compilers actually use, and because compilers hash identifiers all day, that fit is where the speed is won.

Our guide to SplitMix64 explains the one-word generator built on the same golden constant, and our guide to MurmurHash explains the earlier multiply-and-rotate hash it is measured against.

How FxHash works

Everything starts with zero, which is the initial hash value. FxHash sets its running state to nothing before reading any input, which means the empty input hashes to zero, and because the start is fixed, the same key always gives the same hash.

Each step begins with a word of input, which is mixed by the three-step core. FxHash takes the next word, rotates the state left by five bits, xors the word in and multiplies by the constant, which means fresh data arrives at every step, and because the multiply is by a golden-ratio constant, the mixing spreads across all 64 output bits.

The rotation is what makes the design work as a whole. Rotating the state by five before each xor moves the old bits out of the way, which means the new word lands on fresh bits, and because the rotation is fixed, the mixing costs a single instruction.

The constant spreads the effect further. The multiply uses 0x517cc1b727220a95 on 64-bit machines, which means the value is scrambled thoroughly, and because the constant is derived from the golden ratio, the products spread evenly instead of clumping.

The byte path is what turns short inputs into a hash. FxHash reads up to eight bytes at a time and feeds each chunk through the same core, which means small keys are hashed in one or two steps, and because the reads are wide, the hash never crawls byte by byte.

The result comes from the running state, read out as 64 bits. FxHash outputs the state directly as the hash, which means the result is a fixed size whatever the input, and that size is exactly what a hash table needs for its bucket index.

A dark office desk with a laptop showing a glowing green cipher diagram, a brass key and a notebook beside it
A dark office desk with a laptop showing a glowing green cipher diagram, a brass key and a notebook beside it, shows how FxHash works: words take turns being rotated, xored and multiplied, step after step, until a hash emerges.

Comparing FxHash with other table hashes

FxHash is one of several fast hashes built for tables, and each was built with a different emphasis. FNV-1a is the xor-and-multiply hash built for checksums, SipHash-1-3 is the attack-resistant hash Rust’s standard library still uses, and FxHash is the one built on a rotate and a golden constant for compilers, while each approach trades something for something else.

The table below sets out the main contrasts between the leading approaches.

Aspect FxHash FNV-1a SipHash-1-3
Output size 64 bits 32 or 64 bits 64 bits
Core step Rotate, xor, multiply Xor, multiply by prime SipHash rounds
DoS resistance None None Strong
Default in rustc Many codebases Rust std HashMap
Best for Compiler tables Checksums Adversarial input

The practical result is that FxHash is the right choice for compiler tables, FNV-1a suits checksums, and SipHash-1-3 fits adversarial input instead.

Where FxHash is used

The Rust compiler uses it as its internal hasher. rustc hashes identifiers, types and symbols with FxHash, because a compiler looks up names millions of times, and that adoption is the reason it runs in real builds rather than only in benchmarks.

Rust programs use it through the rustc-hash crate. Any program can swap its tables to FxHashMap and FxHashSet, because the crate is a drop-in replacement, and that ease is the reason FxHash earns its keep in Rust code far beyond the compiler.

Firefox uses the algorithm it came from. The browser’s own tables run the homespun hash FxHash was copied from, because a browser hashes strings constantly, and that shared history is the reason the two designs are still compared today.

Benchmarking uses it as a simplicity reference. Engineers still quote FxHash when they discuss how little work a hash needs, because it is the clearest example of three steps doing the job, and that number shapes how new hashes are measured.

Teaching uses it as the rotate-and-multiply example. Courses introduce FxHash when they explain that a rotate can mix, because it is the clearest case of the idea in a real design, and a student who understands FxHash understands why one rotate beats a chain of shifts.

Strengths and limits

The greatest strength of FxHash is its speed on compiler keys. A hash that mixes with three cheap steps gives tables faster lookups for free, and that strength is real, measured in the compiler that uses it and confirmed by the crate that ships it.

It is also a strength that the design is trivial to explain. A rotate, an xor and a multiply is a design anyone can describe, and that simplicity is worth more than cleverness in a field where a hash nobody can audit is a hash nobody trusts.

The limits are real, though, and the biggest is the DoS resistance. FxHash has none at all, which means it must never hash untrusted input in a network service, and a system that faces attackers must use SipHash or a randomised hasher instead.

FxHash checklist

  • Use FxHash for compiler tables, never for security or passwords.
  • Remember it outputs 64 bits from a rotate, xor and multiply.
  • Note the state starts at zero and has no seed.
  • Compare it with FNV-1a to see two kinds of simple hash.
  • Study the rotation, which is what moves the old bits aside.
  • Check the keys, since integers and short strings are its strength.
  • Keep real secrets behind cryptographic hashes.
  • Read the rustc-hash docs to see how the crate is used.
  • Choose SipHash when untrusted input must be hashed.

Frequently asked questions

What is FxHash in simple terms?

It is a 2015 fast hash that turns any key into a 64-bit value by rotating the state, xoring in each word and multiplying by a golden constant.

Who created FxHash?

The Rust project team, who copied the homespun algorithm Firefox used for its own hash tables.

How is FxHash different from FNV-1a?

Both are simple multiply hashes, but FxHash rotates the state before each xor while FNV-1a xors first and multiplies by a prime.

Is FxHash still secure?

It is fast and widely used, but it has no DoS resistance, so it must never guard secrets or hash untrusted input.

Where is FxHash used?

Inside rustc, in the rustc-hash crate, in Firefox’s tables, in benchmarking, and in teaching about rotate-based hashing.

When should I use FxHash?

When you need the fastest hash for trusted keys, when you write Rust, or when you study rotate-and-multiply design.

The bottom line

FxHash explained in one line is this: the compiler’s hash, a 2015 design of a rotate, an xor and a multiply that hashes Rust itself and taught the field that three steps can be enough.

It is proof that simple can be fast, and the reason it still matters is that every fast hash designed since has been judged partly by the lesson FxHash taught.

This article is general information about how FxHash works, not advice about choosing encryption for a particular device or system.

Latest articles

Scroll to Top