From: Gunnar Beutner Date: Mon, 18 Dec 2017 09:30:20 +0000 (+0100) Subject: Avoid unnecessary allocations for script frames X-Git-Tag: v2.9.0~279^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4866f777ccb95ccef9d754fe9e4105b84141c0af;p=icinga2 Avoid unnecessary allocations for script frames --- diff --git a/lib/base/function.cpp b/lib/base/function.cpp index cb34d835f..da2302f6d 100644 --- a/lib/base/function.cpp +++ b/lib/base/function.cpp @@ -38,14 +38,13 @@ Function::Function(const String& name, const Callback& function, const std::vect Value Function::Invoke(const std::vector& arguments) { - ScriptFrame frame; + ScriptFrame frame(false); return m_Callback(arguments); } Value Function::InvokeThis(const Value& otherThis, const std::vector& arguments) { - ScriptFrame frame; - frame.Self = otherThis; + ScriptFrame frame(otherThis, false); return m_Callback(arguments); } diff --git a/lib/base/scriptframe.cpp b/lib/base/scriptframe.cpp index 3d41c4ec8..1576a80b5 100644 --- a/lib/base/scriptframe.cpp +++ b/lib/base/scriptframe.cpp @@ -40,14 +40,14 @@ INITIALIZE_ONCE_WITH_PRIORITY([]() { ScriptFrame::AddImport(deprecatedNS); }, 50); -ScriptFrame::ScriptFrame(void) - : Locals(new Dictionary()), Self(ScriptGlobal::GetGlobals()), Sandboxed(false), Depth(0) +ScriptFrame::ScriptFrame(bool allocLocals) + : Locals(allocLocals ? new Dictionary() : nullptr), Self(ScriptGlobal::GetGlobals()), Sandboxed(false), Depth(0) { InitializeFrame(); } -ScriptFrame::ScriptFrame(const Value& self) - : Locals(new Dictionary()), Self(self), Sandboxed(false), Depth(0) +ScriptFrame::ScriptFrame(const Value& self, bool allocLocals) + : Locals(allocLocals ? new Dictionary() : nullptr), Self(self), Sandboxed(false), Depth(0) { InitializeFrame(); } diff --git a/lib/base/scriptframe.hpp b/lib/base/scriptframe.hpp index 25e71823d..06fdad0fd 100644 --- a/lib/base/scriptframe.hpp +++ b/lib/base/scriptframe.hpp @@ -36,8 +36,8 @@ struct I2_BASE_API ScriptFrame bool Sandboxed; int Depth; - ScriptFrame(void); - ScriptFrame(const Value& self); + ScriptFrame(bool allocLocals = true); + ScriptFrame(const Value& self, bool allocLocals = true); ~ScriptFrame(void); void IncreaseStackDepth(void); diff --git a/lib/config/vmops.hpp b/lib/config/vmops.hpp index 5f0c27eac..97c5d6358 100644 --- a/lib/config/vmops.hpp +++ b/lib/config/vmops.hpp @@ -119,6 +119,8 @@ public: ScriptFrame *frame = ScriptFrame::GetCurrentFrame(); + frame->Locals = new Dictionary(); + if (evaluatedClosedVars) evaluatedClosedVars->CopyTo(frame->Locals);