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
This commit is contained in:
Alug committed 2026-02-08 20:35:08 +01:00
1 parent 7dbb08315c
commit c84724da37
7 files changed
+32 -44

No files matched your search

-16
View File
@@ -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))
+16
View File
@@ -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;
+1
View File
@@ -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);
+3 -3
View File
@@ -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))
+2 -2
View File
@@ -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)
+10 -21
View File
@@ -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<std::size_t>(quickncasehash(str.c_str(), str.length()));
return static_cast<std::size_t>(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);
-2
View File
@@ -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);