QmlJSInspector: Improve performance

Populating watchhandler can be expensive. Update
with just the necessary data.

Change-Id: I06c28e1c8f5437cc05d3f9cef139d3df9da10c81
Reviewed-by: Christiaan Janssen <christiaan.janssen@nokia.com>
This commit is contained in:
Aurindam Jana
2012-05-29 22:21:04 +02:00
parent d65ac4bf0c
commit fc1f265e87
2 changed files with 120 additions and 89 deletions

View File

@@ -62,11 +62,12 @@ QmlInspectorAgent::QmlInspectorAgent(DebuggerEngine *engine, QObject *parent)
, m_engineQueryId(0) , m_engineQueryId(0)
, m_rootContextQueryId(0) , m_rootContextQueryId(0)
, m_objectToSelect(-1) , m_objectToSelect(-1)
, m_newObjectsCreated(false)
{ {
connect(debuggerCore()->action(ShowQmlObjectTree), connect(debuggerCore()->action(ShowQmlObjectTree),
SIGNAL(valueChanged(QVariant)), SLOT(updateStatus())); SIGNAL(valueChanged(QVariant)), SLOT(updateStatus()));
m_delayQueryTimer.setSingleShot(true); m_delayQueryTimer.setSingleShot(true);
m_delayQueryTimer.setInterval(500); m_delayQueryTimer.setInterval(100);
connect(&m_delayQueryTimer, SIGNAL(timeout()), SLOT(queryEngineContext())); connect(&m_delayQueryTimer, SIGNAL(timeout()), SLOT(queryEngineContext()));
} }
@@ -229,7 +230,8 @@ ObjectReference QmlInspectorAgent::objectForName(
QHashIterator<int, QByteArray> iter(m_debugIdToIname); QHashIterator<int, QByteArray> iter(m_debugIdToIname);
while (iter.hasNext()) { while (iter.hasNext()) {
iter.next(); iter.next();
if (m_debuggerEngine->watchHandler()->findData(iter.value())->name == objectId) const WatchData *wd = m_debuggerEngine->watchHandler()->findData(iter.value());
if (wd && wd->name == objectId)
return ObjectReference(iter.key()); return ObjectReference(iter.key());
} }
} }
@@ -467,6 +469,8 @@ void QmlInspectorAgent::newObject(int engineId, int objectId, int /*parentId*/)
if (m_engine.debugId() != engineId) if (m_engine.debugId() != engineId)
return; return;
m_newObjectsCreated = true;
if (m_engineClient->objectName() == QmlDebug::Constants::QML_DEBUGGER) if (m_engineClient->objectName() == QmlDebug::Constants::QML_DEBUGGER)
fetchObject(objectId); fetchObject(objectId);
else else
@@ -530,6 +534,7 @@ void QmlInspectorAgent::queryEngineContext()
m_rootContextQueryId m_rootContextQueryId
= m_engineClient->queryRootContexts(m_engine); = m_engineClient->queryRootContexts(m_engine);
m_newObjectsCreated = true;
} }
void QmlInspectorAgent::fetchObject(int debugId) void QmlInspectorAgent::fetchObject(int debugId)
@@ -608,59 +613,73 @@ void QmlInspectorAgent::objectTreeFetched(const ObjectReference &object)
m_objectStack.push(object); m_objectStack.push(object);
if (m_engineClient->objectName() == QmlDebug::Constants::QML_DEBUGGER) { if (m_objectTreeQueryIds.count()) {
if (object.parentId() != -1 && !m_debugIdToIname.contains(object.parentId())) {
fetchObject(object.parentId());
return;
}
} else if (m_objectTreeQueryIds.count()) {
return; return;
} }
QElapsedTimer timeElapsed;
// sync tree with watchhandler // sync tree with watchhandler
QList<WatchData> watchData; QList<WatchData> watchData;
ObjectReference last; ObjectReference last;
if (m_engineClient->objectName() == QmlDebug::Constants::QML_DEBUGGER) { QStack<QmlDebug::ObjectReference> stack;
while (!m_objectStack.isEmpty()) {
last = m_objectStack.pop(); // 4.x
QByteArray parentIname; if (m_newObjectsCreated && m_engineClient->objectName() != QmlDebug::Constants::QML_DEBUGGER) {
if (last.parentId() != -1) { // We need to reverse the stack as the root objects
QTC_ASSERT(m_debugIdToIname.contains(last.parentId()), return); // are pushed to the bottom since they are fetched first.
parentIname = m_debugIdToIname.value(last.parentId()); // The child objects need to placed in the correct position and therefore
} // the m_debugIdChildIds needs to be populated first.
watchData.append(buildWatchData(last, parentIname)); while (!m_objectStack.isEmpty())
buildDebugIdHashRecursive(last); stack.push(m_objectStack.pop());
}
} else { } else {
QVectorIterator<ObjectReference> iter(m_objectStack); stack = m_objectStack;
while (iter.hasNext()) { }
const ObjectReference &r = iter.next();
int pid = -1; while (!stack.isEmpty()) {
last = stack.pop();
int parentId = last.parentId();
QByteArray parentIname;
// 4.x
if (m_engineClient->objectName() != QmlDebug::Constants::QML_DEBUGGER) {
QHashIterator<int, QList<int> > i(m_debugIdChildIds); QHashIterator<int, QList<int> > i(m_debugIdChildIds);
while (i.hasNext()) { while (i.hasNext()) {
QList<int> cids = i.value(); i.next();
foreach (int cid, cids) { if (i.value().contains(last.debugId())) {
if (cid == r.debugId()) { parentId = i.key();
pid = i.key();
break; break;
} }
} }
if (pid != -1)
break;
} }
QByteArray parentIname; if (m_debugIdToIname.contains(parentId))
if (m_debugIdToIname.contains(pid)) parentIname = m_debugIdToIname.value(parentId);
parentIname = m_debugIdToIname.value(r.parentId()); if (!m_newObjectsCreated && parentId != -1 && parentIname.isEmpty()) {
watchData.append(buildWatchData(r, parentIname)); fetchObject(parentId);
buildDebugIdHashRecursive(r); return;
} }
if (debug)
timeElapsed.start();
watchData.append(buildWatchData(last, parentIname, true));
if (debug)
qDebug() << __FUNCTION__ << "Time: Build Watch Data took "
<< timeElapsed.elapsed() << " ms";
if (debug)
timeElapsed.start();
buildDebugIdHashRecursive(last);
if (debug)
qDebug() << __FUNCTION__ << "Time: Build Debug Id Hash took "
<< timeElapsed.elapsed() << " ms";
}
m_newObjectsCreated = false;
m_objectStack.clear(); m_objectStack.clear();
}
if (debug)
timeElapsed.start();
m_debuggerEngine->watchHandler()->insertData(watchData); m_debuggerEngine->watchHandler()->insertData(watchData);
emit objectTreeUpdated(); if (debug)
qDebug() << __FUNCTION__ << "Time: Insertion took " << timeElapsed.elapsed() << " ms";
if (m_engineClient->objectName() == QmlDebug::Constants::QML_DEBUGGER) { emit objectTreeUpdated();
emit objectFetched(last); emit objectFetched(last);
if (m_objectToSelect == last.debugId()) { if (m_objectToSelect == last.debugId()) {
@@ -672,7 +691,6 @@ void QmlInspectorAgent::objectTreeFetched(const ObjectReference &object)
m_objectToSelect = -1; m_objectToSelect = -1;
} }
} }
}
void QmlInspectorAgent::buildDebugIdHashRecursive(const ObjectReference &ref) void QmlInspectorAgent::buildDebugIdHashRecursive(const ObjectReference &ref)
{ {
@@ -702,6 +720,18 @@ void QmlInspectorAgent::buildDebugIdHashRecursive(const ObjectReference &ref)
if (!m_debugIdHash[file][location].contains(ref.debugId())) if (!m_debugIdHash[file][location].contains(ref.debugId()))
m_debugIdHash[file][location].append(ref.debugId()); m_debugIdHash[file][location].append(ref.debugId());
m_debugIdLocations.insert(ref.debugId(), FileReference(filePath, lineNum, colNum)); m_debugIdLocations.insert(ref.debugId(), FileReference(filePath, lineNum, colNum));
// 4.x
if (m_newObjectsCreated && m_engineClient->objectName() != QmlDebug::Constants::QML_DEBUGGER) {
QList<int> childIds;
foreach (const ObjectReference &c, ref.children()) {
childIds << c.debugId();
}
// For 4.x, we do not get the parentId. Hence, store the child ids
// to look up correct insertion places later
m_debugIdChildIds.insert(ref.debugId(), childIds);
}
foreach (const ObjectReference &it, ref.children()) foreach (const ObjectReference &it, ref.children())
buildDebugIdHashRecursive(it); buildDebugIdHashRecursive(it);
} }
@@ -719,13 +749,18 @@ static QByteArray buildIName(const QByteArray &parentIname, const QString &name)
} }
QList<WatchData> QmlInspectorAgent::buildWatchData(const ObjectReference &obj, QList<WatchData> QmlInspectorAgent::buildWatchData(const ObjectReference &obj,
const QByteArray &parentIname) const QByteArray &parentIname,
bool append)
{ {
if (debug) if (debug)
qDebug() << __FUNCTION__ << "(" << obj << parentIname << ")"; qDebug() << __FUNCTION__ << "(" << obj << parentIname << ")";
QList<WatchData> list; QList<WatchData> list;
int objDebugId = obj.debugId();
QByteArray objIname = buildIName(parentIname, objDebugId);
if (append) {
WatchData objWatch; WatchData objWatch;
QString name = obj.idString(); QString name = obj.idString();
if (name.isEmpty()) if (name.isEmpty())
@@ -735,30 +770,25 @@ QList<WatchData> QmlInspectorAgent::buildWatchData(const ObjectReference &obj,
return list; return list;
// object // object
objWatch.id = obj.debugId(); objWatch.id = objDebugId;
objWatch.exp = name.toLatin1(); objWatch.exp = name.toLatin1();
objWatch.name = name; objWatch.name = name;
objWatch.iname = buildIName(parentIname, obj.debugId()); objWatch.iname = objIname;
objWatch.type = obj.className().toLatin1(); objWatch.type = obj.className().toLatin1();
objWatch.value = _("object"); objWatch.value = _("object");
objWatch.setHasChildren(true); objWatch.setHasChildren(true);
objWatch.setAllUnneeded(); objWatch.setAllUnneeded();
list.append(objWatch); list.append(objWatch);
m_debugIdToIname.insert(objWatch.id, objWatch.iname);
addObjectWatch(objWatch.id); addObjectWatch(objWatch.id);
if (m_engineClient->objectName() != QmlDebug::Constants::QML_DEBUGGER) {
QList<int> childIds;
foreach (const ObjectReference &c, obj.children()) {
childIds << c.debugId();
}
// For 4.x, we do not get the parentId. Hence, store the child ids
// to look up correct insertion places later
m_debugIdChildIds.insert(objWatch.id, childIds);
} }
if (!m_debuggerEngine->watchHandler()->isExpandedIName(objWatch.iname) // To improve performance, we do not insert data for items
// that have not been previously queried when the object tree is refreshed.
if (!m_debuggerEngine->watchHandler()->isExpandedIName(objIname) && m_newObjectsCreated)
append = false;
if (!m_debuggerEngine->watchHandler()->isExpandedIName(objIname)
&& obj.needsMoreData()) { && obj.needsMoreData()) {
// we don't know the children yet. Not adding the 'properties' // we don't know the children yet. Not adding the 'properties'
// element makes sure we're queried on expansion. // element makes sure we're queried on expansion.
@@ -766,12 +796,12 @@ QList<WatchData> QmlInspectorAgent::buildWatchData(const ObjectReference &obj,
} }
// properties // properties
if (obj.properties().count()) { if (append && obj.properties().count()) {
WatchData propertiesWatch; WatchData propertiesWatch;
propertiesWatch.id = objWatch.id; propertiesWatch.id = objDebugId;
propertiesWatch.exp = ""; propertiesWatch.exp = "";
propertiesWatch.name = tr("properties"); propertiesWatch.name = tr("properties");
propertiesWatch.iname = objWatch.iname + ".[properties]"; propertiesWatch.iname = objIname + ".[properties]";
propertiesWatch.type = ""; propertiesWatch.type = "";
propertiesWatch.value = _("list"); propertiesWatch.value = _("list");
propertiesWatch.setHasChildren(true); propertiesWatch.setHasChildren(true);
@@ -781,7 +811,7 @@ QList<WatchData> QmlInspectorAgent::buildWatchData(const ObjectReference &obj,
foreach (const PropertyReference &property, obj.properties()) { foreach (const PropertyReference &property, obj.properties()) {
WatchData propertyWatch; WatchData propertyWatch;
propertyWatch.id = objWatch.id; propertyWatch.id = objDebugId;
propertyWatch.exp = property.name().toLatin1(); propertyWatch.exp = property.name().toLatin1();
propertyWatch.name = property.name(); propertyWatch.name = property.name();
propertyWatch.iname = buildIName(propertiesWatch.iname, property.name()); propertyWatch.iname = buildIName(propertiesWatch.iname, property.name());
@@ -791,11 +821,12 @@ QList<WatchData> QmlInspectorAgent::buildWatchData(const ObjectReference &obj,
propertyWatch.setHasChildren(false); propertyWatch.setHasChildren(false);
list.append(propertyWatch); list.append(propertyWatch);
} }
m_debugIdToIname.insert(objDebugId, objIname);
} }
// recurse // recurse
foreach (const ObjectReference &child, obj.children()) foreach (const ObjectReference &child, obj.children())
list.append(buildWatchData(child, objWatch.iname)); list.append(buildWatchData(child, objIname, append));
return list; return list;
} }

View File

@@ -58,7 +58,6 @@ class QmlInspectorAgent : public QObject
public: public:
explicit QmlInspectorAgent(DebuggerEngine *engine, QObject *parent = 0); explicit QmlInspectorAgent(DebuggerEngine *engine, QObject *parent = 0);
void fetchObject(int debugId); void fetchObject(int debugId);
quint32 queryExpressionResult(int debugId, const QString &expression); quint32 queryExpressionResult(int debugId, const QString &expression);
@@ -118,7 +117,7 @@ private:
void buildDebugIdHashRecursive(const QmlDebug::ObjectReference &ref); void buildDebugIdHashRecursive(const QmlDebug::ObjectReference &ref);
QList<WatchData> buildWatchData(const QmlDebug::ObjectReference &obj, QList<WatchData> buildWatchData(const QmlDebug::ObjectReference &obj,
const QByteArray &parentIname); const QByteArray &parentIname, bool append);
enum LogDirection { enum LogDirection {
LogSend, LogSend,
@@ -147,6 +146,7 @@ private:
QList<int> m_objectWatches; QList<int> m_objectWatches;
QList<int> m_fetchDataIds; QList<int> m_fetchDataIds;
QTimer m_delayQueryTimer; QTimer m_delayQueryTimer;
bool m_newObjectsCreated;
}; };
} // Internal } // Internal