From 30699ed5784812c29415d8797060a631ec151270 Mon Sep 17 00:00:00 2001 From: CORKY <> Date: Tue, 15 Jul 2025 15:30:18 +0300 Subject: [PATCH] feat: faster reading of local files should be around 10x faster then what it was before --- src/EterPack/Folder.cpp | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/src/EterPack/Folder.cpp b/src/EterPack/Folder.cpp index 906f8a14..d7ec3a37 100644 --- a/src/EterPack/Folder.cpp +++ b/src/EterPack/Folder.cpp @@ -76,23 +76,32 @@ bool Folder::fileExists(const std::string& fileName) bool Folder::getFile(const std::string& fileName, std::shared_ptr>& fileData) { - std::string realFileName = fileName; - std::string ymirPrefix = "d:/"; + constexpr char ymirPrefix[] = "d:/"; + std::string relative = fileName; + if (relative.rfind(ymirPrefix, 0) == 0) + relative.erase(0, sizeof(ymirPrefix) - 1); - if (fileName.find(ymirPrefix) == 0) - realFileName = realFileName.substr(ymirPrefix.length()); + std::filesystem::path p = folderPath; + p /= relative; - realFileName = this->folderPath + "/" + realFileName; - - // Try to open the file - std::ifstream file(realFileName, std::ios::binary); + std::ifstream file(p, std::ios::binary | std::ios::ate); if (!file.is_open()) return false; - // Read the file's contents - fileData = std::make_shared>( - std::istreambuf_iterator(file), - std::istreambuf_iterator() - ); + // get size in one call + std::ifstream::pos_type sz = file.tellg(); + if (sz < 0) + return false; + + // allocate exactly once + auto buf = std::make_shared>(static_cast(sz)); + + // rewind and read in one go + file.seekg(0, std::ios::beg); + file.read(buf->data(), sz); + if (!file) + return false; + + fileData = std::move(buf); return true; } \ No newline at end of file