Skip to content

Commit d433899

Browse files
authored
perf(mapcache): Simplify and improve implementation of MapCache to prevent expensive reoccurring redundant map cache reads (#1775)
Reduces cost of map cache updates (with RTS_DEBUG) by around 75%
1 parent d46feb9 commit d433899

File tree

6 files changed

+356
-352
lines changed

6 files changed

+356
-352
lines changed

Generals/Code/GameEngine/Include/GameClient/MapUtil.h

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,10 @@ class MapMetaData
7575
AsciiString m_nameLookupTag;
7676
Region3D m_extent;
7777
Int m_numPlayers;
78-
Bool m_isMultiplayer;
7978

79+
Bool m_isMultiplayer;
8080
Bool m_isOfficial;
81+
Bool m_doesExist; ///< Flag to indicate whether the map physically exists. Should be true.
8182
UnsignedInt m_filesize;
8283
UnsignedInt m_CRC;
8384

@@ -89,10 +90,20 @@ class MapMetaData
8990
AsciiString m_fileName;
9091
};
9192

93+
// TheSuperHackers @performance xezon 02/11/2025 Simplifies and improves the implementation of MapCache
94+
// to prevent expensive reoccurring redundant map cache reads.
95+
9296
class MapCache : public std::map<AsciiString, MapMetaData>
9397
{
98+
typedef std::set<AsciiString> MapNameSet;
99+
94100
public:
95-
MapCache() {}
101+
MapCache()
102+
: m_doCreateStandardMapCacheINI(TRUE)
103+
, m_doLoadStandardMapCacheINI(TRUE)
104+
, m_doLoadUserMapCacheINI(TRUE)
105+
{}
106+
96107
void updateCache( void );
97108

98109
AsciiString getMapDir() const;
@@ -105,22 +116,27 @@ class MapCache : public std::map<AsciiString, MapMetaData>
105116
void addShippingMap(AsciiString mapName) { mapName.toLower(); m_allowedMaps.insert(mapName); }
106117

107118
private:
108-
Bool clearUnseenMaps( AsciiString dirName );
109-
void loadStandardMaps(void);
110-
Bool loadUserMaps(void); // returns true if we needed to (re)parse a map
111-
// Bool addMap( AsciiString dirName, AsciiString fname, WinTimeStamp timestamp,
112-
// UnsignedInt filesize, Bool isOfficial ); ///< returns true if it had to (re)parse the map
113-
Bool addMap( AsciiString dirName, AsciiString fname, FileInfo *fileInfo, Bool isOfficial); ///< returns true if it had to (re)parse the map
114-
void writeCacheINI( Bool userDir );
115-
116-
static const char * m_mapCacheName;
117-
std::map<AsciiString, Bool> m_seen;
118-
119-
std::set<AsciiString> m_allowedMaps;
119+
void prepareUnseenMaps(const AsciiString &mapDir);
120+
Bool clearUnseenMaps(const AsciiString &mapDir);
121+
void loadMapsFromMapCacheINI(const AsciiString &mapDir);
122+
Bool loadMapsFromDisk(const AsciiString &mapDir, Bool isOfficial, Bool filterByAllowedMaps = FALSE); // returns true if we needed to (re)parse a map
123+
Bool addMap(const AsciiString &mapDir, const AsciiString &fname, const AsciiString &lowerFname, FileInfo &fileInfo, Bool isOfficial); ///< returns true if it had to (re)parse the map
124+
void writeCacheINI(const AsciiString &mapDir);
125+
126+
static const char *const m_mapCacheName;
127+
128+
MapNameSet m_allowedMaps;
129+
Bool m_doCreateStandardMapCacheINI;
130+
Bool m_doLoadStandardMapCacheINI;
131+
Bool m_doLoadUserMapCacheINI;
120132
};
121133

122134
extern MapCache *TheMapCache;
123135
extern TechAndSupplyImages TheSupplyAndTechImageLocations;
136+
137+
// TheSuperHackers @refactor xezon 28/11/2025 Refactors the map list population implementation
138+
// by breaking it into smaller pieces to make it more maintainable.
139+
124140
Int populateMapListbox( GameWindow *listbox, Bool useSystemMaps, Bool isMultiplayer, AsciiString mapToSelect = AsciiString::TheEmptyString ); /// Read a list of maps from the run directory and fill in the listbox. Return the selected index
125141
Int populateMapListboxNoReset( GameWindow *listbox, Bool useSystemMaps, Bool isMultiplayer, AsciiString mapToSelect = AsciiString::TheEmptyString ); /// Read a list of maps from the run directory and fill in the listbox. Return the selected index
126142
Bool isValidMap( AsciiString mapName, Bool isMultiplayer ); /// Validate a map

Generals/Code/GameEngine/Source/Common/INI/INIMapCache.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ void INI::parseMapCacheDefinition( INI* ini )
131131

132132
md.m_extent = mdr.m_extent;
133133
md.m_isOfficial = mdr.m_isOfficial != 0;
134+
md.m_doesExist = TRUE;
134135
md.m_isMultiplayer = mdr.m_isMultiplayer != 0;
135136
md.m_numPlayers = mdr.m_numPlayers;
136137
md.m_filesize = mdr.m_filesize;

0 commit comments

Comments
 (0)