From 8017cf63505f7ed477c658634ec882a12d3b3ddc Mon Sep 17 00:00:00 2001 From: Martin Jones Date: Wed, 16 May 2012 17:49:32 +1000 Subject: Optimize type resolution Faster qmlType() and resolveType() lookup. Change-Id: I096439f23bf6071e8bfdf0cda366cc71e00293ba Reviewed-by: Aaron Kennedy --- src/qml/qml/ftw/qhashedstring.cpp | 21 ++++++ src/qml/qml/ftw/qhashedstring_p.h | 1 + src/qml/qml/qqmlcompiler.cpp | 11 +-- src/qml/qml/qqmldirparser.cpp | 8 +-- src/qml/qml/qqmldirparser_p.h | 7 +- src/qml/qml/qqmlimport.cpp | 61 +++++++++-------- src/qml/qml/qqmlimport_p.h | 4 +- src/qml/qml/qqmlmetatype.cpp | 138 +++++++++++++++++++------------------- src/qml/qml/qqmlmetatype_p.h | 15 ++--- 9 files changed, 147 insertions(+), 119 deletions(-) (limited to 'src/qml/qml') diff --git a/src/qml/qml/ftw/qhashedstring.cpp b/src/qml/qml/ftw/qhashedstring.cpp index 1f09d50ed3..2dc717b488 100644 --- a/src/qml/qml/ftw/qhashedstring.cpp +++ b/src/qml/qml/ftw/qhashedstring.cpp @@ -443,6 +443,27 @@ bool QHashedStringRef::startsWith(const QString &s) const QHashedString::compare(s.constData(), m_data, s.length()); } +static int findChar(const QChar *str, int len, QChar ch, int from) +{ + const ushort *s = (const ushort *)str; + ushort c = ch.unicode(); + if (from < 0) + from = qMax(from + len, 0); + if (from < len) { + const ushort *n = s + from - 1; + const ushort *e = s + len; + while (++n != e) + if (*n == c) + return n - s; + } + return -1; +} + +int QHashedStringRef::indexOf(const QChar &c, int from) const +{ + return findChar(m_data, m_length, c, from); +} + QString QHashedStringRef::toString() const { if (m_length == 0) diff --git a/src/qml/qml/ftw/qhashedstring_p.h b/src/qml/qml/ftw/qhashedstring_p.h index f058f21e98..f8099d508b 100644 --- a/src/qml/qml/ftw/qhashedstring_p.h +++ b/src/qml/qml/ftw/qhashedstring_p.h @@ -148,6 +148,7 @@ public: inline const QChar *constData() const; bool startsWith(const QString &) const; bool endsWith(const QString &) const; + int indexOf(const QChar &, int from=0) const; QHashedStringRef mid(int, int) const; inline bool isEmpty() const; diff --git a/src/qml/qml/qqmlcompiler.cpp b/src/qml/qml/qqmlcompiler.cpp index c423d933d0..54035fcccf 100644 --- a/src/qml/qml/qqmlcompiler.cpp +++ b/src/qml/qml/qqmlcompiler.cpp @@ -90,7 +90,8 @@ using namespace QQmlCompilerTypes; static QString id_string(QLatin1String("id")); static QString on_string(QLatin1String("on")); static QString Changed_string(QLatin1String("Changed")); -static QString Component_import_string(QLatin1String("QML/Component")); +static QString Component_string(QLatin1String("Component")); +static QString Component_module_string(QLatin1String("QML")); static QString qsTr_string(QLatin1String("qsTr")); static QString qsTrId_string(QLatin1String("qsTrId")); @@ -1603,7 +1604,7 @@ bool QQmlCompiler::buildSubObject(QQmlScript::Object *obj, const BindingContext int QQmlCompiler::componentTypeRef() { if (cachedComponentTypeRef == -1) { - QQmlType *t = QQmlMetaType::qmlType(Component_import_string,1,0); + QQmlType *t = QQmlMetaType::qmlType(Component_string, Component_module_string, 1, 0); for (int ii = output->types.count() - 1; ii >= 0; --ii) { if (output->types.at(ii).type == t) { cachedComponentTypeRef = ii; @@ -1739,7 +1740,7 @@ bool QQmlCompiler::buildProperty(QQmlScript::Property *prop, QQmlType *type = 0; QQmlImportNamespace *typeNamespace = 0; - unit->imports().resolveType(prop->name().toString(), &type, 0, 0, 0, &typeNamespace); + unit->imports().resolveType(prop->name(), &type, 0, 0, 0, &typeNamespace); if (typeNamespace) { COMPILE_CHECK(buildPropertyInNamespace(typeNamespace, prop, obj, @@ -1861,7 +1862,7 @@ bool QQmlCompiler::buildPropertyInNamespace(QQmlImportNamespace *ns, // Setup attached property data QQmlType *type = 0; - unit->imports().resolveType(ns, prop->name().toString(), &type, 0, 0, 0); + unit->imports().resolveType(ns, prop->name(), &type, 0, 0, 0); if (!type || !type->attachedPropertiesType()) COMPILE_EXCEPTION(prop, tr("Non-existent attached object")); @@ -2875,7 +2876,7 @@ bool QQmlCompiler::buildDynamicMeta(QQmlScript::Object *obj, DynamicMetaMode mod QByteArray customTypeName; QQmlType *qmltype = 0; QString url; - if (!unit->imports().resolveType(p->customType.toString(), &qmltype, &url, 0, 0, 0)) + if (!unit->imports().resolveType(p->customType, &qmltype, &url, 0, 0, 0)) COMPILE_EXCEPTION(p, tr("Invalid property type")); if (!qmltype) { diff --git a/src/qml/qml/qqmldirparser.cpp b/src/qml/qml/qqmldirparser.cpp index 2da4a3300b..2fb47cc008 100644 --- a/src/qml/qml/qqmldirparser.cpp +++ b/src/qml/qml/qqmldirparser.cpp @@ -156,7 +156,7 @@ bool QQmlDirParser::parse() } Component entry(sections[1], sections[2], -1, -1); entry.internal = true; - _components.append(entry); + _components.insertMulti(entry.typeName, entry); } else if (sections[0] == QLatin1String("typeinfo")) { if (sectionCount != 2) { reportError(lineNumber, -1, @@ -171,7 +171,7 @@ bool QQmlDirParser::parse() } else if (sectionCount == 2) { // No version specified (should only be used for relative qmldir files) const Component entry(sections[0], sections[1], -1, -1); - _components.append(entry); + _components.insertMulti(entry.typeName, entry); } else if (sectionCount == 3) { const QString &version = sections[1]; const int dotIndex = version.indexOf(QLatin1Char('.')); @@ -196,7 +196,7 @@ bool QQmlDirParser::parse() _scripts.append(entry); } else { const Component entry(sections[0], fileName, majorVersion, minorVersion); - _components.append(entry); + _components.insertMulti(entry.typeName, entry); } } } @@ -250,7 +250,7 @@ QList QQmlDirParser::plugins() const return _plugins; } -QList QQmlDirParser::components() const +QHash QQmlDirParser::components() const { return _components; } diff --git a/src/qml/qml/qqmldirparser_p.h b/src/qml/qml/qqmldirparser_p.h index 77fe277a7e..4de39e04d9 100644 --- a/src/qml/qml/qqmldirparser_p.h +++ b/src/qml/qml/qqmldirparser_p.h @@ -56,6 +56,7 @@ #include #include #include +#include QT_BEGIN_NAMESPACE @@ -120,7 +121,7 @@ public: int minorVersion; }; - QList components() const; + QHash components() const; QList