From 0211f2806e97130eb9f37c8048450065bd97a3a3 Mon Sep 17 00:00:00 2001 From: Jedrzej Nowacki Date: Thu, 30 Sep 2010 14:52:57 +0200 Subject: Revert "Improve lookup speed of QMetaType::type()." This reverts commit 06893e99c46d28b9f02ce7158c07372daa6b9bba. --- src/corelib/kernel/qmetatype.cpp | 166 ++------------------------------- tests/auto/qmetatype/tst_qmetatype.cpp | 79 +--------------- 2 files changed, 8 insertions(+), 237 deletions(-) diff --git a/src/corelib/kernel/qmetatype.cpp b/src/corelib/kernel/qmetatype.cpp index 37360cb..bb77d2c 100644 --- a/src/corelib/kernel/qmetatype.cpp +++ b/src/corelib/kernel/qmetatype.cpp @@ -229,11 +229,7 @@ QT_BEGIN_NAMESPACE #define QT_ADD_STATIC_METATYPE(STR, TP) \ { STR, sizeof(STR) - 1, TP } -/* - Note: these MUST be in the order of the enums. - Note: adding, removing or changing values in the array should be followed by - recration of a hash function in the QMetaTypeStaticType class. - */ +/* Note: these MUST be in the order of the enums */ static const struct { const char * typeName; int typeNameLength; int type; } types[] = { /* All Core types */ @@ -422,165 +418,17 @@ const char *QMetaType::typeName(int type) return 0; } -/*! - \internal - \threadsafe - This class contains a hash table for converting typeName -> typeId it is used by qMetaTypeStaticType. - - C++ code produced by gperf version 3.0.3 - Command-line: gperf -C -m 100 -t -L C++ keys.txt - To recreate keys.txt file, all registered static types (look at QT_ADD_STATIC_METATYPE) should be - dump with their index number in the types static arrays. It would looks like this: - ... - void, 0 - bool, 1 - int, 2 - uint, 3 - ... - - Computed positions: -k'2,8,$' - The code was modified to save memory and follow the Qt coding style. - - AssoValues and ValueList classes were created to cut size of a correspondent static arrays. - - Function names and some arguments types were renamed. - - By default gperf generated code returns a struct containing a key and copy of a - corresponding value, to save memory the values copy was removed. -*/ -class QMetaTypeStaticType { -#if !((' ' == 32) && ('!' == 33) && ('"' == 34) && ('#' == 35) \ - && ('%' == 37) && ('&' == 38) && ('\'' == 39) && ('(' == 40) \ - && (')' == 41) && ('*' == 42) && ('+' == 43) && (',' == 44) \ - && ('-' == 45) && ('.' == 46) && ('/' == 47) && ('0' == 48) \ - && ('1' == 49) && ('2' == 50) && ('3' == 51) && ('4' == 52) \ - && ('5' == 53) && ('6' == 54) && ('7' == 55) && ('8' == 56) \ - && ('9' == 57) && (':' == 58) && (';' == 59) && ('<' == 60) \ - && ('=' == 61) && ('>' == 62) && ('?' == 63) && ('A' == 65) \ - && ('B' == 66) && ('C' == 67) && ('D' == 68) && ('E' == 69) \ - && ('F' == 70) && ('G' == 71) && ('H' == 72) && ('I' == 73) \ - && ('J' == 74) && ('K' == 75) && ('L' == 76) && ('M' == 77) \ - && ('N' == 78) && ('O' == 79) && ('P' == 80) && ('Q' == 81) \ - && ('R' == 82) && ('S' == 83) && ('T' == 84) && ('U' == 85) \ - && ('V' == 86) && ('W' == 87) && ('X' == 88) && ('Y' == 89) \ - && ('Z' == 90) && ('[' == 91) && ('\\' == 92) && (']' == 93) \ - && ('^' == 94) && ('_' == 95) && ('a' == 97) && ('b' == 98) \ - && ('c' == 99) && ('d' == 100) && ('e' == 101) && ('f' == 102) \ - && ('g' == 103) && ('h' == 104) && ('i' == 105) && ('j' == 106) \ - && ('k' == 107) && ('l' == 108) && ('m' == 109) && ('n' == 110) \ - && ('o' == 111) && ('p' == 112) && ('q' == 113) && ('r' == 114) \ - && ('s' == 115) && ('t' == 116) && ('u' == 117) && ('v' == 118) \ - && ('w' == 119) && ('x' == 120) && ('y' == 121) && ('z' == 122) \ - && ('{' == 123) && ('|' == 124) && ('}' == 125) && ('~' == 126)) -/* The character set is not based on ISO-646. */ -#error "gperf generated tables don't work with this execution character set. Please report a bug to ." -#endif - class AssoValues { - static const uchar assoValues[]; - public: - inline int operator[](uint i) - { - if (i < 42 || i >= 122) - return 126; - return assoValues[i - 42]; - } - }; - - class ValuesList { - static const uchar valuesList[]; - public: - inline int operator[](uchar i) - { - if (i < 83) - return valuesList[i]; - switch (i) { - case 87: - return 48; - case 100: - return 63; - case 101: - return 62; - case 125: - return 49; - default: - return -1; - }; - } - }; - -public: - static inline int type(const char *str, uint len); -private: - static inline uint hash(const char *str, uint len); - - static const uint minWordLength = 3; - static const uint maxWordLength = 18; - static const uint maxHashValue = 125; -}; - -const uchar QMetaTypeStaticType::AssoValues::assoValues[] = { - 46, 126, 126, 126, 126, 126, 126, 126, - 42, 39, 41, 126, 50, 126, 50, 126, 126, 126, - 126, 126, 12, 126, 126, 126, 30, 9, 1, 55, - 19, 126, 42, 48, 126, 45, 21, 33, 126, 1, - 0, 61, 25, 19, 33, 58, 30, 0, 126, 126, - 126, 126, 126, 126, 126, 126, 126, 13, 126, 34, - 0, 1, 126, 2, 16, 1, 126, 126, 6, 32, - 0, 12, 30, 126, 20, 3, 0, 17, 126, 126, - 14, 9 -}; - -const uchar QMetaTypeStaticType::ValuesList::valuesList[] = { - -1, -1, -1, 2, 44, 3, 25, 14, 38, 59, - 35, 61, 66, 58, 67, 65, 0, 4, 55, 6, - 70, 56, 1, 69, 31, 21, 26, 23, 10, 18, - 19, 82, 39, 68, 7, 34, 41, 32, 64, 15, - 57, 5, 9, 16, 22, 42, 24, 80, 77, 75, - 20, 11, 33, 36, 47, 37, 71, 73, 28, 60, - 45, 13, 27, 54, 46, 78, 76, 40, 17, 12, - 30, 8, 53, 72, 74, 43, 81, 29, 79, 51, - -1, 52, 50 -}; - -inline uint QMetaTypeStaticType::hash(register const char *str, register uint len) -{ - register int hval = len; - static AssoValues assoValues; - switch (hval) { - default: - hval += assoValues[(uchar)str[7]]; - /*FALLTHROUGH*/ - case 7: - case 6: - case 5: - case 4: - case 3: - case 2: - hval += assoValues[(uchar)str[1]]; - break; - } - return hval + assoValues[(uchar)str[len - 1]]; -} - -int QMetaTypeStaticType::type(register const char *str, register uint len) -{ - static ValuesList valueList; - if (len <= maxWordLength && len >= minWordLength) { - register uint key = hash(str, len); - if (key <= maxHashValue) - return valueList[key]; - } - return -1; -} - /*! \internal Similar to QMetaType::type(), but only looks in the static set of types. */ static inline int qMetaTypeStaticType(const char *typeName, int length) { - int idx = QMetaTypeStaticType::type(typeName, length); - if (idx < 0) - return QMetaType::Void; - if (types[idx].typeNameLength == length && !strcmp(typeName, types[idx].typeName)) - return types[idx].type; - return QMetaType::Void; + int i = 0; + while (types[i].typeName && ((length != types[i].typeNameLength) + || strcmp(typeName, types[i].typeName))) { + ++i; + } + return types[i].type; } /*! \internal diff --git a/tests/auto/qmetatype/tst_qmetatype.cpp b/tests/auto/qmetatype/tst_qmetatype.cpp index c39b652..eaa19d0 100644 --- a/tests/auto/qmetatype/tst_qmetatype.cpp +++ b/tests/auto/qmetatype/tst_qmetatype.cpp @@ -72,8 +72,6 @@ private slots: void normalizedTypes(); void typeName_data(); void typeName(); - void type_data(); - void type(); void construct(); void typedefs(); void isRegistered_data(); @@ -133,20 +131,14 @@ void tst_QMetaType::threadSafety() MetaTypeTorturer t1; MetaTypeTorturer t2; MetaTypeTorturer t3; - MetaTypeTorturer t4; - MetaTypeTorturer t5; t1.start(); t2.start(); t3.start(); - t4.start(); - t5.start(); QVERIFY(t1.wait()); QVERIFY(t2.wait()); QVERIFY(t3.wait()); - QVERIFY(t4.wait()); - QVERIFY(t5.wait()); } namespace TestSpace @@ -228,70 +220,14 @@ void tst_QMetaType::typeName_data() QTest::addColumn("aTypeName"); QTest::newRow("void") << QMetaType::Void << "void"; - QTest::newRow("bool") << QMetaType::Bool << "bool"; QTest::newRow("int") << QMetaType::Int << "int"; - QTest::newRow("uint") << QMetaType::UInt << "uint"; - QTest::newRow("qlonglong") << QMetaType::LongLong << "qlonglong"; - QTest::newRow("qulonglong") << QMetaType::ULongLong << "qulonglong"; QTest::newRow("double") << QMetaType::Double << "double"; - QTest::newRow("QChar") << QMetaType::QChar << "QChar"; - QTest::newRow("QVariantMap") << QMetaType::QVariantMap << "QVariantMap"; - QTest::newRow("QVariantList") << QMetaType::QVariantList << "QVariantList"; - QTest::newRow("QString") << QMetaType::QString << "QString"; - QTest::newRow("QStringList") << QMetaType::QStringList << "QStringList"; - QTest::newRow("QByteArray") << QMetaType::QByteArray << "QByteArray"; - QTest::newRow("QBitArray") << QMetaType::QBitArray << "QBitArray"; - QTest::newRow("QDate") << QMetaType::QDate << "QDate"; - QTest::newRow("QTime") << QMetaType::QTime << "QTime"; - QTest::newRow("QDateTime") << QMetaType::QDateTime << "QDateTime"; - QTest::newRow("QUrl") << QMetaType::QUrl << "QUrl"; - QTest::newRow("QLocale") << QMetaType::QLocale << "QLocale"; - QTest::newRow("QRect") << QMetaType::QRect << "QRect"; - QTest::newRow("QRectF") << QMetaType::QRectF << "QRectF"; - QTest::newRow("QSize") << QMetaType::QSize << "QSize"; - QTest::newRow("QSizeF") << QMetaType::QSizeF << "QSizeF"; - QTest::newRow("QLine") << QMetaType::QLine << "QLine"; - QTest::newRow("QLineF") << QMetaType::QLineF << "QLineF"; - QTest::newRow("QPoint") << QMetaType::QPoint << "QPoint"; - QTest::newRow("QPointF") << QMetaType::QPointF << "QPointF"; + QTest::newRow("qlonglong") << QMetaType::LongLong << "qlonglong"; QTest::newRow("QRegExp") << QMetaType::QRegExp << "QRegExp"; QTest::newRow("QColorGroup") << QMetaType::Type(63) << "QColorGroup"; - QTest::newRow("QVariantHash") << QMetaType::QVariantHash << "QVariantHash"; - QTest::newRow("QEasingCurve") << QMetaType::QEasingCurve << "QEasingCurve"; - QTest::newRow("QFont") << QMetaType::QFont << "QFont"; - QTest::newRow("QPixmap") << QMetaType::QPixmap << "QPixmap"; - QTest::newRow("QBrush") << QMetaType::QBrush << "QBrush"; - QTest::newRow("QColor") << QMetaType::QColor << "QColor"; - QTest::newRow("QPalette") << QMetaType::QPalette << "QPalette"; - QTest::newRow("QIcon") << QMetaType::QIcon << "QIcon"; - QTest::newRow("QImage") << QMetaType::QImage << "QImage"; - QTest::newRow("QPolygon") << QMetaType::QPolygon << "QPolygon"; - QTest::newRow("QRegion") << QMetaType::QRegion << "QRegion"; - QTest::newRow("QBitmap") << QMetaType::QBitmap << "QBitmap"; - QTest::newRow("QCursor") << QMetaType::QCursor << "QCursor"; - QTest::newRow("QSizePolicy") << QMetaType::QSizePolicy << "QSizePolicy"; - QTest::newRow("QKeySequence") << QMetaType::QKeySequence << "QKeySequence"; - QTest::newRow("QPen") << QMetaType::QPen << "QPen"; - QTest::newRow("QTextLength") << QMetaType::QTextLength << "QTextLength"; - QTest::newRow("QTextFormat") << QMetaType::QTextFormat << "QTextFormat"; - QTest::newRow("QMatrix") << QMetaType::QMatrix << "QMatrix"; - QTest::newRow("QTransform") << QMetaType::QTransform << "QTransform"; - QTest::newRow("QMatrix4x4") << QMetaType::QMatrix4x4 << "QMatrix4x4"; - QTest::newRow("QVector2D") << QMetaType::QVector2D << "QVector2D"; - QTest::newRow("QVector3D") << QMetaType::QVector3D << "QVector3D"; - QTest::newRow("QVector4D") << QMetaType::QVector4D << "QVector4D"; - QTest::newRow("QQuaternion") << QMetaType::QQuaternion << "QQuaternion"; QTest::newRow("void*") << QMetaType::VoidStar << "void*"; - QTest::newRow("long") << QMetaType::Long << "long"; - QTest::newRow("short") << QMetaType::Short << "short"; - QTest::newRow("char") << QMetaType::Char << "char"; QTest::newRow("ulong") << QMetaType::ULong << "ulong"; - QTest::newRow("ushort") << QMetaType::UShort << "ushort"; - QTest::newRow("uchar") << QMetaType::UChar << "uchar"; - QTest::newRow("float") << QMetaType::Float << "float"; - QTest::newRow("QObject*") << QMetaType::QObjectStar << "QObject*"; QTest::newRow("QWidget*") << QMetaType::QWidgetStar << "QWidget*"; - QTest::newRow("QVariant") << QMetaType::QVariant << "QVariant"; } void tst_QMetaType::typeName() @@ -302,19 +238,6 @@ void tst_QMetaType::typeName() QCOMPARE(QString::fromLatin1(QMetaType::typeName(aType)), aTypeName); } -void tst_QMetaType::type_data() -{ - typeName_data(); -} - -void tst_QMetaType::type() -{ - QFETCH(QMetaType::Type, aType); - QFETCH(QString, aTypeName); - - QCOMPARE(QMetaType::type(aTypeName.toLatin1().constData()), static_cast(aType)); -} - void tst_QMetaType::construct() { QSize x(1, 1); -- cgit v0.12