Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type::GetByName(): cache results for faster lookup #9553

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions lib/base/type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "base/scriptglobal.hpp"
#include "base/namespace.hpp"
#include "base/objectlock.hpp"
#include <unordered_map>

using namespace icinga;

Expand All @@ -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<String, Type::Ptr> 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<Type>()) {
typesByName.emplace(kv.first, Type::Ptr(std::move(v)));
}
}

Value ptype;
if (!typesNS->Get(name, &ptype))
return nullptr;
return typesByName;
}());

if (!ptype.IsObjectType<Type>())
return nullptr;
auto type (typesByName.find(name));

return ptype;
if (type != typesByName.end()) {
return type->second;
}

return nullptr;
}

std::vector<Type::Ptr> Type::GetAllTypes()
Expand Down