Add caching to Config::Info

The goal of this change is to make Config::Get(const Info<T>&)
fast so that we can use it in hot paths.
This commit is contained in:
JosJuice
2020-12-05 18:24:41 +01:00
parent 633ab2dd7c
commit d8744e6db8
4 changed files with 110 additions and 14 deletions

View File

@ -24,7 +24,10 @@ std::shared_ptr<Layer> GetLayer(LayerType layer);
void RemoveLayer(LayerType layer);
void AddConfigChangedCallback(ConfigChangedCallback func);
void InvokeConfigChangedCallbacks();
void OnConfigChanged();
// Returns the number of times the config has changed in the current execution of the program
u64 GetConfigVersion();
// Explicit load and save of layers
void Load();
@ -51,6 +54,23 @@ T Get(LayerType layer, const Info<T>& info)
template <typename T>
T Get(const Info<T>& info)
{
CachedValue<T> cached = info.GetCachedValue();
const u64 config_version = GetConfigVersion();
if (cached.config_version < config_version)
{
cached.value = GetUncached(info);
cached.config_version = config_version;
info.SetCachedValue(cached);
}
return cached.value;
}
template <typename T>
T GetUncached(const Info<T>& info)
{
const std::optional<std::string> str = GetAsString(info.GetLocation());
if (!str)
@ -75,7 +95,7 @@ template <typename T>
void Set(LayerType layer, const Info<T>& info, const std::common_type_t<T>& value)
{
GetLayer(layer)->Set(info, value);
InvokeConfigChangedCallbacks();
OnConfigChanged();
}
template <typename T>
@ -99,7 +119,7 @@ void SetBaseOrCurrent(const Info<T>& info, const std::common_type_t<T>& value)
Set<T>(LayerType::CurrentRun, info, value);
}
// Used to defer InvokeConfigChangedCallbacks until after the completion of many config changes.
// Used to defer OnConfigChanged until after the completion of many config changes.
class ConfigChangeCallbackGuard
{
public: