From 7879c09789c759774b0484bab2303485897b5066 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Thu, 1 Sep 2016 07:41:41 +0200 Subject: [PATCH] Avoid unnecessary dictionary lookups refs #12555 --- lib/base/dictionary.cpp | 5 +++++ lib/base/dictionary.hpp | 1 + lib/base/object.cpp | 16 ++++++++++++++++ lib/base/object.hpp | 1 + lib/config/expression.cpp | 6 +++--- 5 files changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/base/dictionary.cpp b/lib/base/dictionary.cpp index e3b348fa6..2c7cba668 100644 --- a/lib/base/dictionary.cpp +++ b/lib/base/dictionary.cpp @@ -228,3 +228,8 @@ bool Dictionary::HasOwnField(const String& field) const { return Contains(field); } + +bool Dictionary::GetOwnField(const String& field, Value *result) const +{ + return Get(field, result); +} diff --git a/lib/base/dictionary.hpp b/lib/base/dictionary.hpp index edacaa244..f5b524753 100644 --- a/lib/base/dictionary.hpp +++ b/lib/base/dictionary.hpp @@ -121,6 +121,7 @@ public: virtual Value GetFieldByName(const String& field, bool sandboxed, const DebugInfo& debugInfo) const override; virtual void SetFieldByName(const String& field, const Value& value, const DebugInfo& debugInfo) override; virtual bool HasOwnField(const String& field) const override; + virtual bool GetOwnField(const String& field, Value *result) const override; private: std::map m_Data; /**< The data for the dictionary. */ diff --git a/lib/base/object.cpp b/lib/base/object.cpp index 3ac220df3..a93aad774 100644 --- a/lib/base/object.cpp +++ b/lib/base/object.cpp @@ -109,6 +109,22 @@ bool Object::HasOwnField(const String& field) const return type->GetFieldId(field) != -1; } +bool Object::GetOwnField(const String& field, Value *result) const +{ + Type::Ptr type = GetReflectionType(); + + if (!type) + return false; + + int tid = type->GetFieldId(field); + + if (tid == -1) + return false; + + *result = GetField(tid); + return true; +} + Value Object::GetFieldByName(const String& field, bool sandboxed, const DebugInfo& debugInfo) const { Type::Ptr type = GetReflectionType(); diff --git a/lib/base/object.hpp b/lib/base/object.hpp index 942764c25..595773a4e 100644 --- a/lib/base/object.hpp +++ b/lib/base/object.hpp @@ -127,6 +127,7 @@ public: virtual Value GetFieldByName(const String& field, bool sandboxed, const DebugInfo& debugInfo) const; virtual void SetFieldByName(const String& field, const Value& value, const DebugInfo& debugInfo); virtual bool HasOwnField(const String& field) const; + virtual bool GetOwnField(const String& field, Value *result) const; virtual void ValidateField(int id, const Value& value, const ValidationUtils& utils); virtual void NotifyField(int id, const Value& cookie = Empty); virtual Object::Ptr NavigateField(int id) const; diff --git a/lib/config/expression.cpp b/lib/config/expression.cpp index 0930a9129..8b23b67e5 100644 --- a/lib/config/expression.cpp +++ b/lib/config/expression.cpp @@ -121,8 +121,8 @@ ExpressionResult VariableExpression::DoEvaluate(ScriptFrame& frame, DebugHint *d if (frame.Locals && frame.Locals->Get(m_Variable, &value)) return value; - else if (frame.Self.IsObject() && frame.Locals != static_cast(frame.Self) && static_cast(frame.Self)->HasOwnField(m_Variable)) - return VMOps::GetField(frame.Self, m_Variable, frame.Sandboxed, m_DebugInfo); + else if (frame.Self.IsObject() && frame.Locals != frame.Self.Get() && frame.Self.Get()->GetOwnField(m_Variable, &value)) + return value; else if (VMOps::FindVarImport(frame, m_Variable, &value, m_DebugInfo)) return value; else @@ -138,7 +138,7 @@ bool VariableExpression::GetReference(ScriptFrame& frame, bool init_dict, Value if (dhint) *dhint = NULL; - } else if (frame.Self.IsObject() && frame.Locals != static_cast(frame.Self) && static_cast(frame.Self)->HasOwnField(m_Variable)) { + } else if (frame.Self.IsObject() && frame.Locals != frame.Self.Get() && frame.Self.Get()->HasOwnField(m_Variable)) { *parent = frame.Self; if (dhint && *dhint) -- 2.40.0