Skip to content

Commit

Permalink
filesystem: got a Idea for initial faster times
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphaelIT7 committed Nov 25, 2024
1 parent 9813481 commit beddb0e
Showing 1 changed file with 68 additions and 4 deletions.
72 changes: 68 additions & 4 deletions source/modules/filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ static ConVar holylib_filesystem_usesearchpathcache("holylib_filesystem_usesearc

static ConVar holylib_filesystem_precachehandle("holylib_filesystem_precachehandle", "1", 0,
"If enabled, it will try to predict which file it will open next and open the file to keep a handle ready to be opened.");
static ConVar holylib_filesystem_savesearchcache("holylib_filesystem_savesearchcache", "1", 0,
"If enabled, it will write the search cache into a file and restore it when starting, using it to improve performance.");


// Optimization Idea: When Gmod calls GetFileTime, we could try to get the filehandle in parallel to have it ready when gmod calls it.
// We could also cache every FULL searchpath to not have to look up a file every time.

IThreadPool* pFileSystemPool = NULL;
static IThreadPool* pFileSystemPool = NULL;

static void OnThreadsChange(IConVar* convar, const char* pOldValue, float flOldValue)
{
Expand Down Expand Up @@ -312,6 +314,68 @@ static void ShowPredictionErrosCmd(const CCommand &args)
}
static ConCommand showpredictionerrors("holylib_filesystem_showpredictionerrors", ShowPredictionErrosCmd, "Shows all prediction errors that ocurred", 0);

struct SaveCacheEntry {
char pathID[24];
char path[128];
char absolutePath[255];
};

struct SaveCache {
unsigned int version = 1;
unsigned int usedPaths = 0;
SaveCacheEntry paths[1 << 14]; // Support 16k entries for now
};

static void WriteSaveCache(const CCommand& args)
{
FileHandle_t handle = g_pFullFileSystem->Open("holylib_searchcache.dat", "wb", "MOD_WRITE");
if (handle)
{
SaveCache* savecache = new SaveCache;
for (auto& [strPath, cache] : m_SearchCache)
{
for (auto& [strEntry, storeID] : cache)
{
SaveCacheEntry& entry = savecache->paths[savecache->usedPaths++];
V_strcpy(entry.pathID, strPath.data());
V_strcpy(entry.path, strEntry.data());
g_pFullFileSystem->RelativePathToFullPath(strEntry.data(), strPath.data(), entry.absolutePath, sizeof(entry.absolutePath));
}
}

g_pFullFileSystem->Write(savecache, sizeof(SaveCache), handle);
g_pFullFileSystem->Close(handle);
Msg("holylib: successfully wrote searchcache file\n");
} else {
Warning("holylib: Failed to open searchcache file!\n");
}
}
static ConCommand writesearchcache("holylib_filesystem_writesearchcache", WriteSaveCache, "Writes the save cache into a file", 0);

static void InitFileSystem(IFileSystem* pFileSystem)
{
g_pFullFileSystem = pFileSystem;

if (holylib_filesystem_savesearchcache.GetBool())
{
FileHandle_t handle = g_pFullFileSystem->Open("holylib_searchcache.dat", "rb", "MOD_WRITE");
if (handle)
{
SaveCache* savecache = new SaveCache;
g_pFullFileSystem->Read(savecache, sizeof(SaveCache), handle);
g_pFullFileSystem->Close(handle);
//if (g_pFileSystemModule.InDebug())
Msg("holylib - filesystem: Loaded searchcache file (%i)\n", savecache->usedPaths);
} else {
if (g_pFileSystemModule.InDebug())
Msg("holylib - filesystem: Failed to find searchcache file\n");
}
}

if (g_pFileSystemModule.InDebug())
Msg("holylib - filesystem: Initialized filesystem\n");
}

inline void OnFileHandleOpen(FileHandle_t handle, const char* pFileMode)
{
if (pFileMode[0] == 'r') // I see a potential crash, but this should never happen.
Expand All @@ -327,7 +391,7 @@ static FileHandle_t hook_CBaseFileSystem_FindFileInSearchPath(void* filesystem,
return detour_CBaseFileSystem_FindFileInSearchPath.GetTrampoline<Symbols::CBaseFileSystem_FindFileInSearchPath>()(filesystem, openInfo);

if (!g_pFullFileSystem)
g_pFullFileSystem = (IFileSystem*)filesystem;
InitFileSystem((IFileSystem*)filesystem);

VPROF_BUDGET("HolyLib - CBaseFileSystem::FindFile", VPROF_BUDGETGROUP_OTHER_FILESYSTEM);

Expand Down Expand Up @@ -396,7 +460,7 @@ static long hook_CBaseFileSystem_FastFileTime(void* filesystem, const CSearchPat
return detour_CBaseFileSystem_FastFileTime.GetTrampoline<Symbols::CBaseFileSystem_FastFileTime>()(filesystem, path, pFileName);

if (!g_pFullFileSystem)
g_pFullFileSystem = (IFileSystem*)filesystem;
InitFileSystem((IFileSystem*)filesystem);

VPROF_BUDGET("HolyLib - CBaseFileSystem::FastFileTime", VPROF_BUDGETGROUP_OTHER_FILESYSTEM);

Expand Down Expand Up @@ -453,7 +517,7 @@ static bool hook_CBaseFileSystem_FixUpPath(IFileSystem* filesystem, const char *
VPROF_BUDGET("HolyLib - CBaseFileSystem::FixUpPath", VPROF_BUDGETGROUP_OTHER_FILESYSTEM);

if (!g_pFullFileSystem)
g_pFullFileSystem = (IFileSystem*)filesystem;
InitFileSystem((IFileSystem*)filesystem);

V_strncpy( pFixedUpFileName, pFileName, sizeFixedUpFileName );
V_FixSlashes( pFixedUpFileName, CORRECT_PATH_SEPARATOR );
Expand Down

0 comments on commit beddb0e

Please sign in to comment.