From 50bd267a0b7623f5310b4d3bba6dcccbfb815179 Mon Sep 17 00:00:00 2001 From: Andrew den Exter Date: Thu, 5 Apr 2012 14:37:46 +1000 Subject: [PATCH] Optimise QQuickItem::stackAfter/Before() for the common case. 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 --- src/quick/items/qquickitem.cpp | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/src/quick/items/qquickitem.cpp b/src/quick/items/qquickitem.cpp index 47bfc98..0dd04e8 100644 --- a/src/quick/items/qquickitem.cpp +++ b/src/quick/items/qquickitem.cpp @@ -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(sibling)); + int myIndex = parentPrivate->childItems.lastIndexOf(this); + int siblingIndex = parentPrivate->childItems.lastIndexOf(const_cast(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(sibling)); + int myIndex = parentPrivate->childItems.lastIndexOf(this); + int siblingIndex = parentPrivate->childItems.lastIndexOf(const_cast(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); -- 2.7.4