From 2f264c64486fdb0d6ff880cce30e229803d4105c Mon Sep 17 00:00:00 2001 From: JosJuice Date: Wed, 25 Nov 2020 16:26:13 +0100 Subject: [PATCH] Common: Optimize Config::Get The way Config::Get works in master, it first calls Config::GetActiveLayerForConfig which searches for the setting in all layers, and then calls Config::Layer::Get which searches for the same setting again within the given layer. We can remove this second search by combining the logic of Config::GetActiveLayerForConfig and Config::Layer::Get into one function. --- Source/Core/Common/Config/Config.cpp | 19 +++++++++++++++++++ Source/Core/Common/Config/Config.h | 8 +++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/Source/Core/Common/Config/Config.cpp b/Source/Core/Common/Config/Config.cpp index 29004787e4..03c0aed72e 100644 --- a/Source/Core/Common/Config/Config.cpp +++ b/Source/Core/Common/Config/Config.cpp @@ -178,6 +178,25 @@ LayerType GetActiveLayerForConfig(const Location& config) return LayerType::Base; } +std::optional GetAsString(const Location& config) +{ + std::optional result; + ReadLock lock(s_layers_rw_lock); + + for (auto layer : SEARCH_ORDER) + { + const auto it = s_layers.find(layer); + if (it != s_layers.end()) + { + result = it->second->Get(config); + if (result.has_value()) + break; + } + } + + return result; +} + ConfigChangeCallbackGuard::ConfigChangeCallbackGuard() { ++s_callback_guards; diff --git a/Source/Core/Common/Config/Config.h b/Source/Core/Common/Config/Config.h index fa315857bf..1fbaef0f88 100644 --- a/Source/Core/Common/Config/Config.h +++ b/Source/Core/Common/Config/Config.h @@ -39,6 +39,8 @@ std::optional GetSystemFromName(const std::string& system); const std::string& GetLayerName(LayerType layer); LayerType GetActiveLayerForConfig(const Location&); +std::optional GetAsString(const Location&); + template T Get(LayerType layer, const Info& info) { @@ -50,7 +52,11 @@ T Get(LayerType layer, const Info& info) template T Get(const Info& info) { - return GetLayer(GetActiveLayerForConfig(info.location))->Get(info); + const std::optional str = GetAsString(info.location); + if (!str) + return info.default_value; + + return detail::TryParse(*str).value_or(info.default_value); } template