summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorliamwhite <liamwhite@users.noreply.github.com>2023-05-09 15:47:36 +0200
committerGitHub <noreply@github.com>2023-05-09 15:47:36 +0200
commit7944f271dcbd8217ea0a9e556b75edbe7a23eaff (patch)
tree7e2159b9fc6043e8318d14d0eb8da803d4019d6a
parentMerge pull request #10203 from german77/calibration (diff)
parentvfs_layered: avoid n^2 lookup in layeredfs building (diff)
downloadyuzu-7944f271dcbd8217ea0a9e556b75edbe7a23eaff.tar
yuzu-7944f271dcbd8217ea0a9e556b75edbe7a23eaff.tar.gz
yuzu-7944f271dcbd8217ea0a9e556b75edbe7a23eaff.tar.bz2
yuzu-7944f271dcbd8217ea0a9e556b75edbe7a23eaff.tar.lz
yuzu-7944f271dcbd8217ea0a9e556b75edbe7a23eaff.tar.xz
yuzu-7944f271dcbd8217ea0a9e556b75edbe7a23eaff.tar.zst
yuzu-7944f271dcbd8217ea0a9e556b75edbe7a23eaff.zip
-rw-r--r--src/core/file_sys/vfs_layered.cpp9
-rw-r--r--src/core/file_sys/vfs_vector.cpp19
-rw-r--r--src/core/file_sys/vfs_vector.h4
3 files changed, 29 insertions, 3 deletions
diff --git a/src/core/file_sys/vfs_layered.cpp b/src/core/file_sys/vfs_layered.cpp
index da05dd395..3e6426afc 100644
--- a/src/core/file_sys/vfs_layered.cpp
+++ b/src/core/file_sys/vfs_layered.cpp
@@ -2,6 +2,7 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#include <algorithm>
+#include <set>
#include <utility>
#include "core/file_sys/vfs_layered.h"
@@ -58,11 +59,13 @@ std::string LayeredVfsDirectory::GetFullPath() const {
std::vector<VirtualFile> LayeredVfsDirectory::GetFiles() const {
std::vector<VirtualFile> out;
+ std::set<std::string, std::less<>> out_names;
+
for (const auto& layer : dirs) {
for (const auto& file : layer->GetFiles()) {
- if (std::find_if(out.begin(), out.end(), [&file](const VirtualFile& comp) {
- return comp->GetName() == file->GetName();
- }) == out.end()) {
+ auto file_name = file->GetName();
+ if (!out_names.contains(file_name)) {
+ out_names.emplace(std::move(file_name));
out.push_back(file);
}
}
diff --git a/src/core/file_sys/vfs_vector.cpp b/src/core/file_sys/vfs_vector.cpp
index 251d9d7c9..af1df4c51 100644
--- a/src/core/file_sys/vfs_vector.cpp
+++ b/src/core/file_sys/vfs_vector.cpp
@@ -67,6 +67,23 @@ VectorVfsDirectory::VectorVfsDirectory(std::vector<VirtualFile> files_,
VectorVfsDirectory::~VectorVfsDirectory() = default;
+VirtualFile VectorVfsDirectory::GetFile(std::string_view file_name) const {
+ if (!optimized_file_index_built) {
+ optimized_file_index.clear();
+ for (size_t i = 0; i < files.size(); i++) {
+ optimized_file_index.emplace(files[i]->GetName(), i);
+ }
+ optimized_file_index_built = true;
+ }
+
+ const auto it = optimized_file_index.find(file_name);
+ if (it != optimized_file_index.end()) {
+ return files[it->second];
+ }
+
+ return nullptr;
+}
+
std::vector<VirtualFile> VectorVfsDirectory::GetFiles() const {
return files;
}
@@ -107,6 +124,7 @@ bool VectorVfsDirectory::DeleteSubdirectory(std::string_view subdir_name) {
}
bool VectorVfsDirectory::DeleteFile(std::string_view file_name) {
+ optimized_file_index_built = false;
return FindAndRemoveVectorElement(files, file_name);
}
@@ -124,6 +142,7 @@ VirtualFile VectorVfsDirectory::CreateFile(std::string_view file_name) {
}
void VectorVfsDirectory::AddFile(VirtualFile file) {
+ optimized_file_index_built = false;
files.push_back(std::move(file));
}
diff --git a/src/core/file_sys/vfs_vector.h b/src/core/file_sys/vfs_vector.h
index bfedb6e42..c9955755b 100644
--- a/src/core/file_sys/vfs_vector.h
+++ b/src/core/file_sys/vfs_vector.h
@@ -105,6 +105,7 @@ public:
VirtualDir parent = nullptr);
~VectorVfsDirectory() override;
+ VirtualFile GetFile(std::string_view file_name) const override;
std::vector<VirtualFile> GetFiles() const override;
std::vector<VirtualDir> GetSubdirectories() const override;
bool IsWritable() const override;
@@ -126,6 +127,9 @@ private:
VirtualDir parent;
std::string name;
+
+ mutable std::map<std::string, size_t, std::less<>> optimized_file_index;
+ mutable bool optimized_file_index_built{};
};
} // namespace FileSys