Skip to content

Commit 4e23a3e

Browse files
authored
refactor(filesystem): Put file existence cache behind ENABLE_FILESYSTEM_EXISTENCE_CACHE macro (#1295)
1 parent 66c4e8f commit 4e23a3e

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

Core/GameEngine/Include/Common/FileSystem.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,11 @@ class FileSystem : public SubsystemInterface
142142
void unloadMusicFilesFromCD();
143143
AsciiString normalizePath(const AsciiString& path) const; ///< normalizes a file path. The path can refer to a directory. File path must be absolute, but does not need to exist. Returns an empty string on failure.
144144
static Bool isPathInDirectory(const AsciiString& testPath, const AsciiString& basePath); ///< determines if a file path is within a base path. Both paths must be absolute, but do not need to exist.
145+
145146
protected:
146-
mutable std::map<unsigned,bool> m_fileExist;
147+
#if ENABLE_FILESYSTEM_EXISTENCE_CACHE
148+
mutable std::map<unsigned,bool> m_fileExist;
149+
#endif
147150
};
148151

149152
extern FileSystem* TheFileSystem;

Core/GameEngine/Include/Common/GameDefines.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,9 @@
5353
#ifndef USE_BUFFERED_IO
5454
#define USE_BUFFERED_IO (1)
5555
#endif
56+
57+
// Enable cache for local file existence. Reduces amount of disk accesses for better performance,
58+
// but decreases file existence correctness and runtime stability, if a cached file is deleted on runtime.
59+
#ifndef ENABLE_FILESYSTEM_EXISTENCE_CACHE
60+
#define ENABLE_FILESYSTEM_EXISTENCE_CACHE (1)
61+
#endif

Core/GameEngine/Source/Common/System/FileSystem.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,14 @@ File* FileSystem::openFile( const Char *filename, Int access )
179179
if ( TheLocalFileSystem != NULL )
180180
{
181181
file = TheLocalFileSystem->openFile( filename, access );
182+
183+
#if ENABLE_FILESYSTEM_EXISTENCE_CACHE
182184
if (file != NULL && (file->getAccess() & File::CREATE))
183185
{
184186
unsigned key = TheNameKeyGenerator->nameToLowercaseKey(filename);
185187
m_fileExist[key] = true;
186188
}
189+
#endif
187190
}
188191

189192
if ( (TheArchiveFileSystem != NULL) && (file == NULL) )
@@ -202,23 +205,31 @@ Bool FileSystem::doesFileExist(const Char *filename) const
202205
{
203206
USE_PERF_TIMER(FileSystem)
204207

205-
unsigned key=TheNameKeyGenerator->nameToLowercaseKey(filename);
206-
std::map<unsigned,bool>::iterator i=m_fileExist.find(key);
207-
if (i!=m_fileExist.end())
208-
return i->second;
208+
#if ENABLE_FILESYSTEM_EXISTENCE_CACHE
209+
unsigned key=TheNameKeyGenerator->nameToLowercaseKey(filename);
210+
std::map<unsigned,bool>::iterator i=m_fileExist.find(key);
211+
if (i!=m_fileExist.end())
212+
return i->second;
213+
#endif
209214

210215
if (TheLocalFileSystem->doesFileExist(filename))
211-
{
212-
m_fileExist[key]=true;
216+
{
217+
#if ENABLE_FILESYSTEM_EXISTENCE_CACHE
218+
m_fileExist[key]=true;
219+
#endif
213220
return TRUE;
214221
}
215222
if (TheArchiveFileSystem->doesFileExist(filename))
216-
{
217-
m_fileExist[key]=true;
223+
{
224+
#if ENABLE_FILESYSTEM_EXISTENCE_CACHE
225+
m_fileExist[key]=true;
226+
#endif
218227
return TRUE;
219228
}
220229

221-
m_fileExist[key]=false;
230+
#if ENABLE_FILESYSTEM_EXISTENCE_CACHE
231+
m_fileExist[key]=false;
232+
#endif
222233
return FALSE;
223234
}
224235

0 commit comments

Comments
 (0)