#hash #hashing

fasthash

A suite of non-cryptographic hash functions for Rust

14 releases

0.3.2 Aug 6, 2018
0.3.1 Jul 28, 2018
0.2.8 Jan 22, 2018
0.2.6 May 10, 2017
0.1.1 Dec 25, 2016

#92 in Cryptography

Download history 6/week @ 2018-05-14 18/week @ 2018-05-21 38/week @ 2018-05-28 36/week @ 2018-06-04 10/week @ 2018-06-11 49/week @ 2018-06-18 32/week @ 2018-06-25 73/week @ 2018-07-02 19/week @ 2018-07-09 26/week @ 2018-07-16 18/week @ 2018-07-23 65/week @ 2018-07-30 60/week @ 2018-08-06

150 downloads per month
Used in 1 crate

Apache-2.0

1MB
29K SLoC

C++ 17K SLoC // 0.1% comments C 8K SLoC // 0.2% comments Rust 4K SLoC // 0.2% comments Assembly 306 SLoC // 0.3% comments Shell 31 SLoC // 0.2% comments Batch 17 SLoC Perl 10 SLoC

rust-fasthash travis build crate docs

A suite of non-cryptographic hash functions for Rust, base on a forked smhasher.

Usage

To use fasthash, first add this to your Cargo.toml:

[dependencies]
fasthash = "0.2"

When use fasthash 128bit supports in Rust 1.15 (beta) or earlier version, we need enable i128 feature.

[dependencies.fasthash]
version = "0.2"
features = ["i128", "sse42"]

Then, add this to your crate root

extern crate fasthash;

use fasthash::*;

And then, use hash function with module or hasher

let h = city::hash64("hello world");

Or work with std::hash::Hash

use std::hash::Hash;

use fasthash::MetroHasher;

fn hash<T: Hash>(t: &T) -> u64 {
    let mut s = MetroHasher::new();
    t.hash(&mut s);
    s.finish()
}

hash(&"hello world");

It also works with HashMap or HashSet, act as the hash function

use std::collections::HashSet;

use fasthash::spooky::SpookyHash128;

let mut set = HashSet::with_hasher(SpookyHash128 {});
set.insert(2);

Or use RandomState with a random seed.

use std::collections::HashMap;

use fasthash::RandomState;
use fasthash::city::CityHash64;

let s = RandomState::<CityHash64>::new();
let mut map = HashMap::with_hasher(s);

assert_eq!(map.insert(37, "a"), None);
assert_eq!(map.is_empty(), false);

map.insert(37, "b");
assert_eq!(map.insert(37, "c"), Some("b"));
assert_eq!(map[&37], "c");

Goal

  • High performance
  • Zero cost binding
  • Compatibility with libstd/libcollection

Features

Performance

To bench the hash function, we need nighly rust

$ rustup run nightly cargo bench

Please check smhasher reports for more details.

Dependencies

~1MB
~10K SLoC

  • build build.rs
  • dev fnv 1.0
  • dev rand 0.5