• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# rustc-hash
2
3[![crates.io](https://img.shields.io/crates/v/rustc-hash.svg)](https://crates.io/crates/rustc-hash)
4[![Documentation](https://docs.rs/rustc-hash/badge.svg)](https://docs.rs/rustc-hash)
5
6A speedy, non-cryptographic hashing algorithm used by `rustc`.
7The [hash map in `std`](https://doc.rust-lang.org/std/collections/struct.HashMap.html) uses SipHash by default, which provides resistance against DOS attacks.
8These attacks aren't a concern in the compiler so we prefer to use a quicker,
9non-cryptographic hash algorithm.
10
11The original hash algorithm provided by this crate was one taken from Firefox,
12hence the hasher it provides is called FxHasher. This name is kept for backwards
13compatibility, but the underlying hash has since been replaced. The current
14design for the hasher is a polynomial hash finished with a single bit rotation,
15together with a wyhash-inspired compression function for strings/slices, both
16designed by Orson Peters.
17
18For `rustc` we have tried many different hashing algorithms. Hashing speed is
19critical, especially for single integers. Spending more CPU cycles on a higher
20quality hash does not reduce hash collisions enough to make the compiler faster
21on real-world benchmarks.
22
23## Usage
24
25This crate provides `FxHashMap` and `FxHashSet` as collections.
26They are simply type aliases for their `std::collection` counterparts using the Fx hasher.
27
28```rust
29use rustc_hash::FxHashMap;
30
31let mut map: FxHashMap<u32, u32> = FxHashMap::default();
32map.insert(22, 44);
33```
34
35### `no_std`
36
37The `std` feature is on by default to enable collections.
38It can be turned off in `Cargo.toml` like so:
39
40```toml
41rustc-hash = { version = "2.1", default-features = false }
42```
43