diff options
| author | Vaxry <vaxry@vaxry.net> | 2025-11-29 13:02:50 +0000 |
|---|---|---|
| committer | Vaxry <vaxry@vaxry.net> | 2025-11-29 17:17:24 +0000 |
| commit | 574ee71d568a95101320f264d7afb25034b8faa3 (patch) | |
| tree | 350f25e697aec0119952a3775ce7d79a3475ddb9 /src | |
| parent | 7e1e24fea615503a3cc05218c12b06c1b6cabdc7 (diff) | |
desktop/overridableVar: improve performance
drop usage of ::map which sucks performance-wise
Diffstat (limited to 'src')
| -rw-r--r-- | src/desktop/types/OverridableVar.hpp | 44 |
1 files changed, 25 insertions, 19 deletions
diff --git a/src/desktop/types/OverridableVar.hpp b/src/desktop/types/OverridableVar.hpp index 9ecfc890..538346c7 100644 --- a/src/desktop/types/OverridableVar.hpp +++ b/src/desktop/types/OverridableVar.hpp @@ -3,7 +3,8 @@ #include <cstdint> #include <type_traits> #include <any> -#include <map> +#include <ranges> +#include <algorithm> #include "../../config/ConfigValue.hpp" namespace Desktop::Types { @@ -25,6 +26,8 @@ namespace Desktop::Types { PRIORITY_WORKSPACE_RULE, PRIORITY_WINDOW_RULE, PRIORITY_SET_PROP, + + PRIORITY_END, }; template <typename T> @@ -56,11 +59,11 @@ namespace Desktop::Types { if (this == &other) 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>) - m_values[value.first] = clampOptional(value.second, m_minValue, m_maxValue); + m_values[i] = clampOptional(*other.m_values[i], m_minValue, m_maxValue); else - m_values[value.first] = value.second; + m_values[i] = other.m_values[i]; } return *this; @@ -71,18 +74,19 @@ namespace Desktop::Types { } void unset(eOverridePriority priority) { - m_values.erase(priority); + m_values[priority] = std::nullopt; } bool hasValue() const { - return !m_values.empty(); + return std::ranges::any_of(m_values, [](const auto& e) { return e.has_value(); }); } T value() const { - if (!m_values.empty()) - return std::prev(m_values.end())->second; - else - throw std::bad_optional_access(); + for (const auto& v : m_values | std::ranges::views::reverse) { + if (v) + return *v; + } + throw std::bad_optional_access(); } T valueOr(T const& other) const { @@ -115,10 +119,12 @@ namespace Desktop::Types { } eOverridePriority getPriority() const { - if (!m_values.empty()) - return std::prev(m_values.end())->first; - else - throw std::bad_optional_access(); + for (int i = PRIORITY_END - 1; i >= 0; --i) { + if (m_values[i]) + return sc<eOverridePriority>(i); + } + + throw std::bad_optional_access(); } void increment(T const& other, eOverridePriority priority) { @@ -143,11 +149,11 @@ namespace Desktop::Types { } private: - std::map<eOverridePriority, T> m_values; - std::optional<T> m_defaultValue; // used for toggling, so required for bool - std::optional<T> m_minValue; - std::optional<T> m_maxValue; - std::any m_configValue; // only there for select variables + 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_minValue; + std::optional<T> m_maxValue; + std::any m_configValue; // only there for select variables }; }
\ No newline at end of file |
