From 28128f2dc6c5eb63d52658ede840e02b06f9ac2c Mon Sep 17 00:00:00 2001 From: Erik Verbruggen Date: Thu, 28 Apr 2016 11:09:41 +0200 Subject: QML: Fill QtObject lazily. Only iterate over the enumerations/enumerators in the staticQtMetaObject when a get() is done, and the key/value is not yet in stored in the underlying Object. The whole cost of the iteration is now moved to get() and advanceIterator(). The latter will add all items in one swoop, but iteration over QtObject isn't used much (if at all). The get() will add all key/value pairs up until it finds the requested key. Checking a number of applications shows that none of them use all (or the "last") key, so it will actually save entries (which equals memory) too. This change reduces the instruction count for QtObject from 2.7M instructions down to 95k. As this initialization is done from the QQmlEngine constructor, it also speeds up that initialization. Task-number: QTBUG-43770 Change-Id: I71331ff76bdacdd4790f7ff0430c4cbc214fe0ab Reviewed-by: Simon Hausmann --- tests/auto/qml/qqmlqt/tst_qqmlqt.cpp | 46 ++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'tests/auto/qml/qqmlqt/tst_qqmlqt.cpp') diff --git a/tests/auto/qml/qqmlqt/tst_qqmlqt.cpp b/tests/auto/qml/qqmlqt/tst_qqmlqt.cpp index 01283dd587..2f44c34bc2 100644 --- a/tests/auto/qml/qqmlqt/tst_qqmlqt.cpp +++ b/tests/auto/qml/qqmlqt/tst_qqmlqt.cpp @@ -87,6 +87,7 @@ private slots: void fontFamilies(); void quit(); void resolvedUrl(); + void qtObjectContents(); private: QQmlEngine engine; @@ -934,6 +935,51 @@ void tst_qqmlqt::resolvedUrl() delete object; } +void tst_qqmlqt::qtObjectContents() +{ + struct StaticQtMetaObject : public QObject + { + static const QMetaObject *get() + { return &staticQtMetaObject; } + }; + + QQmlComponent component(&engine, testFileUrl("qtObjectContents.qml")); + + QObject *object = component.create(); + QVERIFY(object != 0); + + QVERIFY(object->property("values").canConvert()); + QVariantMap values = object->property("values").value().toVariant().toMap(); + + QSet keys; + int uniqueKeys = 0; + const QMetaObject *qtMetaObject = StaticQtMetaObject::get(); + for (int ii = 0; ii < qtMetaObject->enumeratorCount(); ++ii) { + QMetaEnum enumerator = qtMetaObject->enumerator(ii); + for (int jj = 0; jj < enumerator.keyCount(); ++jj) { + auto key = enumerator.key(jj); +// qDebug() << "key:" << key; + if (!keys.contains(key)) { + ++uniqueKeys; + keys.insert(key); + } + QVERIFY(values.contains(key)); + QVariant value = values.value(key); + QVERIFY(value.canConvert()); + QCOMPARE(value.toInt(), enumerator.value(jj)); + } + } + QVERIFY(values.contains("Asynchronous")); + QCOMPARE(values.value("Asynchronous").toInt(), 0); + ++uniqueKeys; + QVERIFY(values.contains("Synchronous")); + QCOMPARE(values.value("Synchronous").toInt(), 1); + ++uniqueKeys; + QCOMPARE(values.count(), uniqueKeys); + + delete object; +} + QTEST_MAIN(tst_qqmlqt) #include "tst_qqmlqt.moc" -- cgit v1.2.3