desktop/overridableVar: improve performance

drop usage of ::map which sucks performance-wise
This commit is contained in:
Vaxry 2025-11-29 13:02:50 +00:00
parent 7e1e24fea6
commit 574ee71d56
No known key found for this signature in database
GPG key ID: 665806380871D640

View file

@ -3,7 +3,8 @@
#include <cstdint> #include <cstdint>
#include <type_traits> #include <type_traits>
#include <any> #include <any>
#include <map> #include <ranges>
#include <algorithm>
#include "../../config/ConfigValue.hpp" #include "../../config/ConfigValue.hpp"
namespace Desktop::Types { namespace Desktop::Types {
@ -25,6 +26,8 @@ namespace Desktop::Types {
PRIORITY_WORKSPACE_RULE, PRIORITY_WORKSPACE_RULE,
PRIORITY_WINDOW_RULE, PRIORITY_WINDOW_RULE,
PRIORITY_SET_PROP, PRIORITY_SET_PROP,
PRIORITY_END,
}; };
template <typename T> template <typename T>
@ -56,11 +59,11 @@ namespace Desktop::Types {
if (this == &other) if (this == &other)
return *this; return *this;
for (auto const& value : other.m_values) { for (size_t i = 0; i < PRIORITY_END; ++i) {
if constexpr (Extended && !std::is_same_v<T, bool>) if constexpr (Extended && !std::is_same_v<T, bool>)
m_values[value.first] = clampOptional(value.second, m_minValue, m_maxValue); m_values[i] = clampOptional(*other.m_values[i], m_minValue, m_maxValue);
else else
m_values[value.first] = value.second; m_values[i] = other.m_values[i];
} }
return *this; return *this;
@ -71,18 +74,19 @@ namespace Desktop::Types {
} }
void unset(eOverridePriority priority) { void unset(eOverridePriority priority) {
m_values.erase(priority); m_values[priority] = std::nullopt;
} }
bool hasValue() const { bool hasValue() const {
return !m_values.empty(); return std::ranges::any_of(m_values, [](const auto& e) { return e.has_value(); });
} }
T value() const { T value() const {
if (!m_values.empty()) for (const auto& v : m_values | std::ranges::views::reverse) {
return std::prev(m_values.end())->second; if (v)
else return *v;
throw std::bad_optional_access(); }
throw std::bad_optional_access();
} }
T valueOr(T const& other) const { T valueOr(T const& other) const {
@ -115,10 +119,12 @@ namespace Desktop::Types {
} }
eOverridePriority getPriority() const { eOverridePriority getPriority() const {
if (!m_values.empty()) for (int i = PRIORITY_END - 1; i >= 0; --i) {
return std::prev(m_values.end())->first; if (m_values[i])
else return sc<eOverridePriority>(i);
throw std::bad_optional_access(); }
throw std::bad_optional_access();
} }
void increment(T const& other, eOverridePriority priority) { void increment(T const& other, eOverridePriority priority) {
@ -143,11 +149,11 @@ namespace Desktop::Types {
} }
private: private:
std::map<eOverridePriority, T> m_values; std::array<std::optional<T>, PRIORITY_END> m_values;
std::optional<T> m_defaultValue; // used for toggling, so required for bool std::optional<T> m_defaultValue; // used for toggling, so required for bool
std::optional<T> m_minValue; std::optional<T> m_minValue;
std::optional<T> m_maxValue; std::optional<T> m_maxValue;
std::any m_configValue; // only there for select variables std::any m_configValue; // only there for select variables
}; };
} }