diff options
author | Simon Hausmann <simon.hausmann@digia.com> | 2013-12-05 16:59:27 +0100 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-12-06 18:18:44 +0100 |
commit | 105d10dea9d790a69595a2e73e8fea80a5d1a3ad (patch) | |
tree | 68c8cfb65c6c23ebb44ea1436da4b0c354bd95a2 /src | |
parent | 0c936ca83c40b73f30195a619900f2be98b9e968 (diff) |
Fix quadratic behavior in QMetaObjectBuilder when writing string table
QHash::key() is O(n) and we're calling it n times. That can make repeated
calls to the meta object builder very slow, as for example QQmlPropertyMap
when inserting properties repeatedly.
Fortunately this is easy to fix, as the value in the hash map is also the
index, so we can simply iterate over the hash once. With the exception of
the class name, which we have to treat specially to ensure that it is always
the first entry in the string table.
Task-number: QTBUG-32720
Change-Id: Ic954c45c454107feee83216131f601cc69d4c63b
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/kernel/qmetaobjectbuilder.cpp | 52 | ||||
-rw-r--r-- | src/corelib/kernel/qmetaobjectbuilder_p.h | 5 | ||||
-rw-r--r-- | src/dbus/qdbusmetaobject.cpp | 3 |
3 files changed, 40 insertions, 20 deletions
diff --git a/src/corelib/kernel/qmetaobjectbuilder.cpp b/src/corelib/kernel/qmetaobjectbuilder.cpp index 4518d702cd..11ab39af1a 100644 --- a/src/corelib/kernel/qmetaobjectbuilder.cpp +++ b/src/corelib/kernel/qmetaobjectbuilder.cpp @@ -1075,8 +1075,14 @@ int QMetaObjectBuilder::indexOfClassInfo(const QByteArray& name) \brief The QMetaStringTable class can generate a meta-object string table at runtime. */ -QMetaStringTable::QMetaStringTable() - : m_index(0) {} +QMetaStringTable::QMetaStringTable(const QByteArray &className) + : m_index(0) + , m_className(className) +{ + const int index = enter(m_className); + Q_ASSERT(index == 0); + Q_UNUSED(index); +} // Enters the given value into the string table (if it hasn't already been // entered). Returns the index of the string. @@ -1106,30 +1112,45 @@ int QMetaStringTable::blobSize() const return size; } +static void writeString(char *out, int i, const QByteArray &str, + const int offsetOfStringdataMember, int &stringdataOffset) +{ + int size = str.size(); + qptrdiff offset = offsetOfStringdataMember + stringdataOffset + - i * sizeof(QByteArrayData); + const QByteArrayData data = + Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(size, offset); + + memcpy(out + i * sizeof(QByteArrayData), &data, sizeof(QByteArrayData)); + + memcpy(out + offsetOfStringdataMember + stringdataOffset, str.constData(), size); + out[offsetOfStringdataMember + stringdataOffset + size] = '\0'; + + stringdataOffset += size + 1; +} + // Writes strings to string data struct. // The struct consists of an array of QByteArrayData, followed by a char array // containing the actual strings. This format must match the one produced by // moc (see generator.cpp). -void QMetaStringTable::writeBlob(char *out) +void QMetaStringTable::writeBlob(char *out) const { Q_ASSERT(!(reinterpret_cast<quintptr>(out) & (preferredAlignment()-1))); int offsetOfStringdataMember = m_entries.size() * sizeof(QByteArrayData); int stringdataOffset = 0; - for (int i = 0; i < m_entries.size(); ++i) { - const QByteArray &str = m_entries.key(i); - int size = str.size(); - qptrdiff offset = offsetOfStringdataMember + stringdataOffset - - i * sizeof(QByteArrayData); - const QByteArrayData data = - Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(size, offset); - memcpy(out + i * sizeof(QByteArrayData), &data, sizeof(QByteArrayData)); + // qt_metacast expects the first string in the string table to be the class name. + writeString(out, /*index*/0, m_className, offsetOfStringdataMember, stringdataOffset); - memcpy(out + offsetOfStringdataMember + stringdataOffset, str.constData(), size); - out[offsetOfStringdataMember + stringdataOffset + size] = '\0'; + for (Entries::ConstIterator it = m_entries.constBegin(), end = m_entries.constEnd(); + it != end; ++it) { + const int i = it.value(); + if (i == 0) + continue; + const QByteArray &str = it.key(); - stringdataOffset += size + 1; + writeString(out, i, str, offsetOfStringdataMember, stringdataOffset); } } @@ -1270,8 +1291,7 @@ static int buildMetaObject(QMetaObjectBuilderPrivate *d, char *buf, // Reset the current data position to just past the QMetaObjectPrivate. dataIndex = MetaObjectPrivateFieldCount; - QMetaStringTable strings; - strings.enter(d->className); + QMetaStringTable strings(d->className); // Output the class infos, Q_ASSERT(!buf || dataIndex == pmeta->classInfoData); diff --git a/src/corelib/kernel/qmetaobjectbuilder_p.h b/src/corelib/kernel/qmetaobjectbuilder_p.h index f79ce2f2f1..110aaf9ce2 100644 --- a/src/corelib/kernel/qmetaobjectbuilder_p.h +++ b/src/corelib/kernel/qmetaobjectbuilder_p.h @@ -323,18 +323,19 @@ private: class Q_CORE_EXPORT QMetaStringTable { public: - QMetaStringTable(); + QMetaStringTable(const QByteArray &className); int enter(const QByteArray &value); static int preferredAlignment(); int blobSize() const; - void writeBlob(char *out); + void writeBlob(char *out) const; private: typedef QHash<QByteArray, int> Entries; // string --> index mapping Entries m_entries; int m_index; + QByteArray m_className; }; Q_DECLARE_OPERATORS_FOR_FLAGS(QMetaObjectBuilder::AddMembers) diff --git a/src/dbus/qdbusmetaobject.cpp b/src/dbus/qdbusmetaobject.cpp index 51c0b27668..13c538bb59 100644 --- a/src/dbus/qdbusmetaobject.cpp +++ b/src/dbus/qdbusmetaobject.cpp @@ -445,8 +445,7 @@ void QDBusMetaObjectGenerator::write(QDBusMetaObject *obj) data_size += 2 + mm.inputTypes.count() + mm.outputTypes.count(); idata.resize(data_size + 1); - QMetaStringTable strings; - strings.enter(className.toLatin1()); + QMetaStringTable strings(className.toLatin1()); int offset = header->methodData; int parametersOffset = offset + header->methodCount * 5; |