Skip to content

Commit 7a697f3

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

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

src/desktop/types/OverridableVar.hpp

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

99
namespace Desktop::Types {
@@ -25,6 +25,8 @@ namespace Desktop::Types {
2525
PRIORITY_WORKSPACE_RULE,
2626
PRIORITY_WINDOW_RULE,
2727
PRIORITY_SET_PROP,
28+
29+
PRIORITY_END,
2830
};
2931

3032
template <typename T>
@@ -56,11 +58,11 @@ namespace Desktop::Types {
5658
if (this == &other)
5759
return *this;
5860

59-
for (auto const& value : other.m_values) {
61+
for (size_t i = 0; i < PRIORITY_END; ++i) {
6062
if constexpr (Extended && !std::is_same_v<T, bool>)
61-
m_values[value.first] = clampOptional(value.second, m_minValue, m_maxValue);
63+
m_values[i] = clampOptional(*other.m_values[i], m_minValue, m_maxValue);
6264
else
63-
m_values[value.first] = value.second;
65+
m_values[i] = other.m_values[i];
6466
}
6567

6668
return *this;
@@ -71,18 +73,19 @@ namespace Desktop::Types {
7173
}
7274

7375
void unset(eOverridePriority priority) {
74-
m_values.erase(priority);
76+
m_values[priority] = std::nullopt;
7577
}
7678

7779
bool hasValue() const {
78-
return !m_values.empty();
80+
return std::ranges::any_of(m_values, [](const auto& e) { return e.has_value(); });
7981
}
8082

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

8891
T valueOr(T const& other) const {
@@ -115,10 +118,12 @@ namespace Desktop::Types {
115118
}
116119

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

124129
void increment(T const& other, eOverridePriority priority) {
@@ -143,11 +148,11 @@ namespace Desktop::Types {
143148
}
144149

145150
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
151+
std::array<std::optional<T>, PRIORITY_END> m_values;
152+
std::optional<T> m_defaultValue; // used for toggling, so required for bool
153+
std::optional<T> m_minValue;
154+
std::optional<T> m_maxValue;
155+
std::any m_configValue; // only there for select variables
151156
};
152157

153158
}

0 commit comments

Comments
 (0)