#hasher #hash #rustc #fxhash

no-std rustc-hash

A speedy, non-cryptographic hashing algorithm used by rustc

5 stable releases

2.1.0 Nov 30, 2024
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 in Programming languages

Download history 1694882/week @ 2024-08-21 1627132/week @ 2024-08-28 1817249/week @ 2024-09-04 1759463/week @ 2024-09-11 1892151/week @ 2024-09-18 2045633/week @ 2024-09-25 2482859/week @ 2024-10-02 2531962/week @ 2024-10-09 2562497/week @ 2024-10-16 2090068/week @ 2024-10-23 2084190/week @ 2024-10-30 2136997/week @ 2024-11-06 2276434/week @ 2024-11-13 2252606/week @ 2024-11-20 1953591/week @ 2024-11-27 1900685/week @ 2024-12-04

8,778,065 downloads per month
Used in 13,736 crates (849 directly)

Apache-2.0 OR MIT

26KB
396 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.1", default-features = false }

Dependencies

~75KB