mirror of
https://github.com/ddnet/ddnet.git
synced 2024-11-10 10:08:18 +00:00
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:
parent
573aedc323
commit
243ab7c2f0
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue