Merge pull request #8118 from Icinga/feature/speed-object-registry-8112

Speed up config object lookup
This commit is contained in:
Julian Brost 2023-01-26 19:03:40 +01:00 committed by GitHub
commit 2d860a0f5e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 8 deletions

View File

@ -11,7 +11,7 @@ ConfigType::~ConfigType()
ConfigObject::Ptr ConfigType::GetObject(const String& name) const ConfigObject::Ptr ConfigType::GetObject(const String& name) const
{ {
std::unique_lock<std::mutex> lock(m_Mutex); std::shared_lock<decltype(m_Mutex)> lock (m_Mutex);
auto nt = m_ObjectMap.find(name); auto nt = m_ObjectMap.find(name);
@ -26,7 +26,7 @@ void ConfigType::RegisterObject(const ConfigObject::Ptr& object)
String name = object->GetName(); String name = object->GetName();
{ {
std::unique_lock<std::mutex> lock(m_Mutex); std::unique_lock<decltype(m_Mutex)> lock (m_Mutex);
auto it = m_ObjectMap.find(name); auto it = m_ObjectMap.find(name);
@ -51,7 +51,7 @@ void ConfigType::UnregisterObject(const ConfigObject::Ptr& object)
String name = object->GetName(); String name = object->GetName();
{ {
std::unique_lock<std::mutex> lock(m_Mutex); std::unique_lock<decltype(m_Mutex)> lock (m_Mutex);
m_ObjectMap.erase(name); m_ObjectMap.erase(name);
m_ObjectVector.erase(std::remove(m_ObjectVector.begin(), m_ObjectVector.end(), object), m_ObjectVector.end()); m_ObjectVector.erase(std::remove(m_ObjectVector.begin(), m_ObjectVector.end(), object), m_ObjectVector.end());
@ -60,7 +60,7 @@ void ConfigType::UnregisterObject(const ConfigObject::Ptr& object)
std::vector<ConfigObject::Ptr> ConfigType::GetObjects() const std::vector<ConfigObject::Ptr> ConfigType::GetObjects() const
{ {
std::unique_lock<std::mutex> lock(m_Mutex); std::shared_lock<decltype(m_Mutex)> lock (m_Mutex);
return m_ObjectVector; return m_ObjectVector;
} }
@ -71,6 +71,6 @@ std::vector<ConfigObject::Ptr> ConfigType::GetObjectsHelper(Type *type)
int ConfigType::GetObjectCount() const int ConfigType::GetObjectCount() const
{ {
std::unique_lock<std::mutex> lock(m_Mutex); std::shared_lock<decltype(m_Mutex)> lock (m_Mutex);
return m_ObjectVector.size(); return m_ObjectVector.size();
} }

View File

@ -7,7 +7,8 @@
#include "base/object.hpp" #include "base/object.hpp"
#include "base/type.hpp" #include "base/type.hpp"
#include "base/dictionary.hpp" #include "base/dictionary.hpp"
#include <mutex> #include <shared_mutex>
#include <unordered_map>
namespace icinga namespace icinga
{ {
@ -48,10 +49,10 @@ for (const auto& object : objects) {
int GetObjectCount() const; int GetObjectCount() const;
private: private:
typedef std::map<String, intrusive_ptr<ConfigObject> > ObjectMap; typedef std::unordered_map<String, intrusive_ptr<ConfigObject> > ObjectMap;
typedef std::vector<intrusive_ptr<ConfigObject> > ObjectVector; typedef std::vector<intrusive_ptr<ConfigObject> > ObjectVector;
mutable std::mutex m_Mutex; mutable std::shared_timed_mutex m_Mutex;
ObjectMap m_ObjectMap; ObjectMap m_ObjectMap;
ObjectVector m_ObjectVector; ObjectVector m_ObjectVector;