Skip to content

kumose/xhash

Repository files navigation

xhash

中文版

Fast non-cryptographic hash functions for C++ hash tables, Bloom filters, etc.

Hash algorithms

Algorithm Output Streaming Type
FNV-1a 32/64 bit lightweight
MurmurHash3 32/64/128 bit classic
XXH32 / XXH64 32/64 bit speed
wyhash64 64 bit modern fast
CityHash 32/64 bit Google
SpookyHash V2 64/128 bit Bob Jenkins
t1ha 64 bit Yandex
SipHash-2-4 64 bit keyed, HashDoS resistant
XXH3 (official) 64/128 bit wraps official xxhash

Hash framework: xhash::Hash<T>

Type-aware hashing similar to turbo::Hash. Automatically selects the fastest path for each type:

std::unordered_map<std::string, int, xhash::Hash<std::string>> map;
xhash::hash_of(1, 2, 3);           // hash_of convenience
xhash::Hash<MyType>{}(obj);        // ADL extension point

Fast paths:

  • int/enum/pointer — inline avalanche mix (0.4ns)
  • string/string_view — XXH3 direct
  • pair/tuple — recursive element expansion
  • container — element iteration with per-type dispatch
  • custom types — ADL xhash_hash_value() extension

Build

cmake --preset=default
cmake --build build -j$(nproc)
ctest --test-dir build

Benchmark (Apple Silicon M1)

Algorithm 8B 64B 512B 4096B
xxh64 (xhash) 1.43ns 5.23ns 32.2ns 258.6ns
XXH64 (official) 2.35ns 6.22ns 32.9ns 262.2ns
wyhash64 8.66ns 6.80ns 17.6ns 144.7ns
cityhash64 1.48ns 2.73ns 30.1ns 238.0ns
t1ha 3.53ns 5.38ns 29.7ns 280.4ns
spookyhash64 4.38ns 12.9ns 44.3ns 298.8ns
murmur3_64 7.29ns 11.1ns 79.9ns 683.8ns
fnv1a_64 4.20ns 41.0ns 543.5ns 4.6μs
siphash_2_4 10.4ns 35.7ns 275.8ns 2.3μs
XXH3 (official) 1.77ns 4.32ns 15.6ns 103.4ns

License

Licensed under the Apache License, Version 2.0. Individual hash algorithms carry their own original licenses (BSD, MIT, Public Domain, etc.).

Releases

Packages

Contributors

Languages