#hasher #hash #rustc #fxhash

no-std rustc-hash

A speedy, non-cryptographic hashing algorithm used by rustc

4 stable releases

2.0.0 Jun 18, 2024
1.2.0 May 28, 2024
1.1.0 Feb 8, 2020
1.0.1 May 28, 2018
1.0.0 May 24, 2018

#1 in Programming languages

Download history 1461317/week @ 2024-07-27 1531898/week @ 2024-08-03 1578150/week @ 2024-08-10 1655139/week @ 2024-08-17 1712121/week @ 2024-08-24 1676558/week @ 2024-08-31 1787861/week @ 2024-09-07 1666936/week @ 2024-09-14 2017050/week @ 2024-09-21 2084201/week @ 2024-09-28 2633384/week @ 2024-10-05 2477192/week @ 2024-10-12 2546590/week @ 2024-10-19 2045010/week @ 2024-10-26 2185722/week @ 2024-11-02 1826668/week @ 2024-11-09

8,959,055 downloads per month
Used in 13,309 crates (822 directly)

Apache-2.0/MIT

25KB
372 lines

rustc-hash

crates.io Documentation

A speedy, non-cryptographic hashing algorithm used by rustc. The hash map in std uses SipHash by default, which provides resistance against DOS attacks. 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

This crate provides FxHashMap and FxHashSet as collections. They are simply type aliases for their std::collection counterparts using the Fx hasher.

use rustc_hash::FxHashMap;

let mut map: FxHashMap<u32, u32> = FxHashMap::default();
map.insert(22, 44);

no_std

The std feature is on by default to enable collections. It can be turned off in Cargo.toml like so:

rustc-hash = { version = "2.0", default-features = false }

Dependencies

~73KB