move plain hasher to a crate
This commit is contained in:
parent
697d17ae9b
commit
7de4a3a2df
10
Cargo.lock
generated
10
Cargo.lock
generated
@ -545,6 +545,7 @@ dependencies = [
|
||||
"bigint 3.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"heapsize 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"plain_hasher 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rustc-hex 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
@ -2192,6 +2193,14 @@ name = "pkg-config"
|
||||
version = "0.3.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "plain_hasher"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"crunchy 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "podio"
|
||||
version = "0.1.5"
|
||||
@ -3371,6 +3380,7 @@ dependencies = [
|
||||
"checksum phf_generator 0.7.14 (registry+https://github.com/rust-lang/crates.io-index)" = "db005608fd99800c8c74106a7c894cf582055b689aa14a79462cefdcb7dc1cc3"
|
||||
"checksum phf_shared 0.7.14 (registry+https://github.com/rust-lang/crates.io-index)" = "fee4d039930e4f45123c9b15976cf93a499847b6483dc09c42ea0ec4940f2aa6"
|
||||
"checksum pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "3a8b4c6b8165cd1a1cd4b9b120978131389f64bdaf456435caa41e630edba903"
|
||||
"checksum plain_hasher 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "83ae80873992f511142c07d0ec6c44de5636628fdb7e204abd655932ea79d995"
|
||||
"checksum podio 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e5422a1ee1bc57cc47ae717b0137314258138f38fd5f3cea083f43a9725383a0"
|
||||
"checksum pretty_assertions 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "2412f3332a07c7a2a50168988dcc184f32180a9758ad470390e5f55e089f6b6e"
|
||||
"checksum primal 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "0e31b86efadeaeb1235452171a66689682783149a6249ff334a2c5d8218d00a4"
|
||||
|
@ -13,6 +13,7 @@ rustc-hex = "1.0"
|
||||
rand = "0.3.12"
|
||||
libc = "0.2"
|
||||
heapsize = { version = "0.4", optional = true }
|
||||
plain_hasher = "0.1"
|
||||
|
||||
[features]
|
||||
x64asm_arithmetic=[]
|
||||
|
@ -16,6 +16,7 @@ use std::collections::{HashMap, HashSet};
|
||||
use rand::{Rand, Rng};
|
||||
use rand::os::OsRng;
|
||||
use rustc_hex::{FromHex, FromHexError};
|
||||
use plain_hasher::PlainHasher;
|
||||
use bigint::U256;
|
||||
use libc::{c_void, memcmp};
|
||||
|
||||
@ -446,52 +447,6 @@ impl_hash!(H2048, 256);
|
||||
known_heap_size!(0, H32, H64, H128, H160, H256, H264, H512, H520, H1024, H2048);
|
||||
// Specialized HashMap and HashSet
|
||||
|
||||
/// Hasher that just takes 8 bytes of the provided value.
|
||||
/// May only be used for keys which are 32 bytes.
|
||||
pub struct PlainHasher {
|
||||
prefix: u64,
|
||||
}
|
||||
|
||||
impl Default for PlainHasher {
|
||||
#[inline]
|
||||
fn default() -> PlainHasher {
|
||||
PlainHasher {
|
||||
prefix: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PlainHasher {
|
||||
#[inline]
|
||||
fn mut_prefix(&mut self) -> &mut [u8; 8] {
|
||||
unsafe { ::std::mem::transmute(&mut self.prefix) }
|
||||
}
|
||||
}
|
||||
|
||||
impl Hasher for PlainHasher {
|
||||
#[inline]
|
||||
fn finish(&self) -> u64 {
|
||||
self.prefix
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn write(&mut self, bytes: &[u8]) {
|
||||
debug_assert!(bytes.len() == 32);
|
||||
|
||||
unsafe {
|
||||
let mut bytes_ptr = bytes.as_ptr();
|
||||
let mut prefix_ptr = self.mut_prefix().as_mut_ptr();
|
||||
|
||||
for _ in 0..8 {
|
||||
*prefix_ptr ^= *bytes_ptr ^ *bytes_ptr.offset(8) ^ *bytes_ptr.offset(16) ^ *bytes_ptr.offset(24);
|
||||
|
||||
bytes_ptr = bytes_ptr.offset(1);
|
||||
prefix_ptr = prefix_ptr.offset(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Specialized version of `HashMap` with H256 keys and fast hashing function.
|
||||
pub type H256FastMap<T> = HashMap<H256, T, BuildHasherDefault<PlainHasher>>;
|
||||
/// Specialized version of `HashSet` with H256 keys and fast hashing function.
|
||||
|
@ -14,6 +14,7 @@ extern crate rand;
|
||||
extern crate rustc_hex;
|
||||
extern crate bigint;
|
||||
extern crate libc;
|
||||
extern crate plain_hasher;
|
||||
|
||||
#[cfg(feature="heapsizeof")]
|
||||
#[macro_use]
|
||||
|
Loading…
Reference in New Issue
Block a user