#hasher #hash-set #hash-map

hash_hasher

A hasher which is designed to work with already-hashed or hash-like data

7 stable releases

2.0.4 Jun 5, 2025
2.0.3 Feb 28, 2020
2.0.2 Dec 11, 2019
2.0.0 Apr 28, 2019
0.2.0 May 31, 2016

#56 in Data structures

Download history 82274/week @ 2025-10-20 80077/week @ 2025-10-27 66795/week @ 2025-11-03 64967/week @ 2025-11-10 90152/week @ 2025-11-17 50658/week @ 2025-11-24 69154/week @ 2025-12-01 62963/week @ 2025-12-08 71697/week @ 2025-12-15 36457/week @ 2025-12-22 28342/week @ 2025-12-29 82277/week @ 2026-01-05 89366/week @ 2026-01-12 72808/week @ 2026-01-19 75392/week @ 2026-01-26 92118/week @ 2026-02-02

336,426 downloads per month
Used in 219 crates (20 directly)

Apache-2.0 OR MIT

14KB
118 lines

hash_hasher

A std::hash::Hasher which is designed to work with already-hashed or hash-like data.

Documentation Crates.io Build Status

Details

The provided hasher does minimal work under the assumption that the input data is already suitable for use as a key in a HashSet or HashMap.

As well as the performance benefit, it also causes HashSets or HashMaps to become somewhat deterministic. Given two equal HashSets or HashMaps containing more than a single element, iterating them will likely yield the elements in differing orders. By using a hash_hasher::HashedSet or hash_hasher::HashedMap, then if the same data is inserted and/or removed in the same order, iterating the collection will yield a consistent order.

Example

Since new() and with_capacity() aren't available for HashSets or HashMaps using custom hashers, the available constructors are default(), with_hasher() and with_capacity_and_hasher().

use hash_hasher::{HashBuildHasher, HashedMap, HashedSet};

let mut map = HashedMap::default ();
assert!(map.insert(0, "zero").is_none());

let mut set = HashedSet::with_capacity_and_hasher(100, HashBuildHasher::default ());
assert!(set.insert(0));

Benchmarks

A benchmark suite is included and sample figures can be found in the CI results in the benchmarks-and-checks job under the Run benchmarks step.

For example:

test hash_sha1s_using_default_hasher                 ... bench:         191.12 ns/iter (+/- 17.41)
test hash_sha1s_using_fnv_hasher                     ... bench:         196.12 ns/iter (+/- 3.09)
test hash_sha1s_using_hash_hasher                    ... bench:           0.18 ns/iter (+/- 0.00)
test hash_sha256s_using_default_hasher               ... bench:         248.04 ns/iter (+/- 4.94)
test hash_sha256s_using_fnv_hasher                   ... bench:         369.20 ns/iter (+/- 3.10)
test hash_sha256s_using_hash_hasher                  ... bench:           0.18 ns/iter (+/- 0.00)
test hash_sha512s_using_default_hasher               ... bench:         263.13 ns/iter (+/- 0.30)
test hash_sha512s_using_fnv_hasher                   ... bench:       1,058.13 ns/iter (+/- 3.17)
test hash_sha512s_using_hash_hasher                  ... bench:           0.18 ns/iter (+/- 0.01)
test hash_sip_hashes_using_default_hasher            ... bench:           0.18 ns/iter (+/- 0.01)
test hash_sip_hashes_using_fnv_hasher                ... bench:           0.18 ns/iter (+/- 0.01)
test hash_sip_hashes_using_hash_hasher               ... bench:           0.18 ns/iter (+/- 0.01)
test insert_sha1s_into_fnv_set                       ... bench:         281.13 ns/iter (+/- 5.70)
test insert_sha1s_into_set_using_default_hasher      ... bench:         381.28 ns/iter (+/- 9.83)
test insert_sha1s_into_set_using_hash_hasher         ... bench:          97.50 ns/iter (+/- 0.69)
test insert_sha256s_into_set_using_default_hasher    ... bench:         407.91 ns/iter (+/- 20.46)
test insert_sha256s_into_set_using_fnv_hasher        ... bench:         823.43 ns/iter (+/- 41.30)
test insert_sha256s_into_set_using_hash_hasher       ... bench:          90.92 ns/iter (+/- 0.89)
test insert_sha512s_into_set_using_default_hasher    ... bench:         541.99 ns/iter (+/- 18.94)
test insert_sha512s_into_set_using_fnv_hasher        ... bench:       1,338.65 ns/iter (+/- 47.01)
test insert_sha512s_into_set_using_hash_hasher       ... bench:         190.27 ns/iter (+/- 1.28)
test insert_sip_hashes_into_set_using_default_hasher ... bench:         265.87 ns/iter (+/- 3.10)
test insert_sip_hashes_into_set_using_fnv_hasher     ... bench:         113.35 ns/iter (+/- 4.27)
test insert_sip_hashes_into_set_using_hash_hasher    ... bench:          55.40 ns/iter (+/- 0.16)

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

No runtime deps