From d8744e6db8f97e4d721d098010d1b1115e31403f Mon Sep 17 00:00:00 2001 From: JosJuice Date: Sat, 5 Dec 2020 18:24:41 +0100 Subject: Add caching to Config::Info The goal of this change is to make Config::Get(const Info&) fast so that we can use it in hot paths. --- Source/Core/Common/Config/Config.cpp | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'Source/Core/Common/Config/Config.cpp') diff --git a/Source/Core/Common/Config/Config.cpp b/Source/Core/Common/Config/Config.cpp index 03c0aed72e..071d009b51 100644 --- a/Source/Core/Common/Config/Config.cpp +++ b/Source/Core/Common/Config/Config.cpp @@ -3,6 +3,7 @@ // Refer to the license.txt file included. #include +#include #include #include #include @@ -17,6 +18,7 @@ using Layers = std::map>; static Layers s_layers; static std::list s_callbacks; static u32 s_callback_guards = 0; +static std::atomic s_config_version = 0; static std::shared_mutex s_layers_rw_lock; @@ -31,7 +33,7 @@ static void AddLayerInternal(std::shared_ptr layer) const Config::LayerType layer_type = layer->GetLayer(); s_layers.insert_or_assign(layer_type, std::move(layer)); } - InvokeConfigChangedCallbacks(); + OnConfigChanged(); } void AddLayer(std::unique_ptr loader) @@ -59,7 +61,7 @@ void RemoveLayer(LayerType layer) s_layers.erase(layer); } - InvokeConfigChangedCallbacks(); + OnConfigChanged(); } void AddConfigChangedCallback(ConfigChangedCallback func) @@ -67,15 +69,22 @@ void AddConfigChangedCallback(ConfigChangedCallback func) s_callbacks.emplace_back(std::move(func)); } -void InvokeConfigChangedCallbacks() +void OnConfigChanged() { if (s_callback_guards) return; + s_config_version.fetch_add(1, std::memory_order_relaxed); + for (const auto& callback : s_callbacks) callback(); } +u64 GetConfigVersion() +{ + return s_config_version.load(std::memory_order_relaxed); +} + // Explicit load and save of layers void Load() { @@ -85,7 +94,7 @@ void Load() for (auto& layer : s_layers) layer.second->Load(); } - InvokeConfigChangedCallbacks(); + OnConfigChanged(); } void Save() @@ -96,7 +105,7 @@ void Save() for (auto& layer : s_layers) layer.second->Save(); } - InvokeConfigChangedCallbacks(); + OnConfigChanged(); } void Init() @@ -207,7 +216,7 @@ ConfigChangeCallbackGuard::~ConfigChangeCallbackGuard() if (--s_callback_guards) return; - InvokeConfigChangedCallbacks(); + OnConfigChanged(); } } // namespace Config -- cgit v1.2.3