diff --git a/lib/base/type.cpp b/lib/base/type.cpp index 6e4fd6b57a3..0980c64a98f 100644 --- a/lib/base/type.cpp +++ b/lib/base/type.cpp @@ -4,6 +4,7 @@ #include "base/scriptglobal.hpp" #include "base/namespace.hpp" #include "base/objectlock.hpp" +#include using namespace icinga; @@ -29,19 +30,31 @@ void Type::Register(const Type::Ptr& type) Type::Ptr Type::GetByName(const String& name) { - Namespace::Ptr typesNS = ScriptGlobal::Get("Types", &Empty); + typedef std::unordered_map TypesByName; + + static const TypesByName typesByName ([]() -> TypesByName { + TypesByName typesByName; + Namespace::Ptr typesNS = ScriptGlobal::Get("Types", &Empty); + ObjectLock oLock (typesNS); - if (!typesNS) - return nullptr; + for (auto& kv : typesNS) { + auto v (kv.second->Get()); + + if (v.IsObjectType()) { + typesByName.emplace(kv.first, Type::Ptr(std::move(v))); + } + } - Value ptype; - if (!typesNS->Get(name, &ptype)) - return nullptr; + return typesByName; + }()); - if (!ptype.IsObjectType()) - return nullptr; + auto type (typesByName.find(name)); - return ptype; + if (type != typesByName.end()) { + return type->second; + } + + return nullptr; } std::vector Type::GetAllTypes()