Merge pull request #9283 from JosJuice/config-get-speedup

Common: Optimize Config::Get
This commit is contained in:
Léo Lam 2020-11-27 02:36:33 +01:00 committed by GitHub
commit 2a85534805
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 1 deletions

View File

@ -178,6 +178,25 @@ LayerType GetActiveLayerForConfig(const Location& config)
return LayerType::Base;
}
std::optional<std::string> GetAsString(const Location& config)
{
std::optional<std::string> 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<std::string>(config);
if (result.has_value())
break;
}
}
return result;
}
ConfigChangeCallbackGuard::ConfigChangeCallbackGuard()
{
++s_callback_guards;

View File

@ -39,6 +39,8 @@ std::optional<System> GetSystemFromName(const std::string& system);
const std::string& GetLayerName(LayerType layer);
LayerType GetActiveLayerForConfig(const Location&);
std::optional<std::string> GetAsString(const Location&);
template <typename T>
T Get(LayerType layer, const Info<T>& info)
{
@ -50,7 +52,11 @@ T Get(LayerType layer, const Info<T>& info)
template <typename T>
T Get(const Info<T>& info)
{
return GetLayer(GetActiveLayerForConfig(info.location))->Get(info);
const std::optional<std::string> str = GetAsString(info.location);
if (!str)
return info.default_value;
return detail::TryParse<T>(*str).value_or(info.default_value);
}
template <typename T>