Boost performance of QChar::isSpace
authorKent Hansen <kent.hansen@nokia.com>
Wed, 14 Sep 2011 12:11:17 +0000 (14:11 +0200)
committerQt by Nokia <qt-info@nokia.com>
Thu, 15 Sep 2011 18:39:28 +0000 (20:39 +0200)
Make it inline; add fast checks for typical spaces;
add fallback function that uses the fastcall calling
convention.

On ia32, this change makes isSpace ~340x faster for
ascii spaces, ~170x faster for non-space ascii
characters, and ~1.3x faster for non-ascii characters.

Note that this change is NOT binary compatible.

Also add an autotest with expected results from
before the optimization, to ensure that the behavior
is the same.

Change-Id: I9438d0ad3c9ba2e80560c4bee7eed05115265798
Reviewed-on: http://codereview.qt-project.org/4905
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
src/corelib/tools/qchar.cpp
src/corelib/tools/qchar.h
tests/auto/corelib/tools/qchar/tst_qchar.cpp

index 56773bd..a60fac4 100644 (file)
@@ -511,18 +511,23 @@ bool QChar::isPrint() const
 }
 
 /*!
+    \fn bool QChar::isSpace() const
+
     Returns true if the character is a separator character
     (Separator_* categories or certain code points from Other_Control category);
     otherwise returns false.
 */
-bool QChar::isSpace() const
+
+/*!
+    \internal
+    \overload
+*/
+bool QChar::isSpace(ushort ucs2)
 {
-    if(ucs >= 9 && ucs <=13)
-        return true;
     const int test = FLAG(Separator_Space) |
                      FLAG(Separator_Line) |
                      FLAG(Separator_Paragraph);
-    return FLAG(qGetProp(ucs)->category) & test;
+    return FLAG(qGetProp(ucs2)->category) & test;
 }
 
 /*!
index 36e185e..b82addb 100644 (file)
@@ -232,7 +232,10 @@ public:
     inline bool isNull() const { return ucs == 0; }
     bool isPrint() const;
     bool isPunct() const;
-    bool isSpace() const;
+    inline bool isSpace() const {
+        return ucs == 0x20 || (ucs <= 0x0D && ucs >= 0x09)
+                || (ucs > 127 && isSpace(ucs));
+    }
     bool isMark() const;
     inline bool isLetter() const {
         return (ucs >= 'a' && ucs <= 'z')
@@ -329,6 +332,7 @@ private:
     static bool QT_FASTCALL isDigit(ushort ucs2);
     static bool QT_FASTCALL isLetter(ushort ucs2);
     static bool QT_FASTCALL isLetterOrNumber(ushort ucs2);
+    static bool QT_FASTCALL isSpace(ushort ucs2);
 
 #ifdef QT_NO_CAST_FROM_ASCII
     QChar(char c);
index f16d32a..195abcd 100644 (file)
@@ -82,6 +82,8 @@ private slots:
     void isPrint();
     void isUpper();
     void isLower();
+    void isSpace_data();
+    void isSpace();
     void isTitle();
     void category();
     void direction();
@@ -335,6 +337,25 @@ void tst_QChar::isLower()
     }
 }
 
+void tst_QChar::isSpace_data()
+{
+    QTest::addColumn<ushort>("ucs");
+    QTest::addColumn<bool>("expected");
+
+    for (ushort ucs = 0; ucs < 256; ++ucs) {
+        bool isSpace = (ucs <= 0x0D && ucs >= 0x09) || ucs == 0x20 || ucs == 0xA0;
+        QString tag = QString::fromLatin1("0x%0").arg(QString::number(ucs, 16));
+        QTest::newRow(tag.toLatin1()) << ucs << isSpace;
+    }
+}
+
+void tst_QChar::isSpace()
+{
+    QFETCH(ushort, ucs);
+    QFETCH(bool, expected);
+    QCOMPARE(QChar(ucs).isSpace(), expected);
+}
+
 void tst_QChar::isTitle()
 {
     for (uint codepoint = 0; codepoint <= UNICODE_LAST_CODEPOINT; ++codepoint) {