Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Optimized hash lookups (#4144)
Browse files Browse the repository at this point in the history
* Optimize hash comparison

* Use libc
  • Loading branch information
arkpar authored and gavofyork committed Jan 13, 2017
1 parent e11353f commit cfb6dd2
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 14 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions util/bigint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ bigint = "1.0"
rustc-serialize = "0.3"
heapsize = "0.3"
rand = "0.3.12"
libc = "0.2"

[features]
x64asm_arithmetic=[]
Expand Down
20 changes: 6 additions & 14 deletions util/bigint/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use rand::Rng;
use rand::os::OsRng;
use rustc_serialize::hex::{FromHex, FromHexError};
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.
pub trait FixedHash: Sized {
Expand Down Expand Up @@ -214,25 +215,16 @@ macro_rules! impl_hash {

impl PartialEq for $from {
fn eq(&self, other: &Self) -> bool {
for i in 0..$size {
if self.0[i] != other.0[i] {
return false;
}
}
true
unsafe { memcmp(self.0.as_ptr() as *const c_void, other.0.as_ptr() as *const c_void, $size) == 0 }
}
}

impl Ord for $from {
fn cmp(&self, other: &Self) -> Ordering {
for i in 0..$size {
if self.0[i] > other.0[i] {
return Ordering::Greater;
} else if self.0[i] < other.0[i] {
return Ordering::Less;
}
}
Ordering::Equal
let r = unsafe { memcmp(self.0.as_ptr() as *const c_void, other.0.as_ptr() as *const c_void, $size) };
if r < 0 { return Ordering::Less }
if r > 0 { return Ordering::Greater }
return Ordering::Equal;
}
}

Expand Down
1 change: 1 addition & 0 deletions util/bigint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
extern crate rand;
extern crate rustc_serialize;
extern crate bigint;
extern crate libc;
#[macro_use] extern crate heapsize;

pub mod hash;
Expand Down

0 comments on commit cfb6dd2

Please sign in to comment.