From 5c1e06ae749dbf5443180a0cc9b4748700424b40 Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Mon, 13 Mar 2023 02:37:19 +0300 Subject: [PATCH] public: crclib: optimize COM_HashKey, implement typical djb hashing as this function is used for hashtables with string lookup --- public/crclib.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/public/crclib.c b/public/crclib.c index 95013b58..0312f820 100644 --- a/public/crclib.c +++ b/public/crclib.c @@ -457,10 +457,11 @@ returns hash key for string */ uint COM_HashKey( const char *string, uint hashSize ) { - uint i, hashKey = 0; + int hashKey = 5381; + unsigned char i; - for( i = 0; string[i]; i++ ) - hashKey = (hashKey + i) * 37 + Q_tolower( string[i] ); + while(( i = *string++ )) + hashKey = ( hashKey << 5 ) + hashKey + ( i & 0xDF ); - return (hashKey % hashSize); + return hashKey & ( hashSize - 1 ); }