Skip to content

Commit

Permalink
Use XXHash instead of SipHash
Browse files Browse the repository at this point in the history
  • Loading branch information
sfackler committed Oct 25, 2014
1 parent 996edc6 commit bd10658
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
6 changes: 3 additions & 3 deletions phf/Cargo.toml
@@ -1,15 +1,15 @@
[package]

name = "phf"
authors = ["Steven Fackler <sfackler@gmail.com>"]
version = "0.0.0"

[lib]

name = "phf"
path = "src/lib.rs"
test = false

[dev_dependencies.phf_mac]
[dependencies.xxhash]
git = "https://github.com/Jurily/rust-xxhash"

[dev_dependencies.phf_mac]
path = "../phf_mac"
3 changes: 3 additions & 0 deletions phf_mac/Cargo.toml
Expand Up @@ -10,3 +10,6 @@ name = "phf_mac"
path = "src/lib.rs"
plugin = true
test = false

[dependencies.xxhash]
git = "https://github.com/Jurily/rust-xxhash"
16 changes: 9 additions & 7 deletions shared/mod.rs
@@ -1,10 +1,11 @@
use std::hash::{Hash, Hasher, Writer};
use std::hash::sip::{SipHasher, SipState};
extern crate xxhash;

#[inline]
pub fn displace(f1: u32, f2: u32, d1: u32, d2: u32) -> u32 {
d2 + f1 * d1 + f2
}

#[inline]
fn split(hash: u64) -> (u32, u32, u32) {
let bits = 21;
let mask = (1 << bits) - 1;
Expand All @@ -21,24 +22,25 @@ pub trait PhfHash {
}

impl<'a> PhfHash for &'a str {
#[inline]
fn phf_hash(&self, seed: u64) -> (u32, u32, u32) {
split(SipHasher::new_with_keys(0, seed).hash(self))
split(xxhash::hash_with_seed(seed, self))
}
}

impl<'a> PhfHash for &'a [u8] {
#[inline]
fn phf_hash(&self, seed: u64) -> (u32, u32, u32) {
let mut state = SipState::new_with_keys(0, seed);
state.write(*self);
split(state.result())
split(xxhash::oneshot(*self, seed))
}
}

macro_rules! sip_impl(
($t:ty) => (
impl PhfHash for $t {
#[inline]
fn phf_hash(&self, seed: u64) -> (u32, u32, u32) {
split(SipHasher::new_with_keys(0, seed).hash(self))
split(xxhash::hash_with_seed(seed, self))
}
}
)
Expand Down

0 comments on commit bd10658

Please sign in to comment.