]> granicus.if.org Git - icinga2/commitdiff
Avoid unnecessary string copies
authorGunnar Beutner <gunnar.beutner@netways.de>
Sat, 20 Aug 2016 21:46:44 +0000 (23:46 +0200)
committerMichael Friedrich <michael.friedrich@netways.de>
Tue, 23 Aug 2016 13:01:40 +0000 (15:01 +0200)
fixes #12509

13 files changed:
lib/base/netstring.cpp
lib/base/netstring.hpp
lib/base/string.hpp
lib/config/configcompilercontext.cpp
lib/config/configcompilercontext.hpp
lib/icinga/dependency-apply.cpp
lib/icinga/dependency.cpp
lib/icinga/host.cpp
lib/icinga/notification-apply.cpp
lib/icinga/scheduleddowntime-apply.cpp
lib/icinga/service-apply.cpp
lib/icinga/service.cpp
lib/icinga/timeperiod.cpp

index 98937b4fb4606e8ec2238ab32bed2aece7b800d0..022d5b0a33d546bb548922ac2fa12f261b03a9cc 100644 (file)
@@ -110,8 +110,19 @@ StreamReadStatus NetString::ReadStringFromStream(const Stream::Ptr& stream, Stri
 void NetString::WriteStringToStream(const Stream::Ptr& stream, const String& str)
 {
        std::ostringstream msgbuf;
-       msgbuf << str.GetLength() << ":" << str << ",";
+       WriteStringToStream(msgbuf, str);
 
        String msg = msgbuf.str();
        stream->Write(msg.CStr(), msg.GetLength());
 }
+
+/**
+ * Writes data into a stream using the netstring format.
+ *
+ * @param stream The stream.
+ * @param str The String that is to be written.
+ */
+void NetString::WriteStringToStream(std::ostream& stream, const String& str)
+{
+       stream << str.GetLength() << ":" << str << ",";
+}
index 9be09f1a24d46543653c318ccc2f7978ce310aa0..a15e6a11a9d5870304927eb99c18b8d77a0fe769 100644 (file)
@@ -40,6 +40,7 @@ class I2_BASE_API NetString
 public:
        static StreamReadStatus ReadStringFromStream(const Stream::Ptr& stream, String *message, StreamReadContext& context, bool may_wait = false);
        static void WriteStringToStream(const Stream::Ptr& stream, const String& message);
+       static void WriteStringToStream(std::ostream& stream, const String& message);
 
 private:
        NetString(void);
index d27cd3c341c849cca1675a50f213fcaf2a1c8e0b..0605896e57b875ff89b383dabec179929f10c4c9 100644 (file)
@@ -327,7 +327,7 @@ private:
 
 inline std::ostream& operator<<(std::ostream& stream, const String& str)
 {
-       stream << static_cast<std::string>(str);
+       stream << str.GetData();
        return stream;
 }
 
@@ -341,102 +341,102 @@ inline std::istream& operator>>(std::istream& stream, String& str)
 
 inline String operator+(const String& lhs, const String& rhs)
 {
-       return static_cast<std::string>(lhs)+static_cast<std::string>(rhs);
+       return lhs.GetData() + rhs.GetData();
 }
 
 inline String operator+(const String& lhs, const char *rhs)
 {
-       return static_cast<std::string>(lhs)+rhs;
+       return lhs.GetData() + rhs;
 }
 
 inline String operator+(const char *lhs, const String& rhs)
 {
-       return lhs + static_cast<std::string>(rhs);
+       return lhs + rhs.GetData();
 }
 
 inline bool operator==(const String& lhs, const String& rhs)
 {
-       return static_cast<std::string>(lhs) == static_cast<std::string>(rhs);
+       return lhs.GetData() == rhs.GetData();
 }
 
 inline bool operator==(const String& lhs, const char *rhs)
 {
-       return static_cast<std::string>(lhs) == rhs;
+       return lhs.GetData() == rhs;
 }
 
 inline bool operator==(const char *lhs, const String& rhs)
 {
-       return lhs == static_cast<std::string>(rhs);
+       return lhs == rhs.GetData();
 }
 
 inline bool operator<(const String& lhs, const char *rhs)
 {
-       return static_cast<std::string>(lhs) < rhs;
+       return lhs.GetData() < rhs;
 }
 
 inline bool operator<(const char *lhs, const String& rhs)
 {
-       return lhs < static_cast<std::string>(rhs);
+       return lhs < rhs.GetData();
 }
 
 inline bool operator>(const String& lhs, const String& rhs)
 {
-       return static_cast<std::string>(lhs) > static_cast<std::string>(rhs);
+       return lhs.GetData() > rhs.GetData();
 }
 
 inline bool operator>(const String& lhs, const char *rhs)
 {
-       return static_cast<std::string>(lhs) > rhs;
+       return lhs.GetData() > rhs;
 }
 
 inline bool operator>(const char *lhs, const String& rhs)
 {
-       return lhs > static_cast<std::string>(rhs);
+       return lhs > rhs.GetData();
 }
 
 inline bool operator<=(const String& lhs, const String& rhs)
 {
-       return static_cast<std::string>(lhs) <= static_cast<std::string>(rhs);
+       return lhs.GetData() <= rhs.GetData();
 }
 
 inline bool operator<=(const String& lhs, const char *rhs)
 {
-       return static_cast<std::string>(lhs) <= rhs;
+       return lhs.GetData() <= rhs;
 }
 
 inline bool operator<=(const char *lhs, const String& rhs)
 {
-       return lhs <= static_cast<std::string>(rhs);
+       return lhs <= rhs.GetData();
 }
 
 inline bool operator>=(const String& lhs, const String& rhs)
 {
-       return static_cast<std::string>(lhs) >= static_cast<std::string>(rhs);
+       return lhs.GetData() >= rhs.GetData();
 }
 
 inline bool operator>=(const String& lhs, const char *rhs)
 {
-       return static_cast<std::string>(lhs) >= rhs;
+       return lhs.GetData() >= rhs;
 }
 
 inline bool operator>=(const char *lhs, const String& rhs)
 {
-       return lhs >= static_cast<std::string>(rhs);
+       return lhs >= rhs.GetData();
 }
 
 inline bool operator!=(const String& lhs, const String& rhs)
 {
-       return static_cast<std::string>(lhs) != static_cast<std::string>(rhs);
+       return lhs.GetData() != rhs.GetData();
 }
 
 inline bool operator!=(const String& lhs, const char *rhs)
 {
-       return static_cast<std::string>(lhs) != rhs;
+       return lhs.GetData() != rhs;
 }
 
 inline bool operator!=(const char *lhs, const String& rhs)
 {
-       return lhs != static_cast<std::string>(rhs);
+       return lhs != rhs.GetData();
 }
 
 inline String::Iterator range_begin(String& x)
index c006dcef13bf680ed4cf825249ad1604b021cd61..0a66968d78de86c8c7af23f700f29498e8af9030 100644 (file)
@@ -23,7 +23,6 @@
 #include "base/netstring.hpp"
 #include "base/exception.hpp"
 #include <boost/foreach.hpp>
-#include <fstream>
 
 using namespace icinga;
 
@@ -32,6 +31,10 @@ ConfigCompilerContext *ConfigCompilerContext::GetInstance(void)
        return Singleton<ConfigCompilerContext>::GetInstance();
 }
 
