Optimized hash lookups (#4144)

* Optimize hash comparison

* Use libc
This commit is contained in:
Arkadiy Paronyan 2017-01-13 09:52:23 +01:00 committed by Gav Wood
parent e11353f94c
commit cfb6dd2ea8
4 changed files with 9 additions and 14 deletions

1
Cargo.lock generated
View File

@ -382,6 +382,7 @@ version = "0.1.2"
dependencies = [ dependencies = [
"bigint 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "bigint 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"heapsize 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "heapsize 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.16 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
] ]

View File

@ -12,6 +12,7 @@ bigint = "1.0"
rustc-serialize = "0.3" rustc-serialize = "0.3"
heapsize = "0.3" heapsize = "0.3"
rand = "0.3.12" rand = "0.3.12"
libc = "0.2"
[features] [features]
x64asm_arithmetic=[] x64asm_arithmetic=[]

View File

@ -26,6 +26,7 @@ use rand::Rng;
use rand::os::OsRng; use rand::os::OsRng;
use rustc_serialize::hex::{FromHex, FromHexError}; use rustc_serialize::hex::{FromHex, FromHexError};
use bigint::{Uint, U256}; use bigint::{Uint, U256};
use libc::{c_void, memcmp};
/// Trait for a fixed-size byte array to be used as the output of hash functions. /// Trait for a fixed-size byte array to be used as the output of hash functions.
pub trait FixedHash: Sized { pub trait FixedHash: Sized {
@ -214,25 +215,16 @@ macro_rules! impl_hash {
impl PartialEq for $from { impl PartialEq for $from {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
for i in 0..$size { unsafe { memcmp(self.0.as_ptr() as *const c_void, other.0.as_ptr() as *const c_void, $size) == 0 }
if self.0[i] != other.0[i] {
return false;
}
}
true
} }
} }
impl Ord for $from { impl Ord for $from {
fn cmp(&self, other: &Self) -> Ordering { fn cmp(&self, other: &Self) -> Ordering {
for i in 0..$size { let r = unsafe { memcmp(self.0.as_ptr() as *const c_void, other.0.as_ptr() as *const c_void, $size) };
if self.0[i] > other.0[i] { if r < 0 { return Ordering::Less }
return Ordering::Greater; if r > 0 { return Ordering::Greater }
} else if self.0[i] < other.0[i] { return Ordering::Equal;
return Ordering::Less;
}
}
Ordering::Equal
} }
} }

View File

@ -21,6 +21,7 @@
extern crate rand; extern crate rand;
extern crate rustc_serialize; extern crate rustc_serialize;
extern crate bigint; extern crate bigint;
extern crate libc;
#[macro_use] extern crate heapsize; #[macro_use] extern crate heapsize;
pub mod hash; pub mod hash;