From 67016c05bcf8d094b70a84cb63839270d6c5eec5 Mon Sep 17 00:00:00 2001 From: Tom Kaitchuck Date: Sun, 21 Jul 2024 15:11:05 -0700 Subject: [PATCH] Speed up xor in intialization on AES path Signed-off-by: Tom Kaitchuck --- compare/src/main.rs | 7 ++++--- compare/tests/compare.rs | 8 ++++---- src/aes_hash.rs | 10 +++++----- src/operations.rs | 16 ++++++++++++++++ 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/compare/src/main.rs b/compare/src/main.rs index b2a0b98..a75a90e 100644 --- a/compare/src/main.rs +++ b/compare/src/main.rs @@ -1,6 +1,7 @@ use std::io::Error; use std::fs::File; use std::io::Write; +use std::hash::BuildHasher; use pcg_mwc::Mwc256XXA64; use ahash::RandomState; use std::io::BufWriter; @@ -16,11 +17,11 @@ fn main() -> Result<(), Error> { let path = Path::new("hash_output"); let mut file = BufWriter::new(File::create(path)?); - let hasher = RandomState::with_seeds(r.gen(), r.gen(), r.gen(), r.gen()); + let hasher = RandomState::::with_seeds(r.gen(), r.gen(), r.gen(), r.gen()); let start = Instant::now(); let mut sum: u64 = 0; - for i in 0..i32::MAX { - let value = hasher.hash_one(i as u64); + for i in 0..5*1024*1024*1024_u64 { + let value = hasher.hash_one(i); sum = sum.wrapping_add(value); let value: [u8; 8] = value.to_ne_bytes(); file.write_all(&value)?; diff --git a/compare/tests/compare.rs b/compare/tests/compare.rs index 294a8d4..02f43df 100644 --- a/compare/tests/compare.rs +++ b/compare/tests/compare.rs @@ -6,10 +6,10 @@ use fxhash::FxBuildHasher; use std::hash::{BuildHasher, BuildHasherDefault, Hash, Hasher}; use xxhash_rust::xxh3::Xxh3Builder; -fn ahash(k: &K, builder: &RandomState) -> u64 { - let mut hasher = builder.build_hasher(); - k.hash(&mut hasher); - hasher.finish() +fn ahash(k: &K, builder: &RandomState) -> u64 { + builder.hash_one(k) + // k.hash(&mut hasher); + // hasher.finish() } fn generic_hash(key: &K, builder: &B) -> u64 { diff --git a/src/aes_hash.rs b/src/aes_hash.rs index 66cd3d1..cff95e7 100644 --- a/src/aes_hash.rs +++ b/src/aes_hash.rs @@ -51,12 +51,12 @@ impl AHasher { #[cfg(test)] pub(crate) fn new_with_keys(key1: u128, key2: u128) -> Self { let pi: [u128; 2] = PI.convert(); - let key1 = key1 ^ pi[0]; - let key2 = key2 ^ pi[1]; + let key1 = xor(key1, pi[0]); + let key2 = xor(key2, pi[1]); Self { enc: key1, sum: key2, - key: key1 ^ key2, + key: xor(key1, key2), } } @@ -65,7 +65,7 @@ impl AHasher { Self { enc: key1, sum: key2, - key: key1 ^ key2, + key: xor(key1, key2), } } @@ -76,7 +76,7 @@ impl AHasher { Self { enc: key1, sum: key2, - key: key1 ^ key2, + key: xor(key1, key2), } } diff --git a/src/operations.rs b/src/operations.rs index eed3a2a..9f46cbe 100644 --- a/src/operations.rs +++ b/src/operations.rs @@ -99,7 +99,23 @@ pub(crate) fn add_by_64s(a: [u64; 2], b: [u64; 2]) -> [u64; 2] { [a[0].wrapping_add(b[0]), a[1].wrapping_add(b[1])] } } +} +#[inline(always)] +pub(crate) fn xor(a: u128, b: u128) -> u128 { + cfg_if::cfg_if! { + if #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "sse2", not(miri)))] { + unsafe { + #[cfg(target_arch = "x86")] + use core::arch::x86::*; + #[cfg(target_arch = "x86_64")] + use core::arch::x86_64::*; + transmute!(_mm_xor_si128(transmute!(a), transmute!(b))) + } + } else { + a ^ b + } + } } #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes", not(miri)))]