+ConfigCompilerContext::ConfigCompilerContext(void)
+    : m_ObjectsFP(NULL)
+{ }
+
 void ConfigCompilerContext::OpenObjectsFile(const String& filename)
 {
        m_ObjectsPath = filename;
@@ -42,7 +45,7 @@ void ConfigCompilerContext::OpenObjectsFile(const String& filename)
        if (!*fp)
                BOOST_THROW_EXCEPTION(std::runtime_error("Could not open '" + m_ObjectsTempFile + "' file"));
 
-       m_ObjectsFP = new StdioStream(fp, true);
+       m_ObjectsFP = fp;
 }
 
 void ConfigCompilerContext::WriteObject(const Dictionary::Ptr& object)
@@ -54,14 +57,14 @@ void ConfigCompilerContext::WriteObject(const Dictionary::Ptr& object)
 
        {
                boost::mutex::scoped_lock lock(m_Mutex);
-               NetString::WriteStringToStream(m_ObjectsFP, json);
+               NetString::WriteStringToStream(*m_ObjectsFP, json);
        }
 }
 
 void ConfigCompilerContext::CancelObjectsFile(void)
 {
-       m_ObjectsFP->Close();
-       m_ObjectsFP.reset();
+       delete m_ObjectsFP;
+       m_ObjectsFP = NULL;
 
 #ifdef _WIN32
        _unlink(m_ObjectsTempFile.CStr());
@@ -72,8 +75,8 @@ void ConfigCompilerContext::CancelObjectsFile(void)
 
 void ConfigCompilerContext::FinishObjectsFile(void)
 {
-       m_ObjectsFP->Close();
-       m_ObjectsFP.reset();
+       delete m_ObjectsFP;
+       m_ObjectsFP = NULL;
 
 #ifdef _WIN32
        _unlink(m_ObjectsPath.CStr());
index 1e4721112d15639ee4cfd7719925a1027049854c..73886a202a9e626767eacb035b87a1833056f5cd 100644 (file)
@@ -21,9 +21,9 @@
 #define CONFIGCOMPILERCONTEXT_H
 
 #include "config/i2-config.hpp"
-#include "base/stdiostream.hpp"
 #include "base/dictionary.hpp"
 #include <boost/thread/mutex.hpp>
+#include <fstream>
 
 namespace icinga
 {
@@ -34,6 +34,8 @@ namespace icinga
 class I2_CONFIG_API ConfigCompilerContext
 {
 public:
+       ConfigCompilerContext(void);
+
        void OpenObjectsFile(const String& filename);
        void WriteObject(const Dictionary::Ptr& object);
        void CancelObjectsFile(void);
@@ -44,7 +46,7 @@ public:
 private:
        String m_ObjectsPath;
        String m_ObjectsTempFile;
-       StdioStream::Ptr m_ObjectsFP;
+       std::fstream *m_ObjectsFP;
 
        mutable boost::mutex m_Mutex;
 };
index e25eaa86f89e0983062e9532668e5bd0fed51c42..168aeb336396f7df26c9c8b3c1218d9299c299a6 100644 (file)
@@ -48,8 +48,10 @@ bool Dependency::EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, cons
 
        DebugInfo di = rule.GetDebugInfo();
 
+#ifdef _DEBUG
        Log(LogDebug, "Dependency")
                << "Applying dependency '" << name << "' to object '" << checkable->GetName() << "' for rule " << di;
+#endif /* _DEBUG */
 
        ConfigItemBuilder::Ptr builder = new ConfigItemBuilder(di);
        builder->SetType("Dependency");
@@ -123,10 +125,9 @@ bool Dependency::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyR
                        BOOST_THROW_EXCEPTION(ScriptError("Dictionary iterator requires value to be a dictionary.", di));
 
                Array::Ptr arr = vinstances;
-               Array::Ptr arrclone = arr->ShallowClone();
 
-               ObjectLock olock(arrclone);
-               BOOST_FOREACH(const Value& instance, arrclone) {
+               ObjectLock olock(arr);
+               BOOST_FOREACH(const Value& instance, arr) {
                        String name = rule.GetName();
 
                        if (!rule.GetFKVar().IsEmpty()) {
index 2369a63a9cec37c53e601bbe81a9e101b374cd7f..5d3ea95075e7db2a58e813790debc99b1cc003c1 100644 (file)
@@ -87,15 +87,10 @@ void Dependency::OnAllConfigLoaded(void)
        Host::Ptr childHost = Host::GetByName(GetChildHostName());
 
        if (childHost) {
-               if (GetChildServiceName().IsEmpty()) {
-                       Log(LogDebug, "Dependency")
-                           << "Dependency '" << GetName() << "' child host '" << GetChildHostName() << ".";
+               if (GetChildServiceName().IsEmpty())
                        m_Child = childHost;
-               } else {
-                       Log(LogDebug, "Dependency")
-                           << "Dependency '" << GetName() << "' child host '" << GetChildHostName() << "' service '" << GetChildServiceName() << "' .";
+               else
                        m_Child = childHost->GetServiceByShortName(GetChildServiceName());
-               }
        }
        
        if (!m_Child)
@@ -106,15 +101,10 @@ void Dependency::OnAllConfigLoaded(void)
        Host::Ptr parentHost = Host::GetByName(GetParentHostName());
 
        if (parentHost) {
-               if (GetParentServiceName().IsEmpty()) {
-                       Log(LogDebug, "Dependency")
-                           << "Dependency '" << GetName() << "' parent host '" << GetParentHostName() << ".";
+               if (GetParentServiceName().IsEmpty())
                        m_Parent = parentHost;
-               } else {
-                       Log(LogDebug, "Dependency")
-                           << "Dependency '" << GetName() << "' parent host '" << GetParentHostName() << "' service '" << GetParentServiceName() << "' .";
+               else
                        m_Parent = parentHost->GetServiceByShortName(GetParentServiceName());
-               }
        }
        
        if (!m_Parent)
index f3c907fcd4c442c175c3edad32a2e303aca3a60f..d9d9716596344fd282191428f5ced9ed7d0896f4 100644 (file)
@@ -67,16 +67,16 @@ void Host::OnAllConfigLoaded(void)
 
 void Host::CreateChildObjects(const Type::Ptr& childType)
 {
-       if (childType->GetName() == "ScheduledDowntime")
+       if (childType == ScheduledDowntime::TypeInstance)
                ScheduledDowntime::EvaluateApplyRules(this);
 
-       if (childType->GetName() == "Notification")
+       if (childType == Notification::TypeInstance)
                Notification::EvaluateApplyRules(this);
 
-       if (childType->GetName() == "Dependency")
+       if (childType == Dependency::TypeInstance)
                Dependency::EvaluateApplyRules(this);
 
-       if (childType->GetName() == "Service")
+       if (childType == Service::TypeInstance)
                Service::EvaluateApplyRules(this);
 }
 
index edc4c2b57e747d76fc1386017c7c2864dc9162f8..a64e2554b74675e0650d6e76b50bac7e66fa89ff 100644 (file)
@@ -48,8 +48,10 @@ bool Notification::EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, co
 
        DebugInfo di = rule.GetDebugInfo();
 
+#ifdef _DEBUG
        Log(LogDebug, "Notification")
            << "Applying notification '" << name << "' to object '" << checkable->GetName() << "' for rule " << di;
+#endif /* _DEBUG */
 
        ConfigItemBuilder::Ptr builder = new ConfigItemBuilder(di);
        builder->SetType("Notification");
index 6eff9fad16220be21d4c7f896ad80a236b66d690..c61e9dc51c3fed26dd63fa448a9fd9b4ce94ac36 100644 (file)
@@ -47,8 +47,10 @@ bool ScheduledDowntime::EvaluateApplyRuleInstance(const Checkable::Ptr& checkabl
 
        DebugInfo di = rule.GetDebugInfo();
 
+#ifdef _DEBUG
        Log(LogDebug, "ScheduledDowntime")
                << "Applying scheduled downtime '" << rule.GetName() << "' to object '" << checkable->GetName() << "' for rule " << di;
+#endif /* _DEBUG */
 
        ConfigItemBuilder::Ptr builder = new ConfigItemBuilder(di);
        builder->SetType("ScheduledDowntime");
@@ -121,10 +123,9 @@ bool ScheduledDowntime::EvaluateApplyRule(const Checkable::Ptr& checkable, const
                        BOOST_THROW_EXCEPTION(ScriptError("Dictionary iterator requires value to be a dictionary.", di));
 
                Array::Ptr arr = vinstances;
-               Array::Ptr arrclone = arr->ShallowClone();
 
-               ObjectLock olock(arrclone);
-               BOOST_FOREACH(const Value& instance, arrclone) {
+               ObjectLock olock(arr);
+               BOOST_FOREACH(const Value& instance, arr) {
                        String name = rule.GetName();
 
                        if (!rule.GetFKVar().IsEmpty()) {
index 24a4247818628a85b9d5219c033a3ae75325fe83..6fa49566472ce1afe7e3d5031e0452b1a902f3d4 100644 (file)
@@ -46,8 +46,10 @@ bool Service::EvaluateApplyRuleInstance(const Host::Ptr& host, const String& nam
 
        DebugInfo di = rule.GetDebugInfo();
 
+#ifdef _DEBUG
        Log(LogDebug, "Service")
            << "Applying service '" << name << "' to host '" << host->GetName() << "' for rule " << di;
+#endif /* _DEBUG */
 
        ConfigItemBuilder::Ptr builder = new ConfigItemBuilder(di);
        builder->SetType("Service");
@@ -109,10 +111,9 @@ bool Service::EvaluateApplyRule(const Host::Ptr& host, const ApplyRule& rule)
                        BOOST_THROW_EXCEPTION(ScriptError("Dictionary iterator requires value to be a dictionary.", di));
 
                Array::Ptr arr = vinstances;
-               Array::Ptr arrclone = arr->ShallowClone();
 
-               ObjectLock olock(arrclone);
-               BOOST_FOREACH(const Value& instance, arrclone) {
+               ObjectLock olock(arr);
+               BOOST_FOREACH(const Value& instance, arr) {
                        String name = rule.GetName();
 
                        if (!rule.GetFKVar().IsEmpty()) {
index 1cb904f1784bc6a0574ad1cee0faa2e0b9dc236c..dfd94c8f7881a6943ca87d700b26fbadfb7f441e 100644 (file)
@@ -97,13 +97,13 @@ void Service::OnAllConfigLoaded(void)
 
 void Service::CreateChildObjects(const Type::Ptr& childType)
 {
-       if (childType->GetName() == "ScheduledDowntime")
+       if (childType == ScheduledDowntime::TypeInstance)
                ScheduledDowntime::EvaluateApplyRules(this);
 
-       if (childType->GetName() == "Notification")
+       if (childType == Notification::TypeInstance)
                Notification::EvaluateApplyRules(this);
 
-       if (childType->GetName() == "Dependency")
+       if (childType == Dependency::TypeInstance)
                Dependency::EvaluateApplyRules(this);
 }
 
index 9899f6d34086cb26e3f1b8f0b6227086d3a7f925..43b8f344730dba689b8a050bf8141d72d41dda38 100644 (file)
@@ -51,7 +51,9 @@ void TimePeriod::Start(bool runtimeCreated)
        /* Pre-fill the time period for the next 24 hours. */
        double now = Utility::GetTime();
        UpdateRegion(now, now + 24 * 3600, true);
+#ifdef _DEBUG
        Dump();
+#endif /* _DEBUG */
 }
 
 void TimePeriod::AddSegment(double begin, double end)
@@ -176,7 +178,9 @@ void TimePeriod::RemoveSegment(double begin, double end)
 
        SetSegments(newSegments);
 
+#ifdef _DEBUG
        Dump();
+#endif /* _DEBUG */
 }
 
 void TimePeriod::RemoveSegment(const Dictionary::Ptr& segment)
@@ -351,7 +355,9 @@ void TimePeriod::UpdateTimerHandler(void)
                }
 
                tp->UpdateRegion(valid_end, now + 24 * 3600, false);
+#ifdef _DEBUG
                tp->Dump();
+#endif /* _DEBUG */
        }
 }