Use djb2 for snapshot item hashlist

The previous hash function was heavily biased towards the hash buckets 64-79, making those buckets overflow faster, which results in snapshot CRC errors and lags.

Using the djb2 hash yields an almost even distribution over the entire range of values.

http://www.cse.yorku.ca/~oz/hash.html
This commit is contained in:
Robert Müller 2022-08-08 23:21:45 +02:00
parent 573aedc323
commit 243ab7c2f0

View file

@ -159,7 +159,11 @@ struct CItemList
inline size_t CalcHashID(int Key)
{
return ((Key >> 12) & 0xf0) | (Key & 0xf);
// djb2 (http://www.cse.yorku.ca/~oz/hash.html)
unsigned Hash = 5381;
for(unsigned Shift = 0; Shift < sizeof(int); Shift++)
Hash = ((Hash << 5) + Hash) + ((Key >> (Shift * 8)) & 0xFF);
return Hash % HASHLIST_SIZE;
}
static void GenerateHash(CItemList *pHashlist, CSnapshot *pSnapshot)