diff options
author | Liam <byteslice@airmail.cc> | 2023-06-13 23:16:10 +0200 |
---|---|---|
committer | Liam <byteslice@airmail.cc> | 2023-06-13 23:16:14 +0200 |
commit | dbbe2376687aa2bebb7f2e8bf74209b2dd08ea8e (patch) | |
tree | 4b64bd1ef436d64d2c21c9877a3b0562e66ecd03 | |
parent | vfs_real: lazily open files (diff) | |
download | yuzu-dbbe2376687aa2bebb7f2e8bf74209b2dd08ea8e.tar yuzu-dbbe2376687aa2bebb7f2e8bf74209b2dd08ea8e.tar.gz yuzu-dbbe2376687aa2bebb7f2e8bf74209b2dd08ea8e.tar.bz2 yuzu-dbbe2376687aa2bebb7f2e8bf74209b2dd08ea8e.tar.lz yuzu-dbbe2376687aa2bebb7f2e8bf74209b2dd08ea8e.tar.xz yuzu-dbbe2376687aa2bebb7f2e8bf74209b2dd08ea8e.tar.zst yuzu-dbbe2376687aa2bebb7f2e8bf74209b2dd08ea8e.zip |
-rw-r--r-- | src/core/file_sys/vfs_real.cpp | 17 | ||||
-rw-r--r-- | src/core/file_sys/vfs_real.h | 2 |
2 files changed, 18 insertions, 1 deletions
diff --git a/src/core/file_sys/vfs_real.cpp b/src/core/file_sys/vfs_real.cpp index d16790b55..13f93eecd 100644 --- a/src/core/file_sys/vfs_real.cpp +++ b/src/core/file_sys/vfs_real.cpp @@ -75,14 +75,26 @@ VfsEntryType RealVfsFilesystem::GetEntryType(std::string_view path_) const { VirtualFile RealVfsFilesystem::OpenFile(std::string_view path_, Mode perms) { const auto path = FS::SanitizePath(path_, FS::DirectorySeparator::PlatformDefault); + if (auto it = cache.find(path); it != cache.end()) { + if (auto file = it->second.lock(); file) { + return file; + } + } + auto reference = std::make_unique<FileReference>(); this->InsertReferenceIntoList(*reference); - return std::shared_ptr<RealVfsFile>(new RealVfsFile(*this, std::move(reference), path, perms)); + auto file = + std::shared_ptr<RealVfsFile>(new RealVfsFile(*this, std::move(reference), path, perms)); + cache[path] = file; + + return file; } VirtualFile RealVfsFilesystem::CreateFile(std::string_view path_, Mode perms) { const auto path = FS::SanitizePath(path_, FS::DirectorySeparator::PlatformDefault); + cache.erase(path); + // Current usages of CreateFile expect to delete the contents of an existing file. if (FS::IsFile(path)) { FS::IOFile temp{path, FS::FileAccessMode::Write, FS::FileType::BinaryFile}; @@ -111,6 +123,8 @@ VirtualFile RealVfsFilesystem::CopyFile(std::string_view old_path_, std::string_ VirtualFile RealVfsFilesystem::MoveFile(std::string_view old_path_, std::string_view new_path_) { const auto old_path = FS::SanitizePath(old_path_, FS::DirectorySeparator::PlatformDefault); const auto new_path = FS::SanitizePath(new_path_, FS::DirectorySeparator::PlatformDefault); + cache.erase(old_path); + cache.erase(new_path); if (!FS::RenameFile(old_path, new_path)) { return nullptr; } @@ -119,6 +133,7 @@ VirtualFile RealVfsFilesystem::MoveFile(std::string_view old_path_, std::string_ bool RealVfsFilesystem::DeleteFile(std::string_view path_) { const auto path = FS::SanitizePath(path_, FS::DirectorySeparator::PlatformDefault); + cache.erase(path); return FS::RemoveFile(path); } diff --git a/src/core/file_sys/vfs_real.h b/src/core/file_sys/vfs_real.h index 48dc2698a..d8c900e33 100644 --- a/src/core/file_sys/vfs_real.h +++ b/src/core/file_sys/vfs_real.h @@ -3,6 +3,7 @@ #pragma once +#include <map> #include <string_view> #include "common/intrusive_list.h" #include "core/file_sys/mode.h" @@ -41,6 +42,7 @@ public: private: using ReferenceListType = Common::IntrusiveListBaseTraits<FileReference>::ListType; + std::map<std::string, std::weak_ptr<VfsFile>, std::less<>> cache; ReferenceListType open_references; ReferenceListType closed_references; size_t num_open_files{}; |