Skip to content

Commit 574ee71

Browse files
committed
desktop/overridableVar: improve performance
drop usage of ::map which sucks performance-wise
1 parent 7e1e24f commit 574ee71

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

src/desktop/types/OverridableVar.hpp

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
#include <cstdint>
44
#include <type_traits>
55
#include <any>
6-
#include <map>
6+
#include <ranges>
7+
#include <algorithm>
78
#include "../../config/ConfigValue.hpp"
89

910
namespace Desktop::Types {
@@ -25,6 +26,8 @@ namespace Desktop::Types {
2526
PRIORITY_WORKSPACE_RULE,
2627
PRIORITY_WINDOW_RULE,
2728
PRIORITY_SET_PROP,
29+
30+
PRIORITY_END,
2831
};
2932

3033
template <typename T>
@@ -56,11 +59,11 @@ namespace Desktop::Types {
5659
if (this == &other)
5760
return *this;
5861

59-
for (auto const& value : other.m_values) {
62+
for (size_t i = 0; i < PRIORITY_END; ++i) {
6063
if constexpr (Extended && !std::is_same_v<T, bool>)
61-
m_values[value.first] = clampOptional(value.second, m_minValue, m_maxValue);
64+
m_values[i] = clampOptional(*other.m_values[i], m_minValue, m_maxValue);
6265
else
63-
m_values[value.first] = value.second;
66+
m_values[i] = other.m_values[i];
6467
}
6568

6669
return *this;
@@ -71,18 +74,19 @@ namespace Desktop::Types {
7174
}
7275

7376
void unset(eOverridePriority priority) {
74-
m_values.erase(priority);
77+
m_values[priority] = std::nullopt;
7578
}
7679

7780
bool hasValue() const {
78-
return !m_values.empty();
81+
return std::ranges::any_of(m_values, [](const auto& e) { return e.has_value(); });
7982
}
8083

8184
T value() const {
82-
if (!m_values.empty())
83-
return std::prev(m_values.end())->second;
84-
else
85-
throw std::bad_optional_access();
85+
for (const auto& v : m_values | std::ranges::views::reverse) {
86+
if (v)
87+
return *v;
88+
}
89+
throw std::bad_optional_access();
8690
}
8791

8892
T valueOr(T const& other) const {
@@ -115,10 +119,12 @@ namespace Desktop::Types {
115119
}
116120

117121
eOverridePriority getPriority() const {
118-
if (!m_values.empty())
119-
return std::prev(m_values.end())->first;
120-
else
121-
throw std::bad_optional_access();
122+
for (int i = PRIORITY_END - 1; i >= 0; --i) {
123+
if (m_values[i])
124+
return sc<eOverridePriority>(i);
125+
}
126+
127+
throw std::bad_optional_access();
122128
}
123129

124130
void increment(T const& other, eOverridePriority priority) {
@@ -143,11 +149,11 @@ namespace Desktop::Types {
143149
}
144150

145151
private:
146-
std::map<eOverridePriority, T> m_values;
147-
std::optional<T> m_defaultValue; // used for toggling, so required for bool
148-
std::optional<T> m_minValue;
149-
std::optional<T> m_maxValue;
150-
std::any m_configValue; // only there for select variables
152+
std::array<std::optional<T>, PRIORITY_END> m_values;
153+
std::optional<T> m_defaultValue; // used for toggling, so required for bool
154+
std::optional<T> m_minValue;
155+
std::optional<T> m_maxValue;
156+
std::any m_configValue; // only there for select variables
151157
};
152158

153159
}

0 commit comments

Comments
 (0)