From 50379838b3ac9d707a60eaf0a9ce6c560e42ea6b Mon Sep 17 00:00:00 2001 From: Georges Berenger Date: Tue, 14 Jan 2025 09:38:47 -0800 Subject: [PATCH] Optimize string copies in FileCache Summary: Minor optimizations to avoid string copies. Differential Revision: D68144276 fbshipit-source-id: 64bda0ded700cd4bbb88e7de4adda01be9e8cc8f --- vrs/FileCache.cpp | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/vrs/FileCache.cpp b/vrs/FileCache.cpp index 38a47ddd..e1fae8b1 100644 --- a/vrs/FileCache.cpp +++ b/vrs/FileCache.cpp @@ -28,28 +28,25 @@ namespace vrs { unique_ptr FileCache::sFileCache; int FileCache::makeFileCache(const string& app, const string& parentFolder) { - string appName; -#if !IS_WINDOWS_PLATFORM() - appName = '.'; // make folder invisible -#endif - appName += app; - - string mainFolder = parentFolder.empty() ? os::getHomeFolder() : parentFolder; + const string& mainFolderBase = parentFolder.empty() ? os::getHomeFolder() : parentFolder; + string mainFolder; + mainFolder.reserve(mainFolderBase.size() + app.size() + 3); + mainFolder.append(mainFolderBase); if (!mainFolder.empty() && mainFolder.back() != '/' && mainFolder.back() != '\\') { mainFolder += '/'; } +#if !IS_WINDOWS_PLATFORM() + mainFolder.append("."); // make folder invisible +#endif + mainFolder.append(app).append("/"); int error = 0; - if (!os::isDir(mainFolder) && (error = os::makeDir(mainFolder)) != 0) { - return error; - } - mainFolder += appName + '/'; - if (!os::isDir(mainFolder) && (error = os::makeDir(mainFolder)) != 0) { + if (!os::isDir(mainFolder) && (error = os::makeDirectories(mainFolder)) != 0) { return error; } #if IS_WINDOWS_PLATFORM() os::hidePath(mainFolder); #endif - sFileCache.reset(new FileCache(mainFolder)); + sFileCache.reset(new FileCache(std::move(mainFolder))); return 0; } @@ -62,8 +59,12 @@ FileCache* FileCache::getFileCache() { } int FileCache::getFile(const string& domain, const string& filename, string& outFilePath) { - string folder = mainFolder_ + domain; - outFilePath = folder + '/' + filename; + string folder; + folder.reserve(mainFolder_.size() + domain.size()); + folder.append(mainFolder_).append(domain); + outFilePath.clear(); + outFilePath.reserve(folder.size() + filename.size() + 1); + outFilePath.append(folder).append("/").append(filename); if (os::isFile(outFilePath)) { return 0; } @@ -75,11 +76,14 @@ int FileCache::getFile(const string& domain, const string& filename, string& out } int FileCache::getFile(const string& filename, string& outFilePath) { - outFilePath = mainFolder_ + filename; + outFilePath.clear(); + outFilePath.reserve(mainFolder_.size() + filename.size()); + outFilePath.append(mainFolder_).append(filename); if (os::isFile(outFilePath)) { return 0; } if (os::pathExists(outFilePath)) { + outFilePath.clear(); return INVALID_DISK_DATA; } return FILE_NOT_FOUND;