[indexer] Optimize RulesHolder

Signed-off-by: Konstantin Pastbin <konstantin.pastbin@gmail.com>
This commit is contained in:
Konstantin Pastbin 2023-09-12 22:03:12 +03:00 committed by Viktor Govako
parent e6999f32c8
commit 433ed87ad1
3 changed files with 23 additions and 110 deletions

View file

@ -20,16 +20,16 @@ int32_t constexpr kOverlaysMaxPriority = 10000;
public:
int m_scale = -1;
int m_type = -1;
int m_index = -1;
size_t m_index = std::numeric_limits<size_t>::max(); // an index to RulesHolder.m_dRules[]
int m_priority = -1;
bool m_hatching = false;
Key() = default;
Key(int s, int t, int i) : m_scale(s), m_type(t), m_index(i), m_priority(-1) {}
Key(int s, int t, size_t i) : m_scale(s), m_type(t), m_index(i), m_priority(-1) {}
bool operator==(Key const & r) const
{
return (m_scale == r.m_scale && m_type == r.m_type && m_index == r.m_index);
return (m_index == r.m_index);
}
void SetPriority(int pr) { m_priority = pr; }

View file

@ -35,36 +35,6 @@ namespace
}
} // namespace
void BaseRule::CheckCacheSize(size_t s)
{
m_id1.resize(s);
MakeEmptyID();
}
uint32_t BaseRule::GetID(size_t threadSlot) const
{
ASSERT(m_id1.size() > threadSlot, ());
return m_id1[threadSlot];
}
void BaseRule::SetID(size_t threadSlot, uint32_t id) const
{
ASSERT(m_id1.size() > threadSlot, ());
m_id1[threadSlot] = id;
}
void BaseRule::MakeEmptyID(size_t threadSlot)
{
ASSERT(m_id1.size() > threadSlot, ());
m_id1[threadSlot] = empty_id;
}
void BaseRule::MakeEmptyID()
{
for (size_t i = 0; i < m_id1.size(); ++i)
MakeEmptyID(i);
}
LineDefProto const * BaseRule::GetLine() const
{
return 0;
@ -118,15 +88,10 @@ RulesHolder::~RulesHolder()
void RulesHolder::Clean()
{
for (size_t i = 0; i < m_container.size(); ++i)
{
RuleVec & v = m_container[i];
for (size_t j = 0; j < v.size(); ++j)
delete v[j];
v.clear();
}
for (size_t i = 0; i < m_dRules.size(); ++i)
delete m_dRules[i];
m_rules.clear();
m_dRules.clear();
m_colors.clear();
}
@ -135,29 +100,18 @@ Key RulesHolder::AddRule(int scale, rule_type_t type, BaseRule * p)
ASSERT ( 0 <= scale && scale <= scales::GetUpperStyleScale(), (scale) );
ASSERT ( 0 <= type && type < count_of_rules, () );
m_container[type].push_back(p);
m_dRules.push_back(p);
auto const index = m_dRules.size() - 1;
Key const k(scale, type, index);
vector<uint32_t> & v = m_rules[scale][type];
v.push_back(static_cast<uint32_t>(m_container[type].size()-1));
int const ret = static_cast<int>(v.size() - 1);
Key k(scale, type, ret);
ASSERT ( Find(k) == p, (ret) );
ASSERT(Find(k) == p, (index));
return k;
}
BaseRule const * RulesHolder::Find(Key const & k) const
{
RulesMap::const_iterator i = m_rules.find(k.m_scale);
if (i == m_rules.end()) return 0;
vector<uint32_t> const & v = (i->second)[k.m_type];
ASSERT ( k.m_index >= 0, (k.m_index) );
if (static_cast<size_t>(k.m_index) < v.size())
return m_container[k.m_type][v[k.m_index]];
else
return 0;
ASSERT_LESS(k.m_index, m_dRules.size(), ());
return m_dRules[k.m_index];
}
uint32_t RulesHolder::GetBgColor(int scale) const
@ -178,16 +132,6 @@ uint32_t RulesHolder::GetColor(std::string const & name) const
return it->second;
}
void RulesHolder::ClearCaches()
{
ForEachRule([](BaseRule * p) { p->MakeEmptyID(); });
}
void RulesHolder::ResizeCaches(size_t s)
{
ForEachRule([s](BaseRule * p) { p->CheckCacheSize(s); });
}
namespace
{
RulesHolder & rules(MapStyle mapStyle)

View file

@ -11,7 +11,6 @@
#include <array>
#include <iostream>
#include <map>
#include <memory>
#include <string>
#include <unordered_map>
@ -30,24 +29,10 @@ namespace drule
{
class BaseRule
{
mutable buffer_vector<uint32_t, 4> m_id1;
std::unique_ptr<ISelector> m_selector;
public:
static uint32_t const empty_id = 0xFFFFFFFF;
BaseRule() = default;
virtual ~BaseRule() = default;
void CheckCacheSize(size_t s);
uint32_t GetID(size_t threadSlot) const;
void SetID(size_t threadSlot, uint32_t id) const;
void MakeEmptyID(size_t threadSlot);
void MakeEmptyID();
virtual LineDefProto const * GetLine() const;
virtual AreaRuleProto const * GetArea() const;
virtual SymbolRuleProto const * GetSymbol() const;
@ -61,34 +46,19 @@ namespace drule
// Set runtime feature style selector
void SetSelector(std::unique_ptr<ISelector> && selector);
private:
std::unique_ptr<ISelector> m_selector;
};
class RulesHolder
{
// container of rules by type
using RuleVec = std::vector<BaseRule*>;
std::array<RuleVec, count_of_rules> m_container;
/// scale -> array of rules by type -> index of rule in m_container
using RulesMap = std::map<int32_t, std::array<std::vector<uint32_t>, count_of_rules>>;
RulesMap m_rules;
/// background color for scales in range [0...scales::UPPER_STYLE_SCALE]
std::vector<uint32_t> m_bgColors;
std::unordered_map<std::string, uint32_t> m_colors;
public:
RulesHolder();
~RulesHolder();
Key AddRule(int scale, rule_type_t type, BaseRule * p);
void Clean();
void ClearCaches();
void ResizeCaches(size_t Size);
BaseRule const * Find(Key const & k) const;
uint32_t GetBgColor(int scale) const;
@ -103,20 +73,19 @@ namespace drule
template <class ToDo> void ForEachRule(ToDo && toDo)
{
for (auto const & rule : m_rules)
{
for (int j = 0; j < count_of_rules; ++j)
{
std::vector<uint32_t> const & v = rule.second[j];
for (size_t k = 0; k < v.size(); ++k)
toDo(m_container[j][v[k]]);
}
}
for (auto const dRule : m_dRules)
toDo(dRule);
}
private:
void InitBackgroundColors(ContainerProto const & cp);
void InitColors(ContainerProto const & cp);
void Clean();
/// background color for scales in range [0...scales::UPPER_STYLE_SCALE]
std::vector<uint32_t> m_bgColors;
std::unordered_map<std::string, uint32_t> m_colors;
std::vector<BaseRule *> m_dRules;
};
RulesHolder & rules();