]> granicus.if.org Git - icinga2/commitdiff
Avoid unnecessary dictionary lookups
authorGunnar Beutner <gunnar@beutner.name>
Tue, 31 Mar 2015 09:45:38 +0000 (11:45 +0200)
committerGunnar Beutner <gunnar@beutner.name>
Tue, 31 Mar 2015 09:45:38 +0000 (11:45 +0200)
fixes #8922

lib/base/dictionary.cpp
lib/base/dictionary.hpp
lib/config/expression.cpp
lib/config/vmops.hpp

index 9b84cca69716ea62515b359c15ba168cfb9cffea..6c3e7b77161b06c7a3b9ac3c635a57e10f25305a 100644 (file)
@@ -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<String, Value>::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.
  *
index 89a0ceccb4b89e4e54b9c4bd7d77a95607d48c7a..6a39f44a2015baf7353cfb089f1ac50747901dd8 100644 (file)
@@ -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;
 
index f1b9611810a18e83af5b797bea7522181dc8d0f8..5c55d78b37ac61e86b799c48dd2e6762a6cdd24e 100644 (file)
@@ -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<Object::Ptr>(frame.Self) && VMOps::HasField(frame.Self, m_Variable))
                return VMOps::GetField(frame.Self, m_Variable, m_DebugInfo);
        else
index 63acdb35c7081e5d5d3535fdb13924092dd1bcd5..5a0797ee75147e1a89db575677cbdd33fc371468 100644 (file)
@@ -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<Object::Ptr>(frame.Self) && HasField(frame.Self, name))
                        return GetField(frame.Self, name, debugInfo);
                else
@@ -244,8 +245,9 @@ public:
                Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(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);
                }