From 2ca80adeb223bec7f7ce266d4c37d9e733a50899 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 11 Dec 2023 11:04:26 -0500 Subject: [PATCH 1/2] GameFileCache: Pass std::function by reference rather than by value std::function is internally allowed to allocate, and these functions aren't being stored anywhere (only called), so we can freely get rid of some minor overhead here by passing by reference. This change also creates aliases for the functions, so that there isn't a lot of visual noise when reading the function signatures. --- Source/Core/UICommon/GameFileCache.cpp | 16 +++++++--------- Source/Core/UICommon/GameFileCache.h | 16 ++++++++++------ 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/Source/Core/UICommon/GameFileCache.cpp b/Source/Core/UICommon/GameFileCache.cpp index d9b08f63d5..1435154941 100644 --- a/Source/Core/UICommon/GameFileCache.cpp +++ b/Source/Core/UICommon/GameFileCache.cpp @@ -43,7 +43,7 @@ GameFileCache::GameFileCache() : m_path(File::GetUserPath(D_CACHE_IDX) + "gameli { } -void GameFileCache::ForEach(std::function&)> f) const +void GameFileCache::ForEach(const ForEachFn& f) const { for (const std::shared_ptr& item : m_cached_files) f(item); @@ -83,11 +83,10 @@ std::shared_ptr GameFileCache::AddOrGet(const std::string& path, return result; } -bool GameFileCache::Update( - const std::vector& all_game_paths, - std::function&)> game_added_to_cache, - std::function game_removed_from_cache, - const std::atomic_bool& processing_halted) +bool GameFileCache::Update(const std::vector& all_game_paths, + const GameAddedToCacheFn& game_added_to_cache, + const GameRemovedFromCacheFn& game_removed_from_cache, + const std::atomic_bool& processing_halted) { // Copy game paths into a set, except ones that match DiscIO::ShouldHideFromGameList. // TODO: Prevent DoFileSearch from looking inside /files/ directories of DirectoryBlobs at all? @@ -151,9 +150,8 @@ bool GameFileCache::Update( return cache_changed; } -bool GameFileCache::UpdateAdditionalMetadata( - std::function&)> game_updated, - const std::atomic_bool& processing_halted) +bool GameFileCache::UpdateAdditionalMetadata(const GameUpdatedFn& game_updated, + const std::atomic_bool& processing_halted) { bool cache_changed = false; diff --git a/Source/Core/UICommon/GameFileCache.h b/Source/Core/UICommon/GameFileCache.h index 1a54942f32..ac8ae7b177 100644 --- a/Source/Core/UICommon/GameFileCache.h +++ b/Source/Core/UICommon/GameFileCache.h @@ -30,9 +30,14 @@ public: Yes = 1, }; + using ForEachFn = std::function&)>; + using GameAddedToCacheFn = std::function&)>; + using GameRemovedFromCacheFn = std::function; + using GameUpdatedFn = std::function&)>; + GameFileCache(); - void ForEach(std::function&)> f) const; + void ForEach(const ForEachFn& f) const; size_t GetSize() const; void Clear(DeleteOnDisk delete_on_disk); @@ -42,12 +47,11 @@ public: // These functions return true if the call modified the cache. bool Update(const std::vector& all_game_paths, - std::function&)> game_added_to_cache = {}, - std::function game_removed_from_cache = {}, + const GameAddedToCacheFn& game_added_to_cache = {}, + const GameRemovedFromCacheFn& game_removed_from_cache = {}, const std::atomic_bool& processing_halted = false); - bool UpdateAdditionalMetadata( - std::function&)> game_updated = {}, - const std::atomic_bool& processing_halted = false); + bool UpdateAdditionalMetadata(const GameUpdatedFn& game_updated = {}, + const std::atomic_bool& processing_halted = false); bool Load(); bool Save(); From ff38362216ff3e73eb99f7fda90ec802da1386ff Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 11 Dec 2023 11:12:06 -0500 Subject: [PATCH 2/2] GameFileCache: Use std::span with Update() All we're really doing is iterating over a sequence of strings, so we don't need to tie this specifically to std::vector. --- Source/Core/UICommon/GameFileCache.cpp | 2 +- Source/Core/UICommon/GameFileCache.h | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Source/Core/UICommon/GameFileCache.cpp b/Source/Core/UICommon/GameFileCache.cpp index 1435154941..2838d331d8 100644 --- a/Source/Core/UICommon/GameFileCache.cpp +++ b/Source/Core/UICommon/GameFileCache.cpp @@ -83,7 +83,7 @@ std::shared_ptr GameFileCache::AddOrGet(const std::string& path, return result; } -bool GameFileCache::Update(const std::vector& all_game_paths, +bool GameFileCache::Update(std::span all_game_paths, const GameAddedToCacheFn& game_added_to_cache, const GameRemovedFromCacheFn& game_removed_from_cache, const std::atomic_bool& processing_halted) diff --git a/Source/Core/UICommon/GameFileCache.h b/Source/Core/UICommon/GameFileCache.h index ac8ae7b177..5544b3a143 100644 --- a/Source/Core/UICommon/GameFileCache.h +++ b/Source/Core/UICommon/GameFileCache.h @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -46,7 +47,7 @@ public: std::shared_ptr AddOrGet(const std::string& path, bool* cache_changed); // These functions return true if the call modified the cache. - bool Update(const std::vector& all_game_paths, + bool Update(std::span all_game_paths, const GameAddedToCacheFn& game_added_to_cache = {}, const GameRemovedFromCacheFn& game_removed_from_cache = {}, const std::atomic_bool& processing_halted = false);