275 points · by corysama · 7y ago · 83 comments
Most hash functions and benchmarks are not tuned for small inputs. This is why SipHash is disfavored in Rust these days, because while its performance is excellent on large keys it suffers quite a bit on small ones.
> The main focus is about short keys of random lengths, with a distribution of length roughly in the 20-30 bytes area, featuring occasional outliers, both tiny and large.
I have been playing around trying to write interpreters and compilers for a hobby, and though I haven't taken stats, but I suspect that in source code, identifiers half that length are more than twice as common.
It make me wonder if compilers are better of using old-school byte-by-byte hashes rather than new ones optmised for parallelism. (Then again, the hash function probably makes no real difference to speed once in a real-world compiler that does optimisation).
Probably not.
Good hashes optimized for small sizes would probably still use wider loads and operations, perhaps overlapping them: e.g., for a 7 byte hash you might do two 4-byte hashes on the first and second half meaning one byte would get done twice, which isn't a problem. High speed small memory copies follow the same approach.
One problem with byte-at-a-time hashes are that byte operations aren't really any faster than wider operations (at least up to 64-bits), so you'll do 7 byte mixing operations in the example above rather than 2.
The other problem is that wider chunks tend to "quantize" the number of loops in the algorithm, which is very helpful for branch prediction. A byte-at-a-time hash that uses a loop or some kind of jump into inline code will necessarily mispredict unless the exact length is predictable: but if it varies by even 1 byte, the branching is different. Hashes with larger chunks may take the same path for a range of lengths, so may have fewer mispredictions even if the size varies. For example, a hash that uses two possibly overlapping 4-byte reads may handle sizes from 5-8 (or even 4-8) bytes with the same path, so random variation within that range doesn't cause a mispredict.
I think the era of the byte-at-a-time hash is really over, except perhaps if gets some special hardware support.
> > The main focus is about short keys of random lengths, with a distribution of length roughly in the 20-30 bytes area, featuring occasional outliers, both tiny and large.
Based on the graphs, XXH3 still seems to beat everything at key sizes > 6 (presumably bytes?)
I wasn't aware of this!
What do people use instead?
(I remember reading that part of the motivation for making SipHash the default Hasher was that its randomness guards against hash flooding attacks. Is that something people care about in practice?)
That's controversial the way climate change is controversial.
I find it to be the fastest hash in my stable of hashes.
Graphs for 64 / 128:
https://github.com/injinj/smhasher/blob/master/plot64.svg
https://github.com/injinj/smhasher/blob/master/plot128.svg
Latency graphs for 64 / 128
https://github.com/injinj/smhasher/blob/master/plot64lat.svg
https://github.com/injinj/smhasher/blob/master/plot128lat.sv...
Latency is using the hash result after each function, the original smhasher algo does not use the result.
It would be interesting to also have a test where an average size was chosen, but where the actual sizes varied randomly around that average - because that's a common case in real world code, and the results can be very different since branch prediction won't be perfect any more as it is with the fixed size tests.
Also, latency usually means feeding the output of the function back into the input of the next call. Throughput is what you get when you don't do that but you still need to consume the result, so it's not optimized away. You can use something like DoNotOptimize here:
https://github.com/google/benchmark/blob/master/include/benc...
The graphs from the "latency" test are doing this:
for (;;) {
h = hash( val, seed );
seed += h;
}
The original smhasher did this: for (;;) {
hash( val, seed );
}Several hashes only use the seed at the very end of calculation. Among them, CityHash, FarmHash, MeowHash, probably a few more. This makes it possible for them to start calculating next hash before the end of the previous one, so it's no longer "latency" for them, and the test condition becomes uneven.
In reality, in a latency scenario, the hash function is waiting for the input. So it's the input which must depends on previous hash result. This way, all algorithms must wait for previous result, no more dependency on how a specific algorithm handle the seed.
total = 0;
for (;;) {
total += hash( val, seed );
}
Note that the hash value is consumed, but doesn't feed into the next result so multiple hashes can run in parallel. If that wasn't the case, it is lucky the loop wasn't compiled away entirely (probably because the hash functions and the test loop are in different compilation units and LTO is not turned on).The core of the function is as follows:
static U64 XXH64_round(U64 acc, U64 input)
{
acc += input * PRIME64_2;
acc = XXH_rotl64(acc, 31);
acc *= PRIME64_1;
return acc;
}
Multiplication, rotation, and a 2nd multiplication. These are 64-bit multiplications, which is well known to mix bits well, but CPUs don't have many 64-bit multipliers. Four rounds execute in parallel (see XXH64_update_endian), which can hide the long latency (~5 cycles) associated with a 64-bit multiply.My own experiments show that multiplication is a great function for hashing. Multiplication however only spreads the bits in "one direction" (towards the most-significant bit), and very poorly spreads bits towards the LSB.
Rotations, as well as a 2nd multiplication, are helpful at spreading the bits around better.
----------
The 64-bit version is somewhat interesting to me, but the 32-bit version probably has more potential for optimization. 32-bit vectorized multiplication exists in AVX2, so the 32-bit version probably can be vectorized. I'd expect the 32-bit version (if vectorized) would be faster, but the 64-bit version probably mixes the bits around better.
Overall looks like a decent design. Uses ILP with 4x state variables (state->v1, state->v2, etc. etc. to allow CPUs to process these multiplications in parallel). Multiply + rotation + multiplication is good from my personal experiments, but the numbers chosen need to be carefully chosen.
Prime numbers are probably not necessary: they just need to be odd numbers to ensure invertibility (Ex: I've achieved decent mixing by multiplying with 0xAAAAAAAA5, which probably isn't a prime number). I'm curious how the prime-constants were selected. There could be some potential for "better numbers" for mixing, depending on how PRIME64_1 and PRIME64_2 were chosen.
Overall checks all of my boxes with regards to good design. Nice! I'm just curious about some details.
----------
I'll note that in my experiments, multiplication seems to "mix" better with XOR, rather than with ADD. I haven't done any tests with this hash yet, but I'm curious how changing the "+=" into "^=" would have on the statistical strength of the hash function.
assert(((size_t)acc) & 31 == 0);
{ ALIGN(32) __m256i* const xacc = (__m256i *) acc;
const __m256i* const xdata = (const __m256i *) data;
const __m256i* const xkey = (const __m256i *) key;
size_t i;
for (i=0; i < STRIPE_LEN/sizeof(__m256i); i++) {
__m256i const d = _mm256_loadu_si256 (xdata+i);
__m256i const k = _mm256_loadu_si256 (xkey+i);
__m256i const dk = _mm256_add_epi32 (d,k); /* uint32 dk[8] = {d0+k0, d1+k1, d2+k2, d3+k3, ...} */
__m256i const res = _mm256_mul_epu32 (dk, _mm256_shuffle_epi32 (dk, 0x31)); /* uint64 res[4] = {dk0*dk1, dk2*dk3, ...} */
__m256i const add = _mm256_add_epi64(d, xacc[i]);
xacc[i] = _mm256_add_epi64(res, add);
}
}
This is followed up by "ScrambleAcc": assert(((size_t)acc) & 31 == 0);
{ ALIGN(32) __m256i* const xacc = (__m256i*) acc;
const __m256i* const xkey = (const __m256i *) key;
size_t i;
for (i=0; i < STRIPE_LEN/sizeof(__m256i); i++) {
__m256i data = xacc[i];
__m256i const shifted = _mm256_srli_epi64(data, 47);
data = _mm256_xor_si256(data, shifted);
{ __m256i const k = _mm256_loadu_si256 (xkey+i);
__m256i const dk = _mm256_mul_epu32 (data,k); /* U32 dk[4] = {d0+k0, d1+k1, d2+k2, d3+k3} */
__m256i const d2 = _mm256_shuffle_epi32 (data,0x31);
__m256i const k2 = _mm256_shuffle_epi32 (k,0x31);
__m256i const dk2 = _mm256_mul_epu32 (d2,k2); /* U32 dk[4] = {d0+k0, d1+k1, d2+k2, d3+k3} */
xacc[i] = _mm256_xor_si256(dk, dk2);
} }
}
I don't believe AVX2 rotation exists (correct me if I'm wrong though). So the author has opted for _mm256_srli_epi64 (... 47), which is a 47-bit right shift, followed up with XOR. This is how the author gets those "highly mixed" high-bits back down to the lower-bits.Vectorization has broadened to 512-bits of state (4x128) so that the vectorized steps can be ILP'd.
The keys are:
ALIGN(64) static const U32 kKey[KEYSET_DEFAULT_SIZE] = {
0xb8fe6c39,0x23a44bbe,0x7c01812c,0xf721ad1c,
0xded46de9,0x839097db,0x7240a4a4,0xb7b3671f,
0xcb79e64e,0xccc0e578,0x825ad07d,0xccff7221,
0xb8084674,0xf743248e,0xe03590e6,0x813a264c,
0x3c2852bb,0x91c300cb,0x88d0658b,0x1b532ea3,
0x71644897,0xa20df94e,0x3819ef46,0xa9deacd8,
0xa8fa763f,0xe39c343f,0xf9dcbbc7,0xc70b4f1d,
0x8a51e04b,0xcdb45931,0xc89f7ec9,0xd9787364,
0xeac5ac83,0x34d3ebc3,0xc581a0ff,0xfa1363eb,
0x170ddd51,0xb7f0da49,0xd3165526,0x29d4689e,
0x2b16be58,0x7d47a1fc,0x8ff8b8d1,0x7ad031ce,
0x45cb3a8f,0x95160428,0xafd7fbca,0xbb4b407e,
};
Hmmm... this key is added with the data as it gets mixed in.---------
Definitely seems like cleaner code actually. Again, I'm not seeing any obvious red-flags in the code, so it looks pretty good to me.
The only thing is that the 32-bit to 64-bit multiply step seems odd to me. It probably isn't losing any information, but I'm kind of mind-farting and can't see whether or not that step is potentially losing entropy or not... I'll probably have to sleep on it.
Specifically this step:
__m256i const res = _mm256_mul_epu32 (dk, _mm256_shuffle_epi32 (dk, 0x31)); /* uint64 res[4] = {dk0*dk1, dk2*dk3, ...} */
Those numbers aren't necessarily odd. Its a 32-bit multiply that takes two 32-bit numbers, and outputs a 64-bit number (vectorized). It doesn't seem like a "strong" hash, but maybe I need to think about it more. If this is a good hash (and with all the tests done on it... it probably is a good hash), then I'd be surprised.It reminds me of a RNG that Knuth discussed in his book: where you multiply two numbers and then take the "middle" results of the multiplication (https://en.wikipedia.org/wiki/Middle-square_method). So I feel like there's potential for weakness here. I can't see it one way or the other yet, its just something I'm trying to think about...
That's enough for XXH3. Since it maintains a 512-bit internal state, that means it transports 256-bit of entropy.
factor $(echo -e "ibase=16\nAAAAAAAA5" | bc -l)
claims.Lemme muse on the concept: "multiplying by 0xAAAAAAA5" is that the binary is 1010,1010,1010...0101.
A multiplication at its core, is equivalent to a LOT of bitshift-left and add. Multiplying by "1000" is the same as bitshift left by 4. Multiplying by "1010" is bitshift left by 4 + bitshift left by 2.
This is why its important to multiply by an odd number: all those bitshifts lose the top-most bit. You only keep the topmost bit if the bottom-most bit is set.
So multiply by 0xAAAAAAA5 is the same as "bitshift left 31 + bitshift left 29 + bitshift left 27 ... + bitshift left 2 + bitshift left 0", all at once.
Addition itself is a great "mixing" instruction, its an XOR innately with the potential for carries. The carries make the XORs far more complicated to analyze. The 31st bit after all of those bitshifts are not only the XOR of a bunch of bits... but also the "propagated carry bit".
The topmost bit of any multiplication operation is very complex, while the bottom most bit is always the original bit (bit0 is only affected by bit0 from the input). That's why a rotation + 2nd multiply is needed.
Any reason why use use 0xAAAAAAA5 instead of 0xAAAAAAAA or 0xA5A5A5A5?
https://github.com/Cyan4973/xxHash/blob/dev/xxh3.h
Is there a description of the algorithm, the C-source is not only highly optimised, it is also highly #ifdefed to take care of various scenarios. So the file is not optimised for readability.
SSE2: CFLAGS=-O3
AVX2: CFLAGS="-O3 -mavx2"
ARCH: CFLAGS="-O3 -march=native"
Compiler Mode Speed
gcc-8 SSE2 32.8 GB/s
clang-7 SSE2 36.5 GB/s
gcc-8 AVX2 44.1 GB/s
clang-7 AVX2 68.3 GB/s
gcc-8 ARCH 60.9 GB/s
clang-7 ARCH 69.7 GB/s
gcc nearly catches up with clang when compiled with -march=native, but with only -mavx2 it isn't performing as well.Trying -march=haswell might give closer results.
On the other hand, if using an older gcc and it can’t identify your processor, march=native will result in mtune=generic and much head scratching with results.
Meow is faster than xxh3 for all sizes, small or large.
I added Meow to my test graphs: https://github.com/injinj/smhasher
I still find xxh3 to slightly faster for small hashes on my machine (i9-7960X), except at the 128bit throughput size > 32b.
[[[ Keyset 'TestLongNeighbors' Tests ]]]
Looking for 2-bit collisions in the last 2400 bits.
Looking for 4-bit collisions in the last 2048 bits.
Looking for 6-bit collisions in the last 160 bits.
Trying bases of length 10 to 300, 5 of each length.
......[16]...............[32]............... [48]...............[64].
Among 285 tested bases, we expected 4.25749e-06 bads.
Actually there were 5.
The most striking collision has surprise score 3.52004e+06 and length 46 bytes:
000: BD 8D 12 8C FB EE 75 2E 17 80 4E 26 8C E7 75 94 DA EF 12 8C 95 CA 91 0D
018: 08 BD 8D 5F F2 78 FF 64 88 27 88 3F 8E 8F CE FC E2 8A 99 CE 2B E3
^D2^ ^7F^ ^18^ ^0B^
The hashes are length 8 bytes:
000: 35 C1 E6 5B 6B 66 C7 28
*********FAIL*********The original Meow algo:
while(Len >= 64)
{
S0 = Meow128_AESDEC_Mem(S0, Source);
S1 = Meow128_AESDEC_Mem(S1, Source + 16);
S2 = Meow128_AESDEC_Mem(S2, Source + 32);
S3 = Meow128_AESDEC_Mem(S3, Source + 48);
Len -= 64;
Source += 64;
}
The modified algo: while(Len >= 64)
{
S0 = Meow128_AESDEC_Mem(S0, Source);
S0 = Meow128_AESDEC_Mem(S0, Source);
S1 = Meow128_AESDEC_Mem(S1, Source + 16);
S1 = Meow128_AESDEC_Mem(S1, Source + 16);
S2 = Meow128_AESDEC_Mem(S2, Source + 32);
S2 = Meow128_AESDEC_Mem(S2, Source + 32);
S3 = Meow128_AESDEC_Mem(S3, Source + 48);
S3 = Meow128_AESDEC_Mem(S3, Source + 48);
Len -= 64;
Source += 64;
}
(also changed the partial rounds, but this is not shown here)This doesn't change it's performance that much, maybe 1 or 2 extra CPU cycles. My i9 CPU must be able to run these extra rounds in parallel... I do have hyperthreading disabled, if that matters.
Update:
Ah, it does cause the bulk speed test to be 50% of the original (48MiB vs 24Mib). The small key test only adds 1/2/3/4 extra AES instructions since they are manipulating 16 bytes at a time. It looks like this is only taking a cycle.
SIMD CRC32 implementations can be more than 14x faster than recent zlib's.
I dropped some details as comments on the original blog post.
Traditionally the fastest CRC code has used lookup tables, I wonder if that's creating cache pressure these days? Or maybe that approach was abandoned while I wasn't paying attention.
Instruction-level parallelism is incredibly important. I'd say that any optimizing programmer needs to fully understand ILP, and how it interacts with pipelines and dependency cutting (and register renaming).
Modern CPUs are extremely well parallelized with ILP. Any good, modern hash function will take advantage of this feature of modern CPUs.
Case in point, it seems like xxhash is SCALAR 32-bit / 64-bit code. No vectorization as far as I can tell, its purely using ILP to get its speed.
Intel Assembly has a 64-bit multiplier (but vectorized only has 32-bit multipliers). I've theorized to myself that this 64-bit multiplier could lead to better mixing than the vectorized instructions, and it seems like xxhash goes for that.
The 32-bit version of xxhash can likely be vectorized and optimized even further.
If you're the slightest bit security conscious you realize that there is NO level below which security is not a factor. If that is the deciding factor these compromises (or as they would tell it, lack of compromise) will decrease overall UTILITY of technology and also increase its FRAGILITY.
The move to aggressively force HTTP to HTTPS even for Joe's Who Cares Web Site for no specifically stated reason is a prime example. If rounds and loops were temperature, it is as if we're shoving the Internet into thermal overload beyond convection cooling, without performing any additional useful work.
Meltdown mitigation is like economic inflation. A general performance decrease for a worst case threat scenario.
What price, paranoia? Is there an upper limit?
Can someone benchmark it for me? Because sha with higher values might be faster.
Anyone have a TL;DR regarding how these two compare?
wyhash still is the recommended hash function: portable and lowest cycles/hash.
However xxxh3 is spectacularly fast, for small and esp. large key sizes. This is now the recommended hash for file or db digests.
String hash table stats not yet done, there FNV1A is still the recommendation, with safe collision handling.
TLDR: XXH3 uses less cycles per key, by about 30%. The real throughput / latency need a more equal and thorough benchmark of both, though.