aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/qml/qqmllanguage
diff options
context:
space:
mode:
authorSimon Hausmann <simon.hausmann@qt.io>2018-03-08 16:18:15 +0100
committerLars Knoll <lars.knoll@qt.io>2018-03-08 16:24:59 +0000
commitf514451cc2e3610e160b5dc8ccd1e390730ecc67 (patch)
treeb02d6e0de35fde62f6ce7864d5053df01d4f495e /tests/auto/qml/qqmllanguage
parent4838687eaa63de6e4d6821f14cf865a13af9ebc1 (diff)
Fix unnecessary evaluation of dependent bindings
Given two simple bindings in this order property int firstVar: secondVar property int secondVar: ... then the binding expression for "secondVar" ends up being evaluated twice at run-time. The first time happens when enabling the binding expression for "firstVar", which results in the engine detecting that there is a dependency onto another binding that has not been enabled yet. This is when QQmlData::flushPendingBinding(Impl) enables the expression for secondVar and does an initial evaluation. Afterwards the QQmlObjectCreator continues enabling the next binding in ::finalize(), which will end up evaluating secondVar a second time, unnecessarily. We can detect this case inside setEnabled and only call update() if we transition from disabled to enabled state. This should also cover the case of bindings created and assigned dynamically through QtQuick PropertyChanges / States, as those call setEnabled(false) before removing the binding (to replace it with something else) and setEnabled(true) when reverting the state (in QQmlPropertyPrivate::setBinding). Change-Id: I447432891eabff2c4393f5abfee1092992746fa0 Task-number: QTBUG-66945 Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Diffstat (limited to 'tests/auto/qml/qqmllanguage')
-rw-r--r--tests/auto/qml/qqmllanguage/data/noDoubleEvaluationForFlushedBindings.2.qml9
-rw-r--r--tests/auto/qml/qqmllanguage/data/noDoubleEvaluationForFlushedBindings.qml9
-rw-r--r--tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp33
3 files changed, 51 insertions, 0 deletions
diff --git a/tests/auto/qml/qqmllanguage/data/noDoubleEvaluationForFlushedBindings.2.qml b/tests/auto/qml/qqmllanguage/data/noDoubleEvaluationForFlushedBindings.2.qml
new file mode 100644
index 0000000000..ff2f0f5a2c
--- /dev/null
+++ b/tests/auto/qml/qqmllanguage/data/noDoubleEvaluationForFlushedBindings.2.qml
@@ -0,0 +1,9 @@
+import QtQml 2.0
+
+QtObject {
+ property int secondVar: {
+ stats.increaseEvaluationCounter()
+ return 1
+ }
+ property int firstVar: secondVar + 1
+}
diff --git a/tests/auto/qml/qqmllanguage/data/noDoubleEvaluationForFlushedBindings.qml b/tests/auto/qml/qqmllanguage/data/noDoubleEvaluationForFlushedBindings.qml
new file mode 100644
index 0000000000..0eb5e03642
--- /dev/null
+++ b/tests/auto/qml/qqmllanguage/data/noDoubleEvaluationForFlushedBindings.qml
@@ -0,0 +1,9 @@
+import QtQml 2.0
+
+QtObject {
+ property int firstVar: secondVar + 1
+ property int secondVar: {
+ stats.increaseEvaluationCounter()
+ return 1
+ }
+}
diff --git a/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp b/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp
index 5bb2b69565..403e8c5730 100644
--- a/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp
+++ b/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp
@@ -125,6 +125,8 @@ private slots:
void dynamicObjectProperties();
void dynamicSignalsAndSlots();
void simpleBindings();
+ void noDoubleEvaluationForFlushedBindings_data();
+ void noDoubleEvaluationForFlushedBindings();
void autoComponentCreation();
void autoComponentCreationInGroupProperty();
void propertyValueSource();
@@ -1478,6 +1480,37 @@ void tst_qqmllanguage::simpleBindings()
QCOMPARE(object->property("objectProperty"), QVariant::fromValue(object));
}
+class EvaluationCounter : public QObject
+{
+ Q_OBJECT
+public:
+ int counter = 0;
+ Q_INVOKABLE void increaseEvaluationCounter() { ++counter; }
+};
+
+void tst_qqmllanguage::noDoubleEvaluationForFlushedBindings_data()
+{
+ QTest::addColumn<QString>("fileName");
+ QTest::newRow("order1") << QString("noDoubleEvaluationForFlushedBindings.qml");
+ QTest::newRow("order2") << QString("noDoubleEvaluationForFlushedBindings.2.qml");
+}
+
+void tst_qqmllanguage::noDoubleEvaluationForFlushedBindings()
+{
+ QFETCH(QString, fileName);
+ QQmlEngine engine;
+
+ EvaluationCounter stats;
+ engine.rootContext()->setContextProperty("stats", &stats);
+
+ QQmlComponent component(&engine, testFileUrl(fileName));
+ VERIFY_ERRORS(0);
+ QScopedPointer<QObject> obj(component.create());
+ QVERIFY(!obj.isNull());
+
+ QCOMPARE(stats.counter, 1);
+}
+
void tst_qqmllanguage::autoComponentCreation()
{
{