From 514b5a86a29aaa8cbd288ecb135e9df5318d18b3 Mon Sep 17 00:00:00 2001 From: Alug Date: Fri, 5 Dec 2025 14:00:04 +0100 Subject: [PATCH] w_wad: use unordered map to cache wad start and end folders --- src/w_wad.cpp | 31 ++++++++++++++++++------------- src/w_wad.h | 4 ++-- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/w_wad.cpp b/src/w_wad.cpp index 09d74f993..8488fe4d6 100644 --- a/src/w_wad.cpp +++ b/src/w_wad.cpp @@ -114,6 +114,11 @@ struct LumpnumStringEquals static std::unordered_map lumpnumcache(LUMPNUMCACHESIZE); +// gotta wrap this in a struct due to external C linkage +struct FolderCache { + std::unordered_map map; +}; + static const char pat_central[] = {0x50, 0x4b, 0x01, 0x02, 0x00}; static const char pat_end[] = {0x50, 0x4b, 0x05, 0x06, 0x00}; @@ -143,8 +148,8 @@ void W_Shutdown(void) Z_Free(wad->lumpinfo[wad->numlumps].fullname); } - M_AATreeFree(wad->startfolders); - M_AATreeFree(wad->endfolders); + delete wad->startfolders; + delete wad->endfolders; Z_Free(wad->lumpinfo); Z_Free(wad); @@ -916,8 +921,8 @@ UINT16 W_InitFile(const char *filename, boolean local, boolean startup) fseek(handle, 0, SEEK_END); wadfile->filesize = static_cast(ftell(handle)); wadfile->type = type; - wadfile->startfolders = M_AATreeAlloc(AATREE_STRING); - wadfile->endfolders = M_AATreeAlloc(AATREE_STRING); + wadfile->startfolders = new FolderCache(); + wadfile->endfolders = new FolderCache(); // already generated, just copy it over memcpy(&wadfile->md5sum, &md5sum, 16); @@ -1171,10 +1176,10 @@ UINT16 W_CheckNumForFolderStartPK3(const char *name, UINT16 wad, UINT16 startlum size_t namelen; lumpinfo_t *lump_p = wadfiles[wad]->lumpinfo + startlump; + auto it = wadfiles[wad]->startfolders->map.find(name); + if (it != wadfiles[wad]->startfolders->map.end()) + return it->second; - void *val = M_AATreeGetString(wadfiles[wad]->startfolders, name); - if (val != NULL) - return static_cast(reinterpret_cast(val)); namelen = strlen(name); for (i = startlump; i < wadfiles[wad]->numlumps; i++, lump_p++) @@ -1185,12 +1190,12 @@ UINT16 W_CheckNumForFolderStartPK3(const char *name, UINT16 wad, UINT16 startlum if (lump_p->fullnamelength == namelen) i++; - M_AATreeSetString(wadfiles[wad]->startfolders, name, reinterpret_cast(static_cast(i))); + wadfiles[wad]->startfolders->map[name] = i; return i; } } - M_AATreeSetString(wadfiles[wad]->startfolders, name, reinterpret_cast(INT16_MAX)); + wadfiles[wad]->startfolders->map[name] = INT16_MAX; return INT16_MAX; } @@ -1203,9 +1208,9 @@ UINT16 W_CheckNumForFolderEndPK3(const char *name, UINT16 wad, UINT16 startlump) size_t namelen; lumpinfo_t *lump_p = wadfiles[wad]->lumpinfo + startlump; - void *val = M_AATreeGetString(wadfiles[wad]->endfolders, name); - if (val != NULL) - return static_cast(reinterpret_cast(val)); + auto it = wadfiles[wad]->endfolders->map.find(name); + if (it != wadfiles[wad]->endfolders->map.end()) + return it->second; namelen = strlen(name); @@ -1215,7 +1220,7 @@ UINT16 W_CheckNumForFolderEndPK3(const char *name, UINT16 wad, UINT16 startlump) break; } - M_AATreeSetString(wadfiles[wad]->endfolders, name, reinterpret_cast(static_cast(i))); + wadfiles[wad]->endfolders->map[name] = i; return i; } diff --git a/src/w_wad.h b/src/w_wad.h index b045b9673..9ed733f78 100644 --- a/src/w_wad.h +++ b/src/w_wad.h @@ -137,8 +137,8 @@ typedef struct wadfile_s #ifdef ROTSPRITE lumpcache_t *rotcache; // Cache rotsprites for rotating patches. #endif - aatree_t *startfolders; - aatree_t *endfolders; + struct FolderCache *startfolders; + struct FolderCache *endfolders; UINT16 numlumps; // this wad's number of resources FILE *handle; UINT32 filesize; // for network