Avoid unnecessary copies for the DebugHint class

refs #12509
This commit is contained in:
Gunnar Beutner 2016-08-27 18:43:14 +02:00
parent 0d3563ddaa
commit eafe4c578d
3 changed files with 39 additions and 32 deletions

View File

@ -50,6 +50,10 @@ public:
inline Array(void) inline Array(void)
{ } { }
inline Array(std::initializer_list<Value> init)
: m_Data(init)
{ }
inline ~Array(void) inline ~Array(void)
{ } { }

View File

@ -460,6 +460,7 @@ ExpressionResult FunctionCallExpression::DoEvaluate(ScriptFrame& frame, DebugHin
ExpressionResult ArrayExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const ExpressionResult ArrayExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
{ {
Array::Ptr result = new Array(); Array::Ptr result = new Array();
result->Reserve(m_Expressions.size());
for (Expression *aexpr : m_Expressions) { for (Expression *aexpr : m_Expressions) {
ExpressionResult element = aexpr->Evaluate(frame); ExpressionResult element = aexpr->Evaluate(frame);

View File

@ -41,37 +41,29 @@ public:
: m_Hints(hints) : m_Hints(hints)
{ } { }
DebugHint(Dictionary::Ptr&& hints)
: m_Hints(std::move(hints))
{ }
inline void AddMessage(const String& message, const DebugInfo& di) inline void AddMessage(const String& message, const DebugInfo& di)
{ {
Array::Ptr amsg = new Array(); GetMessages()->Add(new Array({ message, di.Path, di.FirstLine, di.FirstColumn, di.LastLine, di.LastColumn }));
{
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);
} }
inline DebugHint GetChild(const String& name) inline DebugHint GetChild(const String& name)
{ {
Dictionary::Ptr children = GetChildren(); const Dictionary::Ptr& children = GetChildren();
Value vchild; Value vchild;
Dictionary::Ptr child;
if (!children->Get(name, &vchild)) { if (!children->Get(name, &vchild)) {
vchild = new Dictionary(); child = new Dictionary();
children->Set(name, vchild); children->Set(name, child);
} } else
child = vchild;
return DebugHint(vchild); return DebugHint(child);
} }
Dictionary::Ptr ToDictionary(void) const Dictionary::Ptr ToDictionary(void) const
@ -81,35 +73,45 @@ public:
private: private:
Dictionary::Ptr m_Hints; 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) if (!m_Hints)
m_Hints = new Dictionary(); m_Hints = new Dictionary();
Value vmessages; Value vmessages;
if (!m_Hints->Get("messages", &vmessages)) { if (!m_Hints->Get("messages", &vmessages)) {
vmessages = new Array(); m_Messages = new Array();
m_Hints->Set("messages", vmessages); m_Hints->Set("messages", m_Messages);
} else
m_Messages = vmessages;
return m_Messages;
} }
return vmessages; const Dictionary::Ptr& GetChildren(void)
}
Dictionary::Ptr GetChildren(void)
{ {
if (m_Children)
return m_Children;
if (!m_Hints) if (!m_Hints)
m_Hints = new Dictionary(); m_Hints = new Dictionary();
Value vchildren; Value vchildren;
if (!m_Hints->Get("properties", &vchildren)) { if (!m_Hints->Get("properties", &vchildren)) {
vchildren = new Dictionary(); m_Children = new Dictionary();
m_Hints->Set("properties", vchildren); 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) : m_Value(value), m_Code(code)
{ } { }
operator Value(void) const operator const Value&(void) const
{ {
return m_Value; return m_Value;
} }
Value GetValue(void) const const Value& GetValue(void) const
{ {
return m_Value; return m_Value;
} }