From cef5e9d385d0cf78e43e8ea41edf253b78f0ca13 Mon Sep 17 00:00:00 2001 From: Arnaud Vrac Date: Fri, 24 Jul 2015 15:59:33 +0200 Subject: QQmlListModel: Optimize model updates Only update changed indices in the list model on insertion, deletion or move. This can greatly improves performance when inserting or removing near the end of the list, or when moving elements in a large model. On a work in progress version of qmlbench's modelperf_listmodel_addremove.qml, this appears to improve performance quite a bit (~40%): Before: Average: 363.4 frames; using samples; MedianAll=363; StdDev=5.31977, CoV=0.0146389 After: Average: 518.2 frames; using samples; MedianAll=518; StdDev=20.5475, CoV=0.0396517 Change-Id: I8ce7bea4ab8b1c7c3d0fbccc40b9a12ca3eadf59 Reviewed-by: Robin Burchell Reviewed-by: Shawn Rutledge --- src/qml/types/qqmllistmodel.cpp | 15 ++++++++++----- src/qml/types/qqmllistmodel_p_p.h | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/qml/types/qqmllistmodel.cpp b/src/qml/types/qqmllistmodel.cpp index 06caa77bb4..eebf0f74d8 100644 --- a/src/qml/types/qqmllistmodel.cpp +++ b/src/qml/types/qqmllistmodel.cpp @@ -359,7 +359,7 @@ int ListModel::appendElement() void ListModel::insertElement(int index) { newElement(index); - updateCacheIndices(); + updateCacheIndices(index); } void ListModel::move(int from, int to, int n) @@ -381,7 +381,7 @@ void ListModel::move(int from, int to, int n) for (int i=0 ; i < store.count() ; ++i) elements[from+i] = store[i]; - updateCacheIndices(); + updateCacheIndices(from, to + n); } void ListModel::newElement(int index) @@ -390,9 +390,14 @@ void ListModel::newElement(int index) elements.insert(index, e); } -void ListModel::updateCacheIndices() +void ListModel::updateCacheIndices(int start, int end) { - for (int i=0 ; i < elements.count() ; ++i) { + int count = elements.count(); + + if (end < 0 || end > count) + end = count; + + for (int i = start; i < end; ++i) { ListElement *e = elements.at(i); if (ModelNodeMetaObject *mo = e->objectCache()) mo->m_elementIndex = i; @@ -574,7 +579,7 @@ void ListModel::remove(int index, int count) delete elements[index+i]; } elements.remove(index, count); - updateCacheIndices(); + updateCacheIndices(index); } void ListModel::insert(int elementIndex, QV4::Object *object) diff --git a/src/qml/types/qqmllistmodel_p_p.h b/src/qml/types/qqmllistmodel_p_p.h index 44583df2a6..e2653c220d 100644 --- a/src/qml/types/qqmllistmodel_p_p.h +++ b/src/qml/types/qqmllistmodel_p_p.h @@ -397,7 +397,7 @@ private: void newElement(int index); - void updateCacheIndices(); + void updateCacheIndices(int start = 0, int end = -1); friend class ListElement; friend class QQmlListModelWorkerAgent; -- cgit v1.2.3