From c84724da37c5700c8d83c46304e0bc22ce316f1c Mon Sep 17 00:00:00 2001 From: Alug Date: Sun, 8 Feb 2026 20:29:01 +0100 Subject: [PATCH] make lumpnumcache case sensitive + optimize + replace quickncase hash with FNV1a lumpnumcache can be case sensitive W_CheckNumForName should also look for uppercase stuff and W_LumpExists is case sensitive anyways this avoids having to always account for case (toupper/tolower) during hash and string comparisons and even allows us to even directly compare our strings instead of using fastcmp (std::strings allow this!) from some benchmarking externally this makes this faster quite a bit --- src/doomdef.h | 16 ---------------- src/m_misc.c | 16 ++++++++++++++++ src/m_misc.h | 1 + src/r_data.c | 6 +++--- src/s_sound.c | 4 ++-- src/w_wad.cpp | 31 ++++++++++--------------------- src/w_wad.h | 2 -- 7 files changed, 32 insertions(+), 44 deletions(-) diff --git a/src/doomdef.h b/src/doomdef.h index 23768d0f4..445b6951f 100644 --- a/src/doomdef.h +++ b/src/doomdef.h @@ -516,22 +516,6 @@ extern boolean capslock; // i_system.c, replace getchar() once the keyboard has been appropriated INT32 I_GetKey(void); -/* http://www.cse.yorku.ca/~oz/hash.html */ -static inline -UINT32 quickncasehash (const char *p, size_t n) -{ - size_t i = 0; - UINT32 x = 5381; - - while (i < n && p[i]) - { - x = (x * 33) ^ tolower(p[i]); - i++; - } - - return x; -} - #ifndef __cplusplus #ifndef min // Double-Check with WATTCP-32's cdefs.h #define min(x, y) (((x) < (y)) ? (x) : (y)) diff --git a/src/m_misc.c b/src/m_misc.c index 8c7b34163..bd038a488 100644 --- a/src/m_misc.c +++ b/src/m_misc.c @@ -1956,6 +1956,22 @@ UINT32 FNV1a_Hash(const char *message, size_t size) return hash; } +// Behaves like the old quickncasehash +// Stops either if it encounters null terminator +// or reaches size +UINT32 FNV1a_QuickCaseHash(const char *message, size_t size) +{ + UINT32 hash = FNV1A_OFFSET_BASIS; + + for (size_t i = 0; i < size && message[i]; i++) + { + hash ^= tolower(message[i]); + hash *= FNV1A_PRIME; + } + + return hash; +} + UINT32 FNV1a_HashString(const char *message) { UINT32 hash = FNV1A_OFFSET_BASIS; diff --git a/src/m_misc.h b/src/m_misc.h index ab7c8986f..813e8abf1 100644 --- a/src/m_misc.h +++ b/src/m_misc.h @@ -123,6 +123,7 @@ FUNCMATH UINT8 M_CountBits(UINT32 num, UINT8 size); // Hashes some message using FNV-1a UINT32 FNV1a_Hash(const char *message, size_t size); +UINT32 FNV1a_QuickCaseHash(const char *message, size_t size); UINT32 FNV1a_HashString(const char *message); UINT32 FNV1a_HashLowercaseString(const char *message); diff --git a/src/r_data.c b/src/r_data.c index 386f0e700..a99218765 100644 --- a/src/r_data.c +++ b/src/r_data.c @@ -622,7 +622,7 @@ Rloadtextures (INT32 i, INT32 w) // Set texture properties. memcpy(texture->name, W_CheckNameForNumPwad(wadnum, lumpnum), sizeof(texture->name)); - texture->hash = quickncasehash(texture->name, 8); + texture->hash = FNV1a_QuickCaseHash(texture->name, 8); texture->width = SHORT(patchlump.width); texture->height = SHORT(patchlump.height); @@ -1066,7 +1066,7 @@ static texture_t *R_ParseTexture(boolean actuallyLoadTexture) // Allocate memory for a zero-patch texture. Obviously, we'll be adding patches momentarily. resultTexture = (texture_t *)Z_Calloc(sizeof(texture_t), PU_STATIC, NULL); memcpy(resultTexture->name, newTextureName, 8); - resultTexture->hash = quickncasehash(newTextureName, 8); + resultTexture->hash = FNV1a_QuickCaseHash(newTextureName, 8); resultTexture->width = newTextureWidth; resultTexture->height = newTextureHeight; resultTexture->type = TEXTURETYPE_COMPOSITE; @@ -1756,7 +1756,7 @@ INT32 R_CheckTextureNumForName(const char *name) if (name[0] == '-') return 0; - hash = quickncasehash(name, 8); + hash = FNV1a_QuickCaseHash(name, 8); for (i = 0; i < tidcachelen; i++) if (tidcache[i].hash == hash && !strncasecmp(tidcache[i].name, name, 8)) diff --git a/src/s_sound.c b/src/s_sound.c index 7f931671d..206b5c37c 100644 --- a/src/s_sound.c +++ b/src/s_sound.c @@ -1302,7 +1302,7 @@ static boolean ReadMusicDefFields(UINT16 wadnum, int line, char *stoken, musicde STRBUFCPY(def->name, value); strlwr(def->name); - def->hash = quickncasehash (def->name, 6); + def->hash = FNV1a_QuickCaseHash(def->name, 6); } (*defp) = def; @@ -1484,7 +1484,7 @@ void S_InitMusicDefs(void) // musicdef_t *S_FindMusicCredit(const char *musname) { - UINT32 hash = quickncasehash(musname, 6); + UINT32 hash = FNV1a_QuickCaseHash(musname, 6); musicdef_t *def; for (INT32 i = 0; i < nummusicdefs; ++i) diff --git a/src/w_wad.cpp b/src/w_wad.cpp index bbf543f1d..44fece4a7 100644 --- a/src/w_wad.cpp +++ b/src/w_wad.cpp @@ -99,7 +99,7 @@ struct LumpnumNameHash std::size_t operator()(const std::string& str) const { // hopefully this is fast enough but should be - return static_cast(quickncasehash(str.c_str(), str.length())); + return static_cast(FNV1a_Hash(str.c_str(), str.length())); } }; @@ -107,8 +107,8 @@ struct LumpnumStringEquals { bool operator()(const std::string& str1, const std::string& str2) const { - // fasticmp should be plenty fast and ignores case - return fasticmp(str1.c_str(), str2.c_str()); + //return fastcmp(str1.c_str(), str2.c_str()); + return str1 == str2; } }; @@ -377,12 +377,12 @@ static inline INT32 W_MakeFileMD5(const char *filename, void *resblock) } // Invalidates the cache of lump numbers. Call this whenever a wad is added. -static void W_InvalidateLumpnumCache(void) +FUNCINLINE static ATTRINLINE void W_InvalidateLumpnumCache(void) { lumpnumcache.clear(); } -UINT32 W_HashLumpName(const char *name) +FUNCINLINE static ATTRINLINE UINT32 W_HashLumpName(const char *name) { return FNV1a_HashLowercaseString(name); } @@ -1246,7 +1246,7 @@ UINT16 W_CheckNumForFullNamePK3(const char *name, UINT16 wad, UINT16 startlump) return INT16_MAX; } -static lumpnum_t CheckLumpInCache(const char *name) +FUNCINLINE static ATTRINLINE lumpnum_t CheckLumpInCache(const char *name) { auto it = lumpnumcache.find(name); if (it != lumpnumcache.end()) @@ -1255,9 +1255,9 @@ static lumpnum_t CheckLumpInCache(const char *name) return LUMPERROR; } -static void AddLumpToCache(lumpnum_t lumpnum, const char *name) +FUNCINLINE static ATTRINLINE void AddLumpToCache(lumpnum_t lumpnum, const char *name) { - lumpnumcache.insert({name, lumpnum}); + lumpnumcache.emplace(name, lumpnum); } // @@ -1415,21 +1415,10 @@ UINT8 W_LumpExists(const char *name) UINT32 hash; size_t namelen; + // Check the lumpnumcache first. lumpnum_t cachenum = CheckLumpInCache(name); if (cachenum != LUMPERROR) - { - // ok ok, we did find a lump in our lumpcache BUTT - // the lumpcache map is case insensitive - // extract the lumpinfo out of the lumpnum - // so we can do one more extra case ~sensitive~ name compare - // otherwise we gotta fall through to our manual lump search below - // lumpnum == (wadnum << 16) | lumpinfo id - UINT16 wadnum = (cachenum >> 16) & 0xFFFF; - UINT16 lumpid = cachenum & 0xFFFF; - lumpinfo_t *lump_p = &wadfiles[wadnum]->lumpinfo[lumpid]; - if (fastcmp(lump_p->name, name)) - return true; - } + return cachenum; namelen = strlen(name); hash = W_HashLumpName(name); diff --git a/src/w_wad.h b/src/w_wad.h index 75c7e9c04..d21afb982 100644 --- a/src/w_wad.h +++ b/src/w_wad.h @@ -174,8 +174,6 @@ INT32 W_AddAutoloadedLocalFiles(char **filenames); #define W_FileHasFolders(wadfile) ((wadfile)->type == RET_PK3) -UINT32 W_HashLumpName(const char *name); - const char *W_CheckNameForNumPwad(UINT16 wad, UINT16 lump); const char *W_CheckNameForNum(lumpnum_t lumpnum);