From eac3500387fcd966e936cbe16b75db6cf9df3abe Mon Sep 17 00:00:00 2001 From: Aaron Kennedy Date: Mon, 29 Mar 2010 13:07:29 +1000 Subject: Optimization: Don't use QVariant in QDeclarativeVMEMetaObject --- src/declarative/qml/qdeclarativevmemetaobject.cpp | 430 +++++++++++++++++++-- src/declarative/qml/qdeclarativevmemetaobject_p.h | 15 +- .../qmltime/tests/vmemetaobject/null.qml | 13 + .../qmltime/tests/vmemetaobject/property.qml | 18 + 4 files changed, 447 insertions(+), 29 deletions(-) create mode 100644 tests/benchmarks/declarative/qmltime/tests/vmemetaobject/null.qml create mode 100644 tests/benchmarks/declarative/qmltime/tests/vmemetaobject/property.qml diff --git a/src/declarative/qml/qdeclarativevmemetaobject.cpp b/src/declarative/qml/qdeclarativevmemetaobject.cpp index f9c99ee..7a08a2c 100644 --- a/src/declarative/qml/qdeclarativevmemetaobject.cpp +++ b/src/declarative/qml/qdeclarativevmemetaobject.cpp @@ -47,19 +47,316 @@ #include "qdeclarativeexpression_p.h" #include "qdeclarativecontext_p.h" -#include -#include -#include -#include +Q_DECLARE_METATYPE(QScriptValue); QT_BEGIN_NAMESPACE +class QDeclarativeVMEVariant +{ +public: + inline QDeclarativeVMEVariant(); + inline ~QDeclarativeVMEVariant(); + + inline const void *dataPtr() const; + inline void *dataPtr(); + inline int dataType() const; + + inline QObject *asQObject(); + inline const QVariant &asQVariant(); + inline int asInt(); + inline bool asBool(); + inline double asDouble(); + inline const QString &asQString(); + inline const QUrl &asQUrl(); + inline const QColor &asQColor(); + inline const QDate &asQDate(); + inline const QDateTime &asQDateTime(); + inline const QScriptValue &asQScriptValue(); + + inline void setValue(QObject *); + inline void setValue(const QVariant &); + inline void setValue(int); + inline void setValue(bool); + inline void setValue(double); + inline void setValue(const QString &); + inline void setValue(const QUrl &); + inline void setValue(const QColor &); + inline void setValue(const QDate &); + inline void setValue(const QDateTime &); + inline void setValue(const QScriptValue &); + +private: + int type; + void *data[4]; // Large enough to hold all types + + inline void cleanup(); +}; + +QDeclarativeVMEVariant::QDeclarativeVMEVariant() +: type(QVariant::Invalid) +{ +} + +QDeclarativeVMEVariant::~QDeclarativeVMEVariant() +{ + cleanup(); +} + +void QDeclarativeVMEVariant::cleanup() +{ + if (type == QVariant::Invalid) { + } else if (type == QMetaType::QObjectStar || + type == QMetaType::Int || + type == QMetaType::Bool || + type == QMetaType::Double) { + type = QVariant::Invalid; + } else if (type == QMetaType::QString) { + ((QString *)dataPtr())->~QString(); + type = QVariant::Invalid; + } else if (type == QMetaType::QUrl) { + ((QUrl *)dataPtr())->~QUrl(); + type = QVariant::Invalid; + } else if (type == QMetaType::QColor) { + ((QColor *)dataPtr())->~QColor(); + type = QVariant::Invalid; + } else if (type == QMetaType::QDate) { + ((QDate *)dataPtr())->~QDate(); + type = QVariant::Invalid; + } else if (type == QMetaType::QDateTime) { + ((QDateTime *)dataPtr())->~QDateTime(); + type = QVariant::Invalid; + } else if (type == qMetaTypeId()) { + ((QVariant *)dataPtr())->~QVariant(); + type = QVariant::Invalid; + } else if (type == qMetaTypeId()) { + ((QScriptValue *)dataPtr())->~QScriptValue(); + type = QVariant::Invalid; + } + +} + +int QDeclarativeVMEVariant::dataType() const +{ + return type; +} + +const void *QDeclarativeVMEVariant::dataPtr() const +{ + return &data; +} + +void *QDeclarativeVMEVariant::dataPtr() +{ + return &data; +} + +QObject *QDeclarativeVMEVariant::asQObject() +{ + if (type != QMetaType::QObjectStar) + setValue((QObject *)0); + + return *(QObject **)(dataPtr()); +} + +const QVariant &QDeclarativeVMEVariant::asQVariant() +{ + if (type != QMetaType::QVariant) + setValue(QVariant()); + + return *(QVariant *)(dataPtr()); +} + +int QDeclarativeVMEVariant::asInt() +{ + if (type != QMetaType::Int) + setValue(int(0)); + + return *(int *)(dataPtr()); +} + +bool QDeclarativeVMEVariant::asBool() +{ + if (type != QMetaType::Bool) + setValue(bool(false)); + + return *(bool *)(dataPtr()); +} + +double QDeclarativeVMEVariant::asDouble() +{ + if (type != QMetaType::Double) + setValue(double(0)); + + return *(double *)(dataPtr()); +} + +const QString &QDeclarativeVMEVariant::asQString() +{ + if (type != QMetaType::QString) + setValue(QString()); + + return *(QString *)(dataPtr()); +} + +const QUrl &QDeclarativeVMEVariant::asQUrl() +{ + if (type != QMetaType::QUrl) + setValue(QUrl()); + + return *(QUrl *)(dataPtr()); +} + +const QColor &QDeclarativeVMEVariant::asQColor() +{ + if (type != QMetaType::QColor) + setValue(QColor()); + + return *(QColor *)(dataPtr()); +} + +const QDate &QDeclarativeVMEVariant::asQDate() +{ + if (type != QMetaType::QDate) + setValue(QDate()); + + return *(QDate *)(dataPtr()); +} + +const QDateTime &QDeclarativeVMEVariant::asQDateTime() +{ + if (type != QMetaType::QDateTime) + setValue(QDateTime()); + + return *(QDateTime *)(dataPtr()); +} + +const QScriptValue &QDeclarativeVMEVariant::asQScriptValue() +{ + if (type != qMetaTypeId()) + setValue(QScriptValue()); + + return *(QScriptValue *)(dataPtr()); +} + +void QDeclarativeVMEVariant::setValue(QObject *v) +{ + if (type != QMetaType::QObjectStar) { + cleanup(); + type = QMetaType::QObjectStar; + } + *(QObject **)(dataPtr()) = v; +} + +void QDeclarativeVMEVariant::setValue(const QVariant &v) +{ + if (type != qMetaTypeId()) { + cleanup(); + type = qMetaTypeId(); + new (dataPtr()) QVariant(v); + } else { + *(QVariant *)(dataPtr()) = v; + } +} + +void QDeclarativeVMEVariant::setValue(int v) +{ + if (type != QMetaType::Int) { + cleanup(); + type = QMetaType::Int; + } + *(int *)(dataPtr()) = v; +} + +void QDeclarativeVMEVariant::setValue(bool v) +{ + if (type != QMetaType::Bool) { + cleanup(); + type = QMetaType::Bool; + } + *(bool *)(dataPtr()) = v; +} + +void QDeclarativeVMEVariant::setValue(double v) +{ + if (type != QMetaType::Double) { + cleanup(); + type = QMetaType::Double; + } + *(double *)(dataPtr()) = v; +} + +void QDeclarativeVMEVariant::setValue(const QString &v) +{ + if (type != QMetaType::QString) { + cleanup(); + type = QMetaType::QString; + new (dataPtr()) QString(v); + } else { + *(QString *)(dataPtr()) = v; + } +} + +void QDeclarativeVMEVariant::setValue(const QUrl &v) +{ + if (type != QMetaType::QUrl) { + cleanup(); + type = QMetaType::QUrl; + new (dataPtr()) QUrl(v); + } else { + *(QUrl *)(dataPtr()) = v; + } +} + +void QDeclarativeVMEVariant::setValue(const QColor &v) +{ + if (type != QMetaType::QColor) { + cleanup(); + type = QMetaType::QColor; + new (dataPtr()) QColor(v); + } else { + *(QColor *)(dataPtr()) = v; + } +} + +void QDeclarativeVMEVariant::setValue(const QDate &v) +{ + if (type != QMetaType::QDate) { + cleanup(); + type = QMetaType::QDate; + new (dataPtr()) QDate(v); + } else { + *(QDate *)(dataPtr()) = v; + } +} + +void QDeclarativeVMEVariant::setValue(const QDateTime &v) +{ + if (type != QMetaType::QDateTime) { + cleanup(); + type = QMetaType::QDateTime; + new (dataPtr()) QDateTime(v); + } else { + *(QDateTime *)(dataPtr()) = v; + } +} + +void QDeclarativeVMEVariant::setValue(const QScriptValue &v) +{ + if (type != qMetaTypeId()) { + cleanup(); + type = qMetaTypeId(); + new (dataPtr()) QScriptValue(v); + } else { + *(QScriptValue *)(dataPtr()) = v; + } +} + QDeclarativeVMEMetaObject::QDeclarativeVMEMetaObject(QObject *obj, const QMetaObject *other, const QDeclarativeVMEMetaData *meta, QDeclarativeCompiledData *cdata) : object(obj), compiledData(cdata), ctxt(QDeclarativeDeclarativeData::get(obj)->outerContext), - metaData(meta), methods(0), parent(0) + metaData(meta), data(0), methods(0), parent(0) { compiledData->addref(); @@ -74,20 +371,17 @@ QDeclarativeVMEMetaObject::QDeclarativeVMEMetaObject(QObject *obj, propOffset = QAbstractDynamicMetaObject::propertyOffset(); methodOffset = QAbstractDynamicMetaObject::methodOffset(); - data = new QVariant[metaData->propertyCount]; + data = new QDeclarativeVMEVariant[metaData->propertyCount]; aConnected.resize(metaData->aliasCount); - int list_type = qMetaTypeId >(); + // ### Optimize for (int ii = 0; ii < metaData->propertyCount; ++ii) { int t = (metaData->propertyData() + ii)->propertyType; if (t == list_type) { listProperties.append(new List(methodOffset + ii)); - data[ii] = QVariant::fromValue(QDeclarativeListProperty(obj, listProperties.last(), list_append, - list_count, list_at, list_clear)); - } else if (t != -1) { - data[ii] = QVariant((QVariant::Type)t); - } + data[ii].setValue(listProperties.count() - 1); + } } } @@ -145,11 +439,10 @@ int QDeclarativeVMEMetaObject::metaCall(QMetaObject::Call c, int _id, void **a) if (t == -1) { if (c == QMetaObject::ReadProperty) { - *reinterpret_cast(a[0]) = data[id]; + *reinterpret_cast(a[0]) = readVarPropertyAsVariant(id); } else if (c == QMetaObject::WriteProperty) { - needActivate = - (data[id] != *reinterpret_cast(a[0])); - data[id] = *reinterpret_cast(a[0]); + needActivate = (data[id].asQVariant() != *reinterpret_cast(a[0])); + data[id].setValue(*reinterpret_cast(a[0])); } } else { @@ -157,42 +450,83 @@ int QDeclarativeVMEMetaObject::metaCall(QMetaObject::Call c, int _id, void **a) if (c == QMetaObject::ReadProperty) { switch(t) { case QVariant::Int: - *reinterpret_cast(a[0]) = data[id].toInt(); + *reinterpret_cast(a[0]) = data[id].asInt(); break; case QVariant::Bool: - *reinterpret_cast(a[0]) = data[id].toBool(); + *reinterpret_cast(a[0]) = data[id].asBool(); break; case QVariant::Double: - *reinterpret_cast(a[0]) = data[id].toDouble(); + *reinterpret_cast(a[0]) = data[id].asDouble(); break; case QVariant::String: - *reinterpret_cast(a[0]) = data[id].toString(); + *reinterpret_cast(a[0]) = data[id].asQString(); break; case QVariant::Url: - *reinterpret_cast(a[0]) = data[id].toUrl(); + *reinterpret_cast(a[0]) = data[id].asQUrl(); break; case QVariant::Color: - *reinterpret_cast(a[0]) = data[id].value(); + *reinterpret_cast(a[0]) = data[id].asQColor(); break; case QVariant::Date: - *reinterpret_cast(a[0]) = data[id].toDate(); + *reinterpret_cast(a[0]) = data[id].asQDate(); + break; + case QVariant::DateTime: + *reinterpret_cast(a[0]) = data[id].asQDateTime(); break; case QMetaType::QObjectStar: - *reinterpret_cast(a[0]) = data[id].value(); + *reinterpret_cast(a[0]) = data[id].asQObject(); break; default: break; } if (t == qMetaTypeId >()) { *reinterpret_cast *>(a[0]) = - data[id].value >(); + QDeclarativeListProperty(object, (void *)&listProperties.at(data[id].asInt()), + list_append, list_count, list_at, list_clear); } } else if (c == QMetaObject::WriteProperty) { - QVariant value = QVariant((QVariant::Type)data[id].type(), a[0]); - needActivate = (data[id] != value); - data[id] = value; + switch(t) { + case QVariant::Int: + needActivate = *reinterpret_cast(a[0]) != data[id].asInt(); + data[id].setValue(*reinterpret_cast(a[0])); + break; + case QVariant::Bool: + needActivate = *reinterpret_cast(a[0]) != data[id].asBool(); + data[id].setValue(*reinterpret_cast(a[0])); + break; + case QVariant::Double: + needActivate = *reinterpret_cast(a[0]) != data[id].asDouble(); + data[id].setValue(*reinterpret_cast(a[0])); + break; + case QVariant::String: + needActivate = *reinterpret_cast(a[0]) != data[id].asQString(); + data[id].setValue(*reinterpret_cast(a[0])); + break; + case QVariant::Url: + needActivate = *reinterpret_cast(a[0]) != data[id].asQUrl(); + data[id].setValue(*reinterpret_cast(a[0])); + break; + case QVariant::Color: + needActivate = *reinterpret_cast(a[0]) != data[id].asQColor(); + data[id].setValue(*reinterpret_cast(a[0])); + break; + case QVariant::Date: + needActivate = *reinterpret_cast(a[0]) != data[id].asQDate(); + data[id].setValue(*reinterpret_cast(a[0])); + break; + case QVariant::DateTime: + needActivate = *reinterpret_cast(a[0]) != data[id].asQDateTime(); + data[id].setValue(*reinterpret_cast(a[0])); + break; + case QMetaType::QObjectStar: + needActivate = *reinterpret_cast(a[0]) != data[id].asQObject(); + data[id].setValue(*reinterpret_cast(a[0])); + break; + default: + break; + } } } @@ -316,6 +650,28 @@ QScriptValue QDeclarativeVMEMetaObject::method(int index) return methods[index]; } +QScriptValue QDeclarativeVMEMetaObject::readVarProperty(int id) +{ + if (data[id].dataType() == qMetaTypeId()) + return data[id].asQScriptValue(); + else + return QDeclarativeEnginePrivate::get(ctxt->engine)->scriptValueFromVariant(data[id].asQVariant()); +} + +QVariant QDeclarativeVMEMetaObject::readVarPropertyAsVariant(int id) +{ + if (data[id].dataType() == qMetaTypeId()) + return QDeclarativeEnginePrivate::get(ctxt->engine)->scriptValueToVariant(data[id].asQScriptValue()); + else + return data[id].asQVariant(); +} + +void QDeclarativeVMEMetaObject::writeVarProperty(int id, const QScriptValue &value) +{ + data[id].setValue(value); + activate(object, methodOffset + id, 0); +} + void QDeclarativeVMEMetaObject::listChanged(int id) { activate(object, methodOffset + id, 0); @@ -364,4 +720,22 @@ QScriptValue QDeclarativeVMEMetaObject::vmeMethod(int index) return method(index - methodOffset - plainSignals); } +QScriptValue QDeclarativeVMEMetaObject::vmeProperty(int index) +{ + if (index < propOffset) { + Q_ASSERT(parent); + return static_cast(parent)->vmeProperty(index); + } + return readVarProperty(index - propOffset); +} + +void QDeclarativeVMEMetaObject::setVMEProperty(int index, const QScriptValue &v) +{ + if (index < propOffset) { + Q_ASSERT(parent); + static_cast(parent)->setVMEProperty(index, v); + } + return writeVarProperty(index - propOffset, v); +} + QT_END_NAMESPACE diff --git a/src/declarative/qml/qdeclarativevmemetaobject_p.h b/src/declarative/qml/qdeclarativevmemetaobject_p.h index e11f6fa..4718fa7 100644 --- a/src/declarative/qml/qdeclarativevmemetaobject_p.h +++ b/src/declarative/qml/qdeclarativevmemetaobject_p.h @@ -58,6 +58,10 @@ #include #include #include +#include +#include +#include +#include #include @@ -106,6 +110,7 @@ struct QDeclarativeVMEMetaData } }; +class QDeclarativeVMEVariant; class QDeclarativeRefCount; class QDeclarativeVMEMetaObject : public QAbstractDynamicMetaObject { @@ -116,6 +121,9 @@ public: void registerInterceptor(int index, int valueIndex, QDeclarativePropertyValueInterceptor *interceptor); QScriptValue vmeMethod(int index); + QScriptValue vmeProperty(int index); + void setVMEProperty(int index, const QScriptValue &); + protected: virtual int metaCall(QMetaObject::Call _c, int _id, void **_a); @@ -128,7 +136,8 @@ private: int propOffset; int methodOffset; - QVariant *data; + QDeclarativeVMEVariant *data; + QBitArray aConnected; QBitArray aInterceptors; QHash > interceptors; @@ -136,6 +145,10 @@ private: QScriptValue *methods; QScriptValue method(int); + QScriptValue readVarProperty(int); + QVariant readVarPropertyAsVariant(int); + void writeVarProperty(int, const QScriptValue &); + QAbstractDynamicMetaObject *parent; void listChanged(int); diff --git a/tests/benchmarks/declarative/qmltime/tests/vmemetaobject/null.qml b/tests/benchmarks/declarative/qmltime/tests/vmemetaobject/null.qml new file mode 100644 index 0000000..a31af5a --- /dev/null +++ b/tests/benchmarks/declarative/qmltime/tests/vmemetaobject/null.qml @@ -0,0 +1,13 @@ +import Qt 4.6 +import QmlTime 1.0 as QmlTime + +Item { + + QmlTime.Timer { + component: Component { + QtObject { + } + } + } + +} diff --git a/tests/benchmarks/declarative/qmltime/tests/vmemetaobject/property.qml b/tests/benchmarks/declarative/qmltime/tests/vmemetaobject/property.qml new file mode 100644 index 0000000..007d12a --- /dev/null +++ b/tests/benchmarks/declarative/qmltime/tests/vmemetaobject/property.qml @@ -0,0 +1,18 @@ +import Qt 4.6 +import QmlTime 1.0 as QmlTime + +Item { + + QmlTime.Timer { + component: Component { + QtObject { + property string s + property string s2 + property string s3 + property string s4 + } + } + } + +} + -- cgit v0.12