From: Gunnar Beutner Date: Tue, 31 Mar 2015 09:45:38 +0000 (+0200) Subject: Avoid unnecessary dictionary lookups X-Git-Tag: v2.4.0~744 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=91da55872d75308d7310ef652db27b5c6407bfb4;p=icinga2 Avoid unnecessary dictionary lookups fixes #8922 --- diff --git a/lib/base/dictionary.cpp b/lib/base/dictionary.cpp index 9b84cca69..6c3e7b771 100644 --- a/lib/base/dictionary.cpp +++ b/lib/base/dictionary.cpp @@ -46,6 +46,27 @@ Value Dictionary::Get(const String& key) const return it->second; } +/** + * Retrieves a value from a dictionary. + * + * @param key The key whose value should be retrieved. + * @param result The value of the dictionary item (only set when the key exists) + * @returns true if the key exists, false otherwise. + */ +bool Dictionary::Get(const String& key, Value *result) const +{ + ASSERT(!OwnsLock()); + ObjectLock olock(this); + + std::map::const_iterator it = m_Data.find(key); + + if (it == m_Data.end()) + return false; + + *result = it->second; + return true; +} + /** * Sets a value in the dictionary. * diff --git a/lib/base/dictionary.hpp b/lib/base/dictionary.hpp index 89a0ceccb..6a39f44a2 100644 --- a/lib/base/dictionary.hpp +++ b/lib/base/dictionary.hpp @@ -56,6 +56,7 @@ public: { } Value Get(const String& key) const; + bool Get(const String& key, Value *result) const; void Set(const String& key, const Value& value); bool Contains(const String& key) const; diff --git a/lib/config/expression.cpp b/lib/config/expression.cpp index f1b961181..5c55d78b3 100644 --- a/lib/config/expression.cpp +++ b/lib/config/expression.cpp @@ -92,8 +92,10 @@ const DebugInfo& DebuggableExpression::GetDebugInfo(void) const ExpressionResult VariableExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const { - if (frame.Locals && frame.Locals->Contains(m_Variable)) - return frame.Locals->Get(m_Variable); + Value value; + + if (frame.Locals && frame.Locals->Get(m_Variable, &value)) + return value; else if (frame.Self.IsObject() && frame.Locals != static_cast(frame.Self) && VMOps::HasField(frame.Self, m_Variable)) return VMOps::GetField(frame.Self, m_Variable, m_DebugInfo); else diff --git a/lib/config/vmops.hpp b/lib/config/vmops.hpp index 63acdb35c..5a0797ee7 100644 --- a/lib/config/vmops.hpp +++ b/lib/config/vmops.hpp @@ -46,8 +46,9 @@ class VMOps public: static inline Value Variable(ScriptFrame& frame, const String& name, const DebugInfo& debugInfo = DebugInfo()) { - if (frame.Locals && frame.Locals->Contains(name)) - return frame.Locals->Get(name); + Value value; + if (frame.Locals && frame.Locals->Get(name, &value)) + return value; else if (frame.Self.IsObject() && frame.Locals != static_cast(frame.Self) && HasField(frame.Self, name)) return GetField(frame.Self, name, debugInfo); else @@ -244,8 +245,9 @@ public: Dictionary::Ptr dict = dynamic_pointer_cast(object); if (dict) { - if (dict->Contains(field)) - return dict->Get(field); + Value value; + if (dict->Get(field, &value)) + return value; else return GetPrototypeField(context, field, false, debugInfo); }