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

Commit

Permalink
Optimize hash comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
arkpar committed Jan 12, 2017
1 parent 6f3cefe commit 97a534f
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions util/bigint/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ pub fn clean_0x(s: &str) -> &str {
}
}

extern { fn memcmp(s1: *const i8, s2: *const i8, n: usize) -> i32; }

macro_rules! impl_hash {
($from: ident, $size: expr) => {
#[repr(C)]
Expand Down Expand Up @@ -214,25 +216,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 i8, other.0.as_ptr() as *const i8, $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 i8, other.0.as_ptr() as *const i8, $size) };
if r < 0 { return Ordering::Less }
if r > 0 { return Ordering::Greater }
return Ordering::Equal;
}
}

Expand Down

0 comments on commit 97a534f

Please sign in to comment.