]> granicus.if.org Git - icinga2/commitdiff
Avoid unnecessary copies for the DebugHint class
authorGunnar Beutner <gunnar.beutner@netways.de>
Sat, 27 Aug 2016 16:43:14 +0000 (18:43 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Sat, 27 Aug 2016 16:43:14 +0000 (18:43 +0200)
refs #12509

lib/base/array.hpp
lib/config/expression.cpp
lib/config/expression.hpp

index 5016d66b0acf74b52e472fef404ad2c7cd58d976..a6866d3cf6e4b066f8c2e1b7a171e9078257c2e4 100644 (file)
@@ -50,6 +50,10 @@ public:
        inline Array(void)
        { }
 
+       inline Array(std::initializer_list<Value> init)
+           : m_Data(init)
+       { }
+
        inline ~Array(void)
        { }
 
index 41235476f92ee3c74961bd9d21fce25abc9b79c2..392db1d6d8b06e8e57ad5e5bed13858d21e82bf0 100644 (file)
@@ -460,6 +460,7 @@ ExpressionResult FunctionCallExpression::DoEvaluate(ScriptFrame& frame, DebugHin
 ExpressionResult ArrayExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
 {
        Array::Ptr result = new Array();
+       result->Reserve(m_Expressions.size());
 
        for (Expression *aexpr : m_Expressions) {
                ExpressionResult element = aexpr->Evaluate(frame);
index 5c35c2ad7b508e4cf93e0b801e505bcca2566f99..84cf464c7d07f7ad3a9469de188ab80e2308b050 100644 (file)
@@ -41,37 +41,29 @@ public:
                : m_Hints(hints)
        { }
 
+       DebugHint(Dictionary::Ptr&& hints)
+           : m_Hints(std::move(hints))
+       { }
+
        inline void AddMessage(const String& message, const DebugInfo& di)
        {
-               Array::Ptr amsg = new Array();
-
-               {
-                       ObjectLock olock(amsg);
-
-                       amsg->Reserve(6);
-                       amsg->Add(message);
-                       amsg->Add(di.Path);
-                       amsg->Add(di.FirstLine);
-                       amsg->Add(di.FirstColumn);
-                       amsg->Add(di.LastLine);
-                       amsg->Add(di.LastColumn);
-               }
-
-               GetMessages()->Add(amsg);
+               GetMessages()->Add(new Array({ message, di.Path, di.FirstLine, di.FirstColumn, di.LastLine, di.LastColumn }));
        }
 
        inline DebugHint GetChild(const String& name)
        {
-               Dictionary::Ptr children = GetChildren();
+               const Dictionary::Ptr& children = GetChildren();
 
                Value vchild;
+               Dictionary::Ptr child;
 
                if (!children->Get(name, &vchild)) {
-                       vchild = new Dictionary();
-                       children->Set(name, vchild);
-               }
+                       child = new Dictionary();
+                       children->Set(name, child);
+               } else
+                       child = vchild;
 
-               return DebugHint(vchild);
+               return DebugHint(child);
        }
 
        Dictionary::Ptr ToDictionary(void) const
@@ -81,35 +73,45 @@ public:
 
 private:
        Dictionary::Ptr m_Hints;
+       Array::Ptr m_Messages;
+       Dictionary::Ptr m_Children;
 
-       Array::Ptr GetMessages(void)
+       const Array::Ptr& GetMessages(void)
        {
+               if (m_Messages)
+                       return m_Messages;
+
                if (!m_Hints)
                        m_Hints = new Dictionary();
 
                Value vmessages;
 
                if (!m_Hints->Get("messages", &vmessages)) {
-                       vmessages = new Array();
-                       m_Hints->Set("messages", vmessages);
-               }
+                       m_Messages = new Array();
+                       m_Hints->Set("messages", m_Messages);
+               } else
+                       m_Messages = vmessages;
 
-               return vmessages;
+               return m_Messages;
        }
 
-       Dictionary::Ptr GetChildren(void)
+       const Dictionary::Ptr& GetChildren(void)
        {
+               if (m_Children)
+                       return m_Children;
+
                if (!m_Hints)
                        m_Hints = new Dictionary();
 
                Value vchildren;
 
                if (!m_Hints->Get("properties", &vchildren)) {
-                       vchildren = new Dictionary();
-                       m_Hints->Set("properties", vchildren);
-               }
+                       m_Children = new Dictionary();
+                       m_Hints->Set("properties", m_Children);
+               } else
+                       m_Children = vchildren;
 
-               return vchildren;
+               return m_Children;
        }
 };
 
@@ -157,12 +159,12 @@ public:
            : m_Value(value), m_Code(code)
        { }
 
-       operator Value(void) const
+       operator const Value&(void) const
        {
                return m_Value;
        }
 
-       Value GetValue(void) const
+       const Value& GetValue(void) const
        {
                return m_Value;
        }