aboutsummaryrefslogtreecommitdiffstats
path: root/src/imports/layouts/qquicklinearlayout.cpp
diff options
context:
space:
mode:
authorJan Arve Sæther <jan-arve.saether@qt.io>2020-04-15 16:18:03 +0200
committerJan Arve Sæther <jan-arve.saether@qt.io>2020-05-04 15:11:12 +0200
commitcc77a0bc549ce8f0b218661e7ae0e82e5b89e0da (patch)
treed78d8902579be745ea823b10e56dcc2c4b06b89f /src/imports/layouts/qquicklinearlayout.cpp
parent788a3a183f8c49c1a88270c1456c3d47423df240 (diff)
Improve performance when dynamically adding items to a layout
This was especially noticeable when a Repeater was populating the children of a layout, and its model was dynamically changed: When the model was changed, the repeater removed one item at a time until all items were removed, then applied the new model and then added all the new items for the new model. The layout reacted to that by doing a full sync of the QML layout into the internal gridlayout engine each time an item got removed or added. For very large layouts (or layouts that have complex size hints to calculate), this caused a major slowdown. This patch fixes that by postponing the sync until we get a updatePolish(), basically replacing most calls to updateLayoutItems() (which does the sync) with a call to invalidate() (which schedules a polish). It will also get rid of some binding loop warnings due to this change. This means that there is a small change in behavior: impicitWidth, implicitHeight and Layout.{min,max}imum{Width,Height} might in some cases be incorrect until the updatePolish() have been done. (This is however consistent with how Row/Column/Grid behaves) The creation test in qmlbench was quite simple, so it did not suffer from the most severe performance issues, but we did not regress: > compareresults: auto/creation/layouts/delegates_rowlayout.qml: improvement by 3.91% auto/creation/layouts/delegates_columnlayout.qml: improvement by 6.59% auto/creation/layouts/delegates_gridlayout.qml: improvement by 1.83% Overall average of differences: 4.11% And for the gridlayout_large.qml (Repeater with 1000 dynamically changing items): > compareresults: auto/layouts/gridlayout_large.qml: improvement by 66477.78% Overall average of differences: 66477.78% [ChangeLog][Qt Quick Layouts] Performance improvements to Qt Quick Layouts. This has the small side-effect that size hints (implicitWidth/implicithHeight etc) changes are not immediately emitted after a layout has been modified (e.g item added) Pick-to: 5.15 Fixes: QTBUG-71839 Fixes: QTBUG-65121 Fixes: QTBUG-66017 Change-Id: I6922efe449134246df66b177992e4442747bc8fb Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src/imports/layouts/qquicklinearlayout.cpp')
-rw-r--r--src/imports/layouts/qquicklinearlayout.cpp86
1 files changed, 44 insertions, 42 deletions
diff --git a/src/imports/layouts/qquicklinearlayout.cpp b/src/imports/layouts/qquicklinearlayout.cpp
index 1657f63fcf..7626506c75 100644
--- a/src/imports/layouts/qquicklinearlayout.cpp
+++ b/src/imports/layouts/qquicklinearlayout.cpp
@@ -254,6 +254,7 @@ void QQuickGridLayoutBase::setOrientation(Qt::Orientation orientation)
QSizeF QQuickGridLayoutBase::sizeHint(Qt::SizeHint whichSizeHint) const
{
Q_D(const QQuickGridLayoutBase);
+ ensureLayoutItemsUpdated();
return d->engine.sizeHint(whichSizeHint, QSizeF(), d->styleInfo);
}
@@ -315,14 +316,27 @@ QQuickGridLayoutBase::~QQuickGridLayoutBase()
void QQuickGridLayoutBase::componentComplete()
{
- qCDebug(lcQuickLayouts) << objectName() << "QQuickGridLayoutBase::componentComplete()" << parent();
+ qCDebug(lcQuickLayouts) << "QQuickGridLayoutBase::componentComplete()" << this << parent();
QQuickLayout::componentComplete();
- updateLayoutItems();
+
+ /* The layout is invalid when it is constructed, but during construction of the layout and
+ its children (in the "static/from QML" case which this is trying to cover) things
+ change and as a consequence invalidate() and ensureLayoutItemsUpdated() might be called.
+ As soon as ensureLayoutItemsUpdated() is called it will set d->dirty = false.
+ However, a subsequent invalidate() will return early if the component is not completed
+ because it knows that componentComplete() will take care of doing the proper layouting
+ (so it won't set d->dirty = true). When we then call ensureLayoutItemsUpdated() again here
+ it sees that its not dirty and assumes everything up-to-date. For those cases we therefore
+ need to call invalidate() in advance
+ */
+ invalidate();
+ ensureLayoutItemsUpdated();
QQuickItem *par = parentItem();
if (qobject_cast<QQuickLayout*>(par))
return;
rearrange(QSizeF(width(), height()));
+ qCDebug(lcQuickLayouts) << "QQuickGridLayoutBase::componentComplete(). COMPLETED" << this << parent();
}
/*
@@ -342,13 +356,12 @@ void QQuickGridLayoutBase::componentComplete()
}
}
- 1. l2->invalidateChildItem(a) is called on l2, where item refers to "a".
+ 1. l2->invalidate(a) is called on l2, where item refers to "a".
(this will dirty the cached size hints of item "a")
- 2. l2->invalidate() is called
- this will :
+ 2. The layout engine will invalidate:
i) invalidate the layout engine
- ii) dirty the cached size hints of item "l2" (by calling parentLayout()->invalidateChildItem
-
+ ii) dirty the cached size hints of item "l2" (by calling parentLayout()->invalidate(l2)
+ The recursion continues to the topmost layout
*/
/*!
\internal
@@ -363,43 +376,29 @@ void QQuickGridLayoutBase::invalidate(QQuickItem *childItem)
Q_D(QQuickGridLayoutBase);
if (!isReady())
return;
+ qCDebug(lcQuickLayouts) << "QQuickGridLayoutBase::invalidate()" << this << ", invalidated:" << invalidated();
+ if (invalidated()) {
+ return;
+ }
+ qCDebug(lcQuickLayouts) << "d->m_rearranging:" << d->m_rearranging;
if (d->m_rearranging) {
d->m_invalidateAfterRearrange << childItem;
return;
}
- qCDebug(lcQuickLayouts) << "QQuickGridLayoutBase::invalidate()";
-
if (childItem) {
if (QQuickGridLayoutItem *layoutItem = d->engine.findLayoutItem(childItem))
layoutItem->invalidate();
- if (d->m_ignoredItems.contains(childItem)) {
- updateLayoutItems();
- return;
- }
}
// invalidate engine
d->engine.invalidate();
- QQuickLayout::invalidate(this);
+ qCDebug(lcQuickLayouts) << "calling QQuickLayout::invalidate();";
+ QQuickLayout::invalidate();
- QQuickLayoutAttached *info = attachedLayoutObject(this);
-
- const QSizeF min = sizeHint(Qt::MinimumSize);
- const QSizeF pref = sizeHint(Qt::PreferredSize);
- const QSizeF max = sizeHint(Qt::MaximumSize);
-
- const bool old = info->setChangesNotificationEnabled(false);
- info->setMinimumImplicitSize(min);
- info->setMaximumImplicitSize(max);
- info->setChangesNotificationEnabled(old);
- if (pref.width() == implicitWidth() && pref.height() == implicitHeight()) {
- // In case setImplicitSize does not emit implicit{Width|Height}Changed
- if (QQuickLayout *parentLayout = qobject_cast<QQuickLayout *>(parentItem()))
- parentLayout->invalidate(this);
- } else {
- setImplicitSize(pref.width(), pref.height());
- }
+ if (QQuickLayout *parentLayout = qobject_cast<QQuickLayout *>(parentItem()))
+ parentLayout->invalidate(this);
+ qCDebug(lcQuickLayouts) << "QQuickGridLayoutBase::invalidate() LEAVING" << this;
}
void QQuickGridLayoutBase::updateLayoutItems()
@@ -412,23 +411,25 @@ void QQuickGridLayoutBase::updateLayoutItems()
return;
}
- qCDebug(lcQuickLayouts) << "QQuickGridLayoutBase::updateLayoutItems()";
+ qCDebug(lcQuickLayouts) << "QQuickGridLayoutBase::updateLayoutItems ENTERING" << this;
d->engine.deleteItems();
insertLayoutItems();
-
- invalidate();
- qCDebug(lcQuickLayouts) << "QQuickGridLayoutBase::updateLayoutItems() LEAVING";
+ qCDebug(lcQuickLayouts) << "QQuickGridLayoutBase::updateLayoutItems() LEAVING" << this;
}
QQuickItem *QQuickGridLayoutBase::itemAt(int index) const
{
Q_D(const QQuickGridLayoutBase);
+ qCDebug(lcQuickLayouts).nospace() << "QQuickGridLayoutBase::itemAt(" << index << ")";
+ ensureLayoutItemsUpdated();
+ qCDebug(lcQuickLayouts).nospace() << "QQuickGridLayoutBase::itemAt(" << index << ") LEAVING";
return static_cast<QQuickGridLayoutItem*>(d->engine.itemAt(index))->layoutItem();
}
int QQuickGridLayoutBase::itemCount() const
{
Q_D(const QQuickGridLayoutBase);
+ ensureLayoutItemsUpdated();
return d->engine.itemCount();
}
@@ -460,7 +461,7 @@ void QQuickGridLayoutBase::itemVisibilityChanged(QQuickItem *item)
if (!isReady())
return;
qCDebug(lcQuickLayouts) << "QQuickGridLayoutBase::itemVisibilityChanged()";
- updateLayoutItems();
+ invalidate(item);
}
void QQuickGridLayoutBase::rearrange(const QSizeF &size)
@@ -469,6 +470,9 @@ void QQuickGridLayoutBase::rearrange(const QSizeF &size)
if (!isReady())
return;
+ ensureLayoutItemsUpdated();
+
+ qCDebug(lcQuickLayouts) << "QQuickGridLayoutBase::rearrange" << d->m_recurRearrangeCounter << this;
const auto refCounter = qScopeGuard([&d] {
--(d->m_recurRearrangeCounter);
});
@@ -502,7 +506,7 @@ void QQuickGridLayoutBase::rearrange(const QSizeF &size)
d->m_invalidateAfterRearrange.clear();
if (d->m_updateAfterRearrange) {
- updateLayoutItems();
+ ensureLayoutItemsUpdated();
d->m_updateAfterRearrange = false;
}
}
@@ -582,7 +586,7 @@ void QQuickGridLayout::setColumns(int columns)
if (d->columns == columns)
return;
d->columns = columns;
- updateLayoutItems();
+ invalidate();
emit columnsChanged();
}
@@ -605,7 +609,7 @@ void QQuickGridLayout::setRows(int rows)
if (d->rows == rows)
return;
d->rows = rows;
- updateLayoutItems();
+ invalidate();
emit rowsChanged();
}
@@ -643,7 +647,7 @@ void QQuickGridLayout::setFlow(QQuickGridLayout::Flow flow)
return;
d->flow = flow;
// If flow is changed, the layout needs to be repopulated
- updateLayoutItems();
+ invalidate();
emit flowChanged();
}
@@ -664,7 +668,6 @@ void QQuickGridLayout::insertLayoutItems()
if (flowBound < 0)
flowBound = std::numeric_limits<int>::max();
- d->m_ignoredItems.clear();
QSizeF sizeHints[Qt::NSizeHints];
const auto items = childItems();
for (QQuickItem *child : items) {
@@ -852,7 +855,6 @@ void QQuickLinearLayout::setSpacing(qreal space)
void QQuickLinearLayout::insertLayoutItems()
{
Q_D(QQuickLinearLayout);
- d->m_ignoredItems.clear();
QSizeF sizeHints[Qt::NSizeHints];
const auto items = childItems();
for (QQuickItem *child : items) {