Hash name lookup for textures and lumps

This commit is contained in:
James R 2022-01-22 03:18:06 -08:00
parent 93dfbf382c
commit b30ffea49a
5 changed files with 44 additions and 5 deletions

View file

@ -550,6 +550,22 @@ extern boolean capslock;
// i_system.c, replace getchar() once the keyboard has been appropriated // i_system.c, replace getchar() once the keyboard has been appropriated
INT32 I_GetKey(void); 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 min // Double-Check with WATTCP-32's cdefs.h #ifndef min // Double-Check with WATTCP-32's cdefs.h
#define min(x, y) (((x) < (y)) ? (x) : (y)) #define min(x, y) (((x) < (y)) ? (x) : (y))
#endif #endif

View file

@ -60,6 +60,7 @@ INT32 *texturebrightmaps;
// Painfully simple texture id cacheing to make maps load faster. :3 // Painfully simple texture id cacheing to make maps load faster. :3
static struct { static struct {
char name[9]; char name[9];
UINT32 hash;
INT32 id; INT32 id;
} *tidcache = NULL; } *tidcache = NULL;
static INT32 tidcachelen = 0; static INT32 tidcachelen = 0;
@ -803,6 +804,7 @@ Rloadflats (INT32 i, INT32 w)
// Set texture properties. // Set texture properties.
M_Memcpy(texture->name, W_CheckNameForNumPwad(wadnum, lumpnum), sizeof(texture->name)); M_Memcpy(texture->name, W_CheckNameForNumPwad(wadnum, lumpnum), sizeof(texture->name));
texture->hash = quickncasehash(texture->name, 8);
#ifndef NO_PNG_LUMPS #ifndef NO_PNG_LUMPS
if (Picture_IsLumpPNG(header, lumplength)) if (Picture_IsLumpPNG(header, lumplength))
@ -901,6 +903,7 @@ Rloadtextures (INT32 i, INT32 w)
// Set texture properties. // Set texture properties.
M_Memcpy(texture->name, W_CheckNameForNumPwad(wadnum, lumpnum), sizeof(texture->name)); M_Memcpy(texture->name, W_CheckNameForNumPwad(wadnum, lumpnum), sizeof(texture->name));
texture->hash = quickncasehash(texture->name, 8);
#ifndef NO_PNG_LUMPS #ifndef NO_PNG_LUMPS
if (Picture_IsLumpPNG((UINT8 *)&patchlump, lumplength)) if (Picture_IsLumpPNG((UINT8 *)&patchlump, lumplength))
@ -1389,6 +1392,7 @@ static texture_t *R_ParseTexture(boolean actuallyLoadTexture)
// Allocate memory for a zero-patch texture. Obviously, we'll be adding patches momentarily. // 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); resultTexture = (texture_t *)Z_Calloc(sizeof(texture_t),PU_STATIC,NULL);
M_Memcpy(resultTexture->name, newTextureName, 8); M_Memcpy(resultTexture->name, newTextureName, 8);
resultTexture->hash = quickncasehash(newTextureName, 8);
resultTexture->width = newTextureWidth; resultTexture->width = newTextureWidth;
resultTexture->height = newTextureHeight; resultTexture->height = newTextureHeight;
resultTexture->type = TEXTURETYPE_COMPOSITE; resultTexture->type = TEXTURETYPE_COMPOSITE;
@ -1614,25 +1618,29 @@ void R_ClearTextureNumCache(boolean btell)
INT32 R_CheckTextureNumForName(const char *name) INT32 R_CheckTextureNumForName(const char *name)
{ {
INT32 i; INT32 i;
UINT32 hash;
// "NoTexture" marker. // "NoTexture" marker.
if (name[0] == '-') if (name[0] == '-')
return 0; return 0;
hash = quickncasehash(name, 8);
for (i = 0; i < tidcachelen; i++) for (i = 0; i < tidcachelen; i++)
if (!strncasecmp(tidcache[i].name, name, 8)) if (tidcache[i].hash == hash && !strncasecmp(tidcache[i].name, name, 8))
return tidcache[i].id; return tidcache[i].id;
// Need to parse the list backwards, so textures loaded more recently are used in lieu of ones loaded earlier // Need to parse the list backwards, so textures loaded more recently are used in lieu of ones loaded earlier
//for (i = 0; i < numtextures; i++) <- old //for (i = 0; i < numtextures; i++) <- old
for (i = (numtextures - 1); i >= 0; i--) // <- new for (i = (numtextures - 1); i >= 0; i--) // <- new
if (!strncasecmp(textures[i]->name, name, 8)) if (textures[i]->hash == hash && !strncasecmp(textures[i]->name, name, 8))
{ {
tidcachelen++; tidcachelen++;
Z_Realloc(tidcache, tidcachelen * sizeof(*tidcache), PU_STATIC, &tidcache); Z_Realloc(tidcache, tidcachelen * sizeof(*tidcache), PU_STATIC, &tidcache);
strncpy(tidcache[tidcachelen-1].name, name, 8); strncpy(tidcache[tidcachelen-1].name, name, 8);
tidcache[tidcachelen-1].name[8] = '\0'; tidcache[tidcachelen-1].name[8] = '\0';
CONS_Debug(DBG_SETUP, "texture #%s: %s\n", sizeu1(tidcachelen), tidcache[tidcachelen-1].name); CONS_Debug(DBG_SETUP, "texture #%s: %s\n", sizeu1(tidcachelen), tidcache[tidcachelen-1].name);
tidcache[tidcachelen-1].hash = hash;
tidcache[tidcachelen-1].id = i; tidcache[tidcachelen-1].id = i;
return i; return i;
} }

View file

@ -54,6 +54,7 @@ typedef struct
{ {
// Keep name for switch changing, etc. // Keep name for switch changing, etc.
char name[8]; char name[8];
UINT32 hash;
UINT8 type; // TEXTURETYPE_ UINT8 type; // TEXTURETYPE_
INT16 width, height; INT16 width, height;
boolean holes; boolean holes;

View file

@ -356,6 +356,7 @@ static lumpinfo_t* ResGetLumpsStandalone (FILE* handle, UINT16* numlumps, const
lumpinfo->size = ftell(handle); lumpinfo->size = ftell(handle);
fseek(handle, 0, SEEK_SET); fseek(handle, 0, SEEK_SET);
strcpy(lumpinfo->name, lumpname); strcpy(lumpinfo->name, lumpname);
lumpinfo->hash = quickncasehash(lumpname, 8);
// Allocate the lump's long name. // Allocate the lump's long name.
lumpinfo->longname = Z_Malloc(9 * sizeof(char), PU_STATIC, NULL); lumpinfo->longname = Z_Malloc(9 * sizeof(char), PU_STATIC, NULL);
@ -453,6 +454,7 @@ static lumpinfo_t* ResGetLumpsWad (FILE* handle, UINT16* nlmp, const char* filen
lump_p->compression = CM_NOCOMPRESSION; lump_p->compression = CM_NOCOMPRESSION;
memset(lump_p->name, 0x00, 9); memset(lump_p->name, 0x00, 9);
strncpy(lump_p->name, fileinfo->name, 8); strncpy(lump_p->name, fileinfo->name, 8);
lump_p->hash = quickncasehash(lump_p->name, 8);
// Allocate the lump's long name. // Allocate the lump's long name.
lump_p->longname = Z_Malloc(9 * sizeof(char), PU_STATIC, NULL); lump_p->longname = Z_Malloc(9 * sizeof(char), PU_STATIC, NULL);
@ -627,6 +629,7 @@ static lumpinfo_t* ResGetLumpsZip (FILE* handle, UINT16* nlmp)
memset(lump_p->name, '\0', 9); // Making sure they're initialized to 0. Is it necessary? memset(lump_p->name, '\0', 9); // Making sure they're initialized to 0. Is it necessary?
strncpy(lump_p->name, trimname, min(8, dotpos - trimname)); strncpy(lump_p->name, trimname, min(8, dotpos - trimname));
lump_p->hash = quickncasehash(lump_p->name, 8);
lump_p->longname = Z_Calloc(dotpos - trimname + 1, PU_STATIC, NULL); lump_p->longname = Z_Calloc(dotpos - trimname + 1, PU_STATIC, NULL);
strlcpy(lump_p->longname, trimname, dotpos - trimname + 1); strlcpy(lump_p->longname, trimname, dotpos - trimname + 1);
@ -978,12 +981,14 @@ UINT16 W_CheckNumForNamePwad(const char *name, UINT16 wad, UINT16 startlump)
{ {
UINT16 i; UINT16 i;
static char uname[8 + 1]; static char uname[8 + 1];
UINT32 hash;
if (!TestValidLump(wad,0)) if (!TestValidLump(wad,0))
return INT16_MAX; return INT16_MAX;
strlcpy(uname, name, sizeof uname); strlcpy(uname, name, sizeof uname);
strupr(uname); strupr(uname);
hash = quickncasehash(uname, 8);
// //
// scan forward // scan forward
@ -994,7 +999,7 @@ UINT16 W_CheckNumForNamePwad(const char *name, UINT16 wad, UINT16 startlump)
{ {
lumpinfo_t *lump_p = wadfiles[wad]->lumpinfo + startlump; lumpinfo_t *lump_p = wadfiles[wad]->lumpinfo + startlump;
for (i = startlump; i < wadfiles[wad]->numlumps; i++, lump_p++) for (i = startlump; i < wadfiles[wad]->numlumps; i++, lump_p++)
if (!strncmp(lump_p->name, uname, sizeof(uname) - 1)) if (lump_p->hash == hash && !strncmp(lump_p->name, uname, sizeof(uname) - 1))
return i; return i;
} }
@ -1197,15 +1202,20 @@ lumpnum_t W_CheckNumForLongName(const char *name)
// TODO: Make it search through cache first, maybe...? // TODO: Make it search through cache first, maybe...?
lumpnum_t W_CheckNumForMap(const char *name) lumpnum_t W_CheckNumForMap(const char *name)
{ {
UINT32 hash = quickncasehash(name, 8);
UINT16 lumpNum, end; UINT16 lumpNum, end;
UINT32 i; UINT32 i;
lumpinfo_t *p;
for (i = numwadfiles - 1; i < numwadfiles; i--) for (i = numwadfiles - 1; i < numwadfiles; i--)
{ {
if (wadfiles[i]->type == RET_WAD) if (wadfiles[i]->type == RET_WAD)
{ {
for (lumpNum = 0; lumpNum < wadfiles[i]->numlumps; lumpNum++) for (lumpNum = 0; lumpNum < wadfiles[i]->numlumps; lumpNum++)
if (!strncmp(name, (wadfiles[i]->lumpinfo + lumpNum)->name, 8)) {
p = wadfiles[i]->lumpinfo + lumpNum;
if (p->hash == hash && !strncmp(name, p->name, 8))
return (i<<16) + lumpNum; return (i<<16) + lumpNum;
}
} }
else if (wadfiles[i]->type == RET_PK3) else if (wadfiles[i]->type == RET_PK3)
{ {
@ -1216,8 +1226,11 @@ lumpnum_t W_CheckNumForMap(const char *name)
continue; continue;
// Now look for the specified map. // Now look for the specified map.
for (; lumpNum < end; lumpNum++) for (; lumpNum < end; lumpNum++)
if (!strnicmp(name, (wadfiles[i]->lumpinfo + lumpNum)->name, 8)) {
p = wadfiles[i]->lumpinfo + lumpNum;
if (p->hash == hash && !strnicmp(name, p->name, 8))
return (i<<16) + lumpNum; return (i<<16) + lumpNum;
}
} }
} }
return LUMPERROR; return LUMPERROR;

View file

@ -67,6 +67,7 @@ typedef struct
unsigned long position; // filelump_t filepos unsigned long position; // filelump_t filepos
unsigned long disksize; // filelump_t size unsigned long disksize; // filelump_t size
char name[9]; // filelump_t name[] e.g. "LongEntr" char name[9]; // filelump_t name[] e.g. "LongEntr"
UINT32 hash;
char *longname; // e.g. "LongEntryName" char *longname; // e.g. "LongEntryName"
char *fullname; // e.g. "Folder/Subfolder/LongEntryName.extension" char *fullname; // e.g. "Folder/Subfolder/LongEntryName.extension"
size_t size; // real (uncompressed) size size_t size; // real (uncompressed) size