From bd10658648539a13553bd9ea8853f490ee424cc8 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sat, 25 Oct 2014 10:02:54 -0700 Subject: [PATCH] Use XXHash instead of SipHash --- phf/Cargo.toml | 6 +++--- phf_mac/Cargo.toml | 3 +++ shared/mod.rs | 16 +++++++++------- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/phf/Cargo.toml b/phf/Cargo.toml index f39f4428..f1a95813 100644 --- a/phf/Cargo.toml +++ b/phf/Cargo.toml @@ -1,15 +1,15 @@ [package] - name = "phf" authors = ["Steven Fackler "] 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" diff --git a/phf_mac/Cargo.toml b/phf_mac/Cargo.toml index 5c226259..c1cb4449 100644 --- a/phf_mac/Cargo.toml +++ b/phf_mac/Cargo.toml @@ -10,3 +10,6 @@ name = "phf_mac" path = "src/lib.rs" plugin = true test = false + +[dependencies.xxhash] +git = "https://github.com/Jurily/rust-xxhash" diff --git a/shared/mod.rs b/shared/mod.rs index 97734b6d..d73fbbf5 100644 --- a/shared/mod.rs +++ b/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; @@ -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)) } } )