From 9c0a1eb8fdc5886ca5b823f406628854131e6015 Mon Sep 17 00:00:00 2001 From: Ryan Brue Date: Wed, 9 Sep 2026 12:29:31 -0500 Subject: [PATCH] Do not query metadata for files that cannot match a search pattern FileScannerUnix stats every entry it lists in order to read its size and modified time, but files whose name does not match the search pattern are never returned by next(). Skipping them before that call cuts the repository freshness scans at startup from 10832 stat calls to 345 for a RollerCoaster Tycoon Classic installation, and lets the load/save window list a directory's sub directories without querying every file in it. This matters on file systems where metadata operations are expensive: a stat costs ~60us on the FUSE file system Flatpak's document portal provides, against ~1.4us on local disk. Entries whose type readdir did not report are still queried, as they may be a directory that has to be recursed into. Used an LLM to find the issue, and generate the patch. Performed my own testing. Assisted-by: LLM Signed-off-by: Ryan Brue --- src/openrct2/core/FileScanner.cpp | 45 ++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/src/openrct2/core/FileScanner.cpp b/src/openrct2/core/FileScanner.cpp index 4532dc9748..cb91839ab4 100644 --- a/src/openrct2/core/FileScanner.cpp +++ b/src/openrct2/core/FileScanner.cpp @@ -24,6 +24,7 @@ #include "String.hpp" #include +#include #include #include #include @@ -148,17 +149,8 @@ public: virtual void getDirectoryChildren(std::vector& children, const std::string& path) = 0; -private: - void pushState(const std::string& directory) - { - DirectoryState newState; - newState.path = directory; - newState.index = -1; - getDirectoryChildren(newState.listing, directory); - _directoryStack.push(newState); - } - - bool patternMatch(const std::string& fileName) +protected: + bool patternMatch(const std::string& fileName) const { for (const auto& pattern : _patterns) { @@ -170,6 +162,16 @@ private: return false; } +private: + void pushState(const std::string& directory) + { + DirectoryState newState; + newState.path = directory; + newState.index = -1; + getDirectoryChildren(newState.listing, directory); + _directoryStack.push(newState); + } + static std::vector getPatterns(const std::string& delimitedPatterns) { std::vector patterns; @@ -328,7 +330,11 @@ public: const struct dirent* node = namelist[i]; if (!String::equals(node->d_name, ".") && !String::equals(node->d_name, "..")) { - children.push_back(createChild(path.c_str(), node)); + auto child = createChild(path.c_str(), node); + if (child.has_value()) + { + children.push_back(std::move(child.value())); + } } free(namelist[i]); } @@ -342,7 +348,7 @@ private: return 1; } - static DirectoryChild createChild(const utf8* directory, const struct dirent* node) + std::optional createChild(const utf8* directory, const struct dirent* node) const { DirectoryChild result; result.name = std::string(node->d_name); @@ -358,6 +364,19 @@ private: } else { + #ifndef __HAIKU__ + // Reading the size and modified time of a file costs a stat call each, which is + // expensive on file systems with a high per-operation cost, such as network shares or + // the FUSE file system that Flatpak's document portal provides. Files that cannot match + // the search pattern are never returned, so skip them before paying for that. Entries of + // an unknown type still have to be queried to find out whether they are a directory + // which has to be recursed into. + if (node->d_type == DT_REG && !patternMatch(result.name)) + { + return std::nullopt; + } + #endif + result.type = DirectoryChildType::file; // Get the full path of the file