Optimise QQuickItem::stackAfter/Before() for the common case.
authorAndrew den Exter <andrew.den-exter@nokia.com>
Thu, 5 Apr 2012 04:37:46 +0000 (14:37 +1000)
committerQt by Nokia <qt-info@nokia.com>
Fri, 25 May 2012 09:51:50 +0000 (11:51 +0200)
These functions are most often used to position items just given a new
parent, meaning one item is very likely to be at the end of the
child list, and when appending multiple items the other is also likely
to be near the end.  When this is true we can save a lot of time
spent iterating to the end of the list by doing a reverse search.

Change-Id: If16d6d19689a6140d3d9a873857776cf79666910
Reviewed-by: Martin Jones <martin.jones@nokia.com>
src/quick/items/qquickitem.cpp

index 47bfc98..0dd04e8 100644 (file)
@@ -2083,19 +2083,15 @@ void QQuickItem::stackBefore(const QQuickItem *sibling)
 
     QQuickItemPrivate *parentPrivate = QQuickItemPrivate::get(d->parentItem);
 
-    int myIndex = parentPrivate->childItems.indexOf(this);
-    int siblingIndex = parentPrivate->childItems.indexOf(const_cast<QQuickItem *>(sibling));
+    int myIndex = parentPrivate->childItems.lastIndexOf(this);
+    int siblingIndex = parentPrivate->childItems.lastIndexOf(const_cast<QQuickItem *>(sibling));
 
     Q_ASSERT(myIndex != -1 && siblingIndex != -1);
 
     if (myIndex == siblingIndex - 1)
         return;
 
-    parentPrivate->childItems.removeAt(myIndex);
-
-    if (myIndex < siblingIndex) --siblingIndex;
-
-    parentPrivate->childItems.insert(siblingIndex, this);
+    parentPrivate->childItems.move(myIndex, myIndex < siblingIndex ? siblingIndex - 1 : siblingIndex);
 
     parentPrivate->dirty(QQuickItemPrivate::ChildrenStackingChanged);
     parentPrivate->markSortedChildrenDirty(this);
@@ -2114,19 +2110,15 @@ void QQuickItem::stackAfter(const QQuickItem *sibling)
 
     QQuickItemPrivate *parentPrivate = QQuickItemPrivate::get(d->parentItem);
 
-    int myIndex = parentPrivate->childItems.indexOf(this);
-    int siblingIndex = parentPrivate->childItems.indexOf(const_cast<QQuickItem *>(sibling));
+    int myIndex = parentPrivate->childItems.lastIndexOf(this);
+    int siblingIndex = parentPrivate->childItems.lastIndexOf(const_cast<QQuickItem *>(sibling));
 
     Q_ASSERT(myIndex != -1 && siblingIndex != -1);
 
     if (myIndex == siblingIndex + 1)
         return;
 
-    parentPrivate->childItems.removeAt(myIndex);
-
-    if (myIndex < siblingIndex) --siblingIndex;
-
-    parentPrivate->childItems.insert(siblingIndex + 1, this);
+    parentPrivate->childItems.move(myIndex, myIndex > siblingIndex ? siblingIndex + 1 : siblingIndex);
 
     parentPrivate->dirty(QQuickItemPrivate::ChildrenStackingChanged);
     parentPrivate->markSortedChildrenDirty(this);