Skip to content

Commit

Permalink
Merge pull request #37 from orlp/faster-hash
Browse files Browse the repository at this point in the history
Replace hash with faster and better finalized hash
  • Loading branch information
Noratrieb authored Jun 2, 2024
2 parents 1c85035 + 0a95d9d commit 3e39d8e
Show file tree
Hide file tree
Showing 3 changed files with 270 additions and 105 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "rustc-hash"
version = "1.2.0"
authors = ["The Rust Project Developers"]
description = "A speedy, non-cryptographic hashing algorithm used by rustc and Firefox"
description = "A speedy, non-cryptographic hashing algorithm used by rustc"
license = "Apache-2.0/MIT"
readme = "README.md"
keywords = ["hash", "hasher", "fxhash", "rustc"]
Expand All @@ -12,6 +12,7 @@ edition = "2021"
[features]
default = ["std"]
std = []
nightly = []
rand = ["dep:rand", "std"]

[dependencies]
Expand Down
21 changes: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@
[![crates.io](https://img.shields.io/crates/v/rustc-hash.svg)](https://crates.io/crates/rustc-hash)
[![Documentation](https://docs.rs/rustc-hash/badge.svg)](https://docs.rs/rustc-hash)

A speedy, non-cryptographic hashing algorithm used by `rustc` and Firefox.
A speedy, non-cryptographic hashing algorithm used by `rustc`.
The [hash map in `std`](https://doc.rust-lang.org/std/collections/struct.HashMap.html) uses SipHash by default, which provides resistance against DOS attacks.
These attacks aren't as much of a concern in the compiler so we prefer to use the quicker, non-cryptographic Fx algorithm.

The Fx algorithm is a unique one used by Firefox. It is fast because it can hash eight bytes at a time.
Within `rustc`, it consistently outperforms every other tested algorithm (such as FNV).
The collision rate is similar or slightly worse than other low-quality hash functions.
These attacks aren't a concern in the compiler so we prefer to use a quicker,
non-cryptographic hash algorithm.

The original hash algorithm provided by this crate was one taken from Firefox,
hence the hasher it provides is called FxHasher. This name is kept for backwards
compatibility, but the underlying hash has since been replaced. The current
design for the hasher is a polynomial hash finished with a single bit rotation,
together with a wyhash-inspired compression function for strings/slices, both
designed by Orson Peters.

For `rustc` we have tried many different hashing algorithms. Hashing speed is
critical, especially for single integers. Spending more CPU cycles on a higher
quality hash does not reduce hash collisions enough to make the compiler faster
on real-world benchmarks.

## Usage

Expand Down
Loading

0 comments on commit 3e39d8e

Please sign in to comment.