Boost performance of QChar::isLetter

Make it inline; add fast checks for ascii letters;
add fallback function that uses the fastcall calling
convention.

On ia32, this change makes isLetter ~370x faster for
ascii letters, ~250x faster for non-letter ascii
characters, and ~1.5x 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: I06f8d3d43114537cee5567e670898cef6494c20a
Reviewed-on: http://codereview.qt-project.org/4903
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
This commit is contained in:
Kent Hansen 2011-09-14 13:38:39 +02:00 committed by Qt by Nokia
parent 5639423ea7
commit a48e9af146
3 changed files with 44 additions and 3 deletions

View File

@ -556,17 +556,24 @@ bool QChar::isPunct() const
}
/*!
\fn bool QChar::isLetter() const
Returns true if the character is a letter (Letter_* categories);
otherwise returns false.
*/
bool QChar::isLetter() const
/*!
\internal
\overload
*/
bool QChar::isLetter(ushort ucs2)
{
const int test = FLAG(Letter_Uppercase) |
FLAG(Letter_Lowercase) |
FLAG(Letter_Titlecase) |
FLAG(Letter_Modifier) |
FLAG(Letter_Other);
return FLAG(qGetProp(ucs)->category) & test;
return FLAG(qGetProp(ucs2)->category) & test;
}
/*!

View File

@ -234,7 +234,11 @@ public:
bool isPunct() const;
bool isSpace() const;
bool isMark() const;
bool isLetter() const;
inline bool isLetter() const {
return (ucs >= 'a' && ucs <= 'z')
|| (ucs <= 'Z' && ucs >= 'A')
|| (ucs > 127 && isLetter(ucs));
}
bool isNumber() const;
bool isLetterOrNumber() const;
inline bool isDigit() const
@ -317,6 +321,7 @@ public:
private:
static bool QT_FASTCALL isDigit(ushort ucs2);
static bool QT_FASTCALL isLetter(ushort ucs2);
#ifdef QT_NO_CAST_FROM_ASCII
QChar(char c);

View File

@ -75,6 +75,8 @@ private slots:
void toCaseFolded();
void isDigit_data();
void isDigit();
void isLetter_data();
void isLetter();
void isPrint();
void isUpper();
void isLower();
@ -244,6 +246,33 @@ void tst_QChar::isDigit()
QCOMPARE(QChar(ucs).isDigit(), expected);
}
static bool isExpectedLetter(ushort ucs)
{
return (ucs >= 'a' && ucs <= 'z') || (ucs >= 'A' && ucs <= 'Z')
|| ucs == 0xAA || ucs == 0xB5 || ucs == 0xBA
|| (ucs >= 0xC0 && ucs <= 0xD6)
|| (ucs >= 0xD8 && ucs <= 0xF6)
|| (ucs >= 0xF8 && ucs <= 0xFF);
}
void tst_QChar::isLetter_data()
{
QTest::addColumn<ushort>("ucs");
QTest::addColumn<bool>("expected");
for (ushort ucs = 0; ucs < 256; ++ucs) {
QString tag = QString::fromLatin1("0x%0").arg(QString::number(ucs, 16));
QTest::newRow(tag.toLatin1()) << ucs << isExpectedLetter(ucs);
}
}
void tst_QChar::isLetter()
{
QFETCH(ushort, ucs);
QFETCH(bool, expected);
QCOMPARE(QChar(ucs).isLetter(), expected);
}
void tst_QChar::isPrint()
{
QVERIFY(QChar('A').isPrint());