tkaitchuck / aHash

aHash is a non-cryptographic hashing algorithm that uses the AES hardware instruction
https://crates.io/crates/ahash
Apache License 2.0
986 stars 94 forks source link

issue 158: Add SmallState #226

Closed tkaitchuck closed 3 months ago

tkaitchuck commented 4 months ago

Is an optional reduced size state. Fixes #158

arthurprs commented 3 months ago

I performed a quick benchmark with runtime-rng enabled (the most common feature I suppose) and the time to hash a 10 byte string goes from 2.5ns to 12.3ns, which puts it in siphash territory.

Do you think we could take some liberties with the creation of RandomState from SmallState, like

        let [a, _] = get_fixed_seeds();
        AHasher::from_random_state(&RandomState {
            k0: self.key as u64 ^ a[0],
            k1: self.key as u64 ^ a[1],
            k2: self.key as u64 ^ a[2],
            k3: self.key as u64 ^ a[3],
            _h: PhantomData::<T>,
        })

This brings it down from 12ns to 4ns

tkaitchuck commented 3 months ago

@arthurprs The code you have there won't be sufficiently strong. But I have tried to speed it up. Give it another try.

arthurprs commented 3 months ago

I got 7ns for 3e14ceb and 6.3ns for 1e94eac. For context, it's 2.5ns for the regular ahash and 10.3ns for sip13.

I think it's good now, unless you can squeeze more out of it :rocket:

tkaitchuck commented 3 months ago

I think I should be able to lower the added overhead by another 20%.

tkaitchuck commented 3 months ago

@arthurprs Is e2ae7b7 any faster on your system? It should be, but it my tests it appears to be, but only by about the same margin as random variation, so I am not sure if it is significant.

arthurprs commented 3 months ago

I can confirm the latest commit (~5ns) is 12% faster than the previous.