Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace hash with faster and better finalized hash #37

Merged
merged 7 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading