Skip to content

Commit

Permalink
Speed up xor in intialization on AES path
Browse files Browse the repository at this point in the history
Signed-off-by: Tom Kaitchuck <[email protected]>
  • Loading branch information
tkaitchuck committed Jul 21, 2024
1 parent 3c2100e commit 67016c0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
7 changes: 4 additions & 3 deletions compare/src/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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::<String>::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)?;
Expand Down
8 changes: 4 additions & 4 deletions compare/tests/compare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use fxhash::FxBuildHasher;
use std::hash::{BuildHasher, BuildHasherDefault, Hash, Hasher};
use xxhash_rust::xxh3::Xxh3Builder;

fn ahash<K: Hash>(k: &K, builder: &RandomState) -> u64 {
let mut hasher = builder.build_hasher();
k.hash(&mut hasher);
hasher.finish()
fn ahash<K: Hash>(k: &K, builder: &RandomState<String>) -> u64 {
builder.hash_one(k)
// k.hash(&mut hasher);
// hasher.finish()
}

fn generic_hash<K: Hash, B: BuildHasher>(key: &K, builder: &B) -> u64 {
Expand Down
10 changes: 5 additions & 5 deletions src/aes_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}

Expand All @@ -65,7 +65,7 @@ impl AHasher {
Self {
enc: key1,
sum: key2,
key: key1 ^ key2,
key: xor(key1, key2),
}
}

Expand All @@ -76,7 +76,7 @@ impl AHasher {
Self {
enc: key1,
sum: key2,
key: key1 ^ key2,
key: xor(key1, key2),
}
}

Expand Down
16 changes: 16 additions & 0 deletions src/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))]
Expand Down

0 comments on commit 67016c0

Please sign in to comment.