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