From 97a534fcda87a937bc21fce8d5dd5f4aad800483 Mon Sep 17 00:00:00 2001 From: arkpar Date: Thu, 12 Jan 2017 12:58:22 +0100 Subject: [PATCH] Optimize hash comparison --- util/bigint/src/hash.rs | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/util/bigint/src/hash.rs b/util/bigint/src/hash.rs index fb9fbbd7271..d32eded5623 100644 --- a/util/bigint/src/hash.rs +++ b/util/bigint/src/hash.rs @@ -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)] @@ -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; } }