vfs_real: Avoid redundant map lookups

Avoids some trivially avoidable map lookups by keeping the result of
find operations around and querying them.
This commit is contained in:
Lioncash 2020-08-18 14:45:18 -04:00
parent bea9ed2548
commit ed9ae5a977

View File

@ -72,8 +72,10 @@ VfsEntryType RealVfsFilesystem::GetEntryType(std::string_view path_) const {
VirtualFile RealVfsFilesystem::OpenFile(std::string_view path_, Mode perms) { VirtualFile RealVfsFilesystem::OpenFile(std::string_view path_, Mode perms) {
const auto path = FS::SanitizePath(path_, FS::DirectorySeparator::PlatformDefault); const auto path = FS::SanitizePath(path_, FS::DirectorySeparator::PlatformDefault);
if (cache.find(path) != cache.end()) {
auto weak = cache[path]; if (const auto weak_iter = cache.find(path); weak_iter != cache.cend()) {
const auto& weak = weak_iter->second;
if (!weak.expired()) { if (!weak.expired()) {
return std::shared_ptr<RealVfsFile>(new RealVfsFile(*this, weak.lock(), path, perms)); return std::shared_ptr<RealVfsFile>(new RealVfsFile(*this, weak.lock(), path, perms));
} }
@ -84,7 +86,7 @@ VirtualFile RealVfsFilesystem::OpenFile(std::string_view path_, Mode perms) {
} }
auto backing = std::make_shared<FS::IOFile>(path, ModeFlagsToString(perms).c_str()); auto backing = std::make_shared<FS::IOFile>(path, ModeFlagsToString(perms).c_str());
cache[path] = backing; cache.insert_or_assign(path, backing);
// Cannot use make_shared as RealVfsFile constructor is private // Cannot use make_shared as RealVfsFile constructor is private
return std::shared_ptr<RealVfsFile>(new RealVfsFile(*this, backing, path, perms)); return std::shared_ptr<RealVfsFile>(new RealVfsFile(*this, backing, path, perms));
@ -116,11 +118,12 @@ VirtualFile RealVfsFilesystem::CopyFile(std::string_view old_path_, std::string_
VirtualFile RealVfsFilesystem::MoveFile(std::string_view old_path_, std::string_view new_path_) { 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 old_path = FS::SanitizePath(old_path_, FS::DirectorySeparator::PlatformDefault);
const auto new_path = FS::SanitizePath(new_path_, FS::DirectorySeparator::PlatformDefault); const auto new_path = FS::SanitizePath(new_path_, FS::DirectorySeparator::PlatformDefault);
const auto cached_file_iter = cache.find(old_path);
if (cache.find(old_path) != cache.end()) { if (cached_file_iter != cache.cend()) {
auto file = cache[old_path].lock(); auto file = cached_file_iter->second.lock();
if (!cache[old_path].expired()) { if (!cached_file_iter->second.expired()) {
file->Close(); file->Close();
} }
@ -131,7 +134,7 @@ VirtualFile RealVfsFilesystem::MoveFile(std::string_view old_path_, std::string_
cache.erase(old_path); cache.erase(old_path);
file->Open(new_path, "r+b"); file->Open(new_path, "r+b");
cache[new_path] = file; cache.insert_or_assign(new_path, std::move(file));
} else { } else {
UNREACHABLE(); UNREACHABLE();
return nullptr; return nullptr;
@ -142,12 +145,15 @@ VirtualFile RealVfsFilesystem::MoveFile(std::string_view old_path_, std::string_
bool RealVfsFilesystem::DeleteFile(std::string_view path_) { bool RealVfsFilesystem::DeleteFile(std::string_view path_) {
const auto path = FS::SanitizePath(path_, FS::DirectorySeparator::PlatformDefault); const auto path = FS::SanitizePath(path_, FS::DirectorySeparator::PlatformDefault);
if (cache.find(path) != cache.end()) { const auto cached_iter = cache.find(path);
if (!cache[path].expired()) {
cache[path].lock()->Close(); if (cached_iter != cache.cend()) {
if (!cached_iter->second.expired()) {
cached_iter->second.lock()->Close();
} }
cache.erase(path); cache.erase(path);
} }
return FS::Delete(path); return FS::Delete(path);
} }
@ -192,21 +198,25 @@ VirtualDir RealVfsFilesystem::MoveDirectory(std::string_view old_path_,
} }
for (auto& kv : cache) { for (auto& kv : cache) {
// Path in cache starts with old_path // If the path in the cache doesn't start with old_path, then bail on this file.
if (kv.first.rfind(old_path, 0) == 0) { if (kv.first.rfind(old_path, 0) != 0) {
continue;
}
const auto file_old_path = const auto file_old_path =
FS::SanitizePath(kv.first, FS::DirectorySeparator::PlatformDefault); FS::SanitizePath(kv.first, FS::DirectorySeparator::PlatformDefault);
const auto file_new_path = auto file_new_path = FS::SanitizePath(new_path + DIR_SEP + kv.first.substr(old_path.size()),
FS::SanitizePath(new_path + DIR_SEP + kv.first.substr(old_path.size()),
FS::DirectorySeparator::PlatformDefault); FS::DirectorySeparator::PlatformDefault);
auto cached = cache[file_old_path]; const auto& cached = cache[file_old_path];
if (!cached.expired()) {
if (cached.expired()) {
continue;
}
auto file = cached.lock(); auto file = cached.lock();
file->Open(file_new_path, "r+b"); file->Open(file_new_path, "r+b");
cache.erase(file_old_path); cache.erase(file_old_path);
cache[file_new_path] = file; cache.insert_or_assign(std::move(file_new_path), std::move(file));
}
}
} }
return OpenDirectory(new_path, Mode::ReadWrite); return OpenDirectory(new_path, Mode::ReadWrite);
@ -214,15 +224,21 @@ VirtualDir RealVfsFilesystem::MoveDirectory(std::string_view old_path_,
bool RealVfsFilesystem::DeleteDirectory(std::string_view path_) { bool RealVfsFilesystem::DeleteDirectory(std::string_view path_) {
const auto path = FS::SanitizePath(path_, FS::DirectorySeparator::PlatformDefault); const auto path = FS::SanitizePath(path_, FS::DirectorySeparator::PlatformDefault);
for (auto& kv : cache) { for (auto& kv : cache) {
// Path in cache starts with old_path // If the path in the cache doesn't start with path, then bail on this file.
if (kv.first.rfind(path, 0) == 0) { if (kv.first.rfind(path, 0) != 0) {
if (!cache[kv.first].expired()) { continue;
cache[kv.first].lock()->Close();
} }
const auto& entry = cache[kv.first];
if (!entry.expired()) {
entry.lock()->Close();
}
cache.erase(kv.first); cache.erase(kv.first);
} }
}
return FS::DeleteDirRecursively(path); return FS::DeleteDirRecursively(path);
} }