From 243ab7c2f04e8ad0cba86cf96da6df06918a8888 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Mon, 8 Aug 2022 23:21:45 +0200 Subject: [PATCH] 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 --- src/engine/shared/snapshot.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/engine/shared/snapshot.cpp b/src/engine/shared/snapshot.cpp index 54d680033..8f4100314 100644 --- a/src/engine/shared/snapshot.cpp +++ b/src/engine/shared/snapshot.cpp @@ -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)