diff options
author | Arnaud Vrac <avrac@freebox.fr> | 2015-07-24 15:59:33 +0200 |
---|---|---|
committer | Arnaud Vrac <avrac@freebox.fr> | 2017-05-05 11:16:45 +0000 |
commit | cef5e9d385d0cf78e43e8ea41edf253b78f0ca13 (patch) | |
tree | ff17e42290644e66fa16f51179b2bffa43c112aa /src/qml/types/qqmllistmodel.cpp | |
parent | 4be38abab97f07b999ca3b2da3c45c5423d83dc0 (diff) |
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 <robin.burchell@crimson.no>
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
Diffstat (limited to 'src/qml/types/qqmllistmodel.cpp')
-rw-r--r-- | src/qml/types/qqmllistmodel.cpp | 15 |
1 files changed, 10 insertions, 5 deletions
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) |