VisualDataModel performance improvements.
authorAndrew den Exter <andrew.den-exter@nokia.com>
Wed, 21 Dec 2011 04:23:41 +0000 (14:23 +1000)
committerQt by Nokia <qt-info@nokia.com>
Fri, 23 Dec 2011 06:23:55 +0000 (07:23 +0100)
Avoid a linear scan of all cache items and associated accesses by
getting the cache item from an objects  vdm attached object instead.

Make the model context property a property of the context object instead
of a separate property on the context object.

Parent the vdm attached object to the delegate object with
QDeclarative_setParent_noEvent instead of passing it in the constructor.

Change-Id: Ib77c5cdb963f3dfe8f2bdef039e010a6bb30140f
Reviewed-by: Martin Jones <martin.jones@nokia.com>
src/quick/items/qquickvisualdatamodel.cpp
src/quick/items/qquickvisualdatamodel_p.h
src/quick/items/qquickvisualdatamodel_p_p.h

index b029443..379a42c 100644 (file)
@@ -418,9 +418,8 @@ QQuickVisualDataModel::ReleaseFlags QQuickVisualDataModelPrivate::release(QObjec
     if (!object)
         return stat;
 
-    int cacheIndex = cacheIndexOf(object);
-    if (cacheIndex != -1) {
-        QQuickVisualDataModelItem *cacheItem = m_cache.at(cacheIndex);
+    if (QQuickVisualDataModelAttached *attached = QQuickVisualDataModelAttached::properties(object)) {
+        QQuickVisualDataModelItem *cacheItem = attached->m_cacheItem;
         if (cacheItem->releaseObject()) {
             destroy(object);
             if (QQuickItem *item = qobject_cast<QQuickItem *>(object))
@@ -782,7 +781,6 @@ QObject *QQuickVisualDataModelPrivate::object(Compositor::Group group, int index
             }
         }
 
-        ctxt->setContextProperty(QLatin1String("model"), cacheItem);
         ctxt->setContextObject(cacheItem);
 
         incubator->incubating = cacheItem;
@@ -844,22 +842,12 @@ QString QQuickVisualDataModel::stringValue(int index, const QString &name)
     return d->stringValue(d->m_compositorGroup, index, name);
 }
 
-int QQuickVisualDataModelPrivate::cacheIndexOf(QObject *object) const
-{
-    for (int cacheIndex = 0; cacheIndex < m_cache.count(); ++cacheIndex) {
-        if (m_cache.at(cacheIndex)->object == object)
-            return cacheIndex;
-    }
-    return -1;
-}
-
 int QQuickVisualDataModel::indexOf(QQuickItem *item, QObject *) const
 {
     Q_D(const QQuickVisualDataModel);
-    const int cacheIndex = d->cacheIndexOf(item);
-    return cacheIndex != -1
-            ? d->m_cache.at(cacheIndex)->index[d->m_compositorGroup]
-            : -1;
+    if (QQuickVisualDataModelAttached *attached = QQuickVisualDataModelAttached::properties(item))
+        return attached->m_cacheItem->index[d->m_compositorGroup];
+    return -1;
 }
 
 void QQuickVisualDataModel::setWatchedRoles(QList<QByteArray> roles)
@@ -2555,11 +2543,8 @@ int QQuickVisualPartsModel::indexOf(QQuickItem *item, QObject *) const
 {
     QHash<QObject *, QDeclarativePackage *>::const_iterator it = m_packaged.find(item);
     if (it != m_packaged.end()) {
-        const QQuickVisualDataModelPrivate *model = QQuickVisualDataModelPrivate::get(m_model);
-        const int cacheIndex = model->cacheIndexOf(*it);
-        return cacheIndex != -1
-                ? model->m_cache.at(cacheIndex)->index[m_compositorGroup]
-                : -1;
+        if (QQuickVisualDataModelAttached *attached = QQuickVisualDataModelAttached::properties(*it))
+            return attached->m_cacheItem->index[m_compositorGroup];
     }
     return -1;
 }
index aecf5e3..6cb028f 100644 (file)
 #include <private/qdeclarativelistcompositor_p.h>
 #include <private/qquickvisualitemmodel_p.h>
 
-
 #include <QtCore/qabstractitemmodel.h>
 #include <QtCore/qstringlist.h>
 
+
 #include <private/qv8engine_p.h>
+#include <private/qdeclarativeglobal_p.h>
 
 QT_BEGIN_HEADER
 
@@ -190,11 +191,12 @@ class QQuickVisualDataModelAttached : public QObject
     Q_PROPERTY(bool isUnresolved READ isUnresolved NOTIFY unresolvedChanged)
 public:
     QQuickVisualDataModelAttached(QObject *parent)
-        : QObject(parent)
-        , m_cacheItem(0)
+        : m_cacheItem(0)
         , m_previousGroups(0)
         , m_modelChanged(false)
-    {}
+    {
+        QDeclarative_setParent_noEvent(this, parent);
+    }
     ~QQuickVisualDataModelAttached() { attachedProperties.remove(parent()); }
 
     void setCacheItem(QQuickVisualDataModelItem *item);
index 03d9767..7565192 100644 (file)
@@ -102,6 +102,7 @@ class QQuickVisualDataModelItem : public QObject, public QV8ObjectResource
 {
     Q_OBJECT
     Q_PROPERTY(int index READ modelIndex NOTIFY modelIndexChanged)
+    Q_PROPERTY(QObject *model READ modelObject CONSTANT)
     V8_RESOURCE_TYPE(VisualDataItemType)
 public:
     QQuickVisualDataModelItem(
@@ -120,6 +121,8 @@ public:
 
     void Dispose();
 
+    QObject *modelObject() { return this; }
+
     int modelIndex() const { return index[0]; }
     void setModelIndex(int idx) { index[0] = idx; emit modelIndexChanged(); }
 
@@ -231,7 +234,6 @@ public:
     void destroy(QObject *object);
     QQuickVisualDataModel::ReleaseFlags release(QObject *object);
     QString stringValue(Compositor::Group group, int index, const QString &name);
-    int cacheIndexOf(QObject *object) const;
     void emitCreatedPackage(QQuickVisualDataModelItem *cacheItem, QDeclarativePackage *package);
     void emitInitPackage(QQuickVisualDataModelItem *cacheItem, QDeclarativePackage *package);
     void emitCreatedItem(QQuickVisualDataModelItem *cacheItem, QQuickItem *item) {