summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorKent Hansen <khansen@trolltech.com>2009-05-22 12:23:15 (GMT)
committerKent Hansen <khansen@trolltech.com>2009-05-22 12:23:15 (GMT)
commita0fb8f874d0b8b8db5f50706a432bdb5a0294bf0 (patch)
tree8572f5ba08537661a62bfb36cff8c8e9fe27381e /src
parent6f67fcf54de82c65c5f36ac10dfb6e41b550c296 (diff)
parent1ff30b2b88af31cf18d2a1e6961c3ed7bd9b0240 (diff)
downloadQt-a0fb8f874d0b8b8db5f50706a432bdb5a0294bf0.zip
Qt-a0fb8f874d0b8b8db5f50706a432bdb5a0294bf0.tar.gz
Qt-a0fb8f874d0b8b8db5f50706a432bdb5a0294bf0.tar.bz2
Merge branch 'master' of git@scm.dev.nokia.troll.no:qt/qt
Diffstat (limited to 'src')
-rw-r--r--src/corelib/tools/qstring.cpp78
1 files changed, 72 insertions, 6 deletions
diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp
index c3649e3..29509c5 100644
--- a/src/corelib/tools/qstring.cpp
+++ b/src/corelib/tools/qstring.cpp
@@ -195,6 +195,68 @@ static int ucstrnicmp(const ushort *a, const ushort *b, int l)
return ucstricmp(a, a + l, b, b + l);
}
+static bool qMemEquals(const quint16 *a, const quint16 *b, int length)
+{
+ // Benchmarking indicates that doing memcmp is much slower than
+ // executing the comparison ourselves.
+ // To make it even faster, we do a 32-bit comparison, comparing
+ // twice the amount of data as a normal word-by-word comparison.
+ //
+ // Benchmarking results on a 2.33 GHz Core2 Duo, with a 64-QChar
+ // block of data, with 4194304 iterations (per iteration):
+ // operation usec cpu ticks
+ // memcmp 330 710
+ // 16-bit 79 167-171
+ // 32-bit aligned 49 105-109
+ //
+ // Testing also indicates that unaligned 32-bit loads are as
+ // performant as 32-bit aligned.
+ if (a == b || !length)
+ return true;
+
+ register union {
+ const quint16 *w;
+ const quint32 *d;
+ quintptr value;
+ } sa, sb;
+ sa.w = a;
+ sb.w = b;
+
+ // check alignment
+ if ((sa.value & 2) == (sb.value & 2)) {
+ // both addresses have the same alignment
+ if (sa.value & 2) {
+ // both addresses are not aligned to 4-bytes boundaries
+ // compare the first character
+ if (*sa.w != *sb.w)
+ return false;
+ --length;
+ ++sa.w;
+ ++sb.w;
+
+ // now both addresses are 4-bytes aligned
+ }
+
+ // both addresses are 4-bytes aligned
+ // do a fast 32-bit comparison
+ register const quint32 *e = sa.d + (length >> 1);
+ for ( ; sa.d != e; ++sa.d, ++sb.d) {
+ if (*sa.d != *sb.d)
+ return false;
+ }
+
+ // do we have a tail?
+ return (length & 1) ? *sa.w == *sb.w : true;
+ } else {
+ // one of the addresses isn't 4-byte aligned but the other is
+ register const quint16 *e = sa.w + length;
+ for ( ; sa.w != e; ++sa.w, ++sb.w) {
+ if (*sa.w != *sb.w)
+ return false;
+ }
+ }
+ return true;
+}
/*!
\internal
@@ -1908,8 +1970,10 @@ QString &QString::replace(QChar c, const QLatin1String &after, Qt::CaseSensitivi
*/
bool QString::operator==(const QString &other) const
{
- return (size() == other.size()) &&
- (memcmp((char*)unicode(),(char*)other.unicode(), size()*sizeof(QChar))==0);
+ if (d->size != other.d->size)
+ return false;
+
+ return qMemEquals(d->data, other.d->data, d->size);
}
/*!
@@ -3136,7 +3200,7 @@ bool QString::startsWith(const QString& s, Qt::CaseSensitivity cs) const
if (s.d->size > d->size)
return false;
if (cs == Qt::CaseSensitive) {
- return memcmp((char*)d->data, (char*)s.d->data, s.d->size*sizeof(QChar)) == 0;
+ return qMemEquals(d->data, s.d->data, s.d->size);
} else {
uint last = 0;
uint olast = 0;
@@ -3207,7 +3271,7 @@ bool QString::endsWith(const QString& s, Qt::CaseSensitivity cs) const
if (pos < 0)
return false;
if (cs == Qt::CaseSensitive) {
- return memcmp((char*)&d->data[pos], (char*)s.d->data, s.d->size*sizeof(QChar)) == 0;
+ return qMemEquals(d->data + pos, s.d->data, s.d->size);
} else {
uint last = 0;
uint olast = 0;
@@ -7692,7 +7756,8 @@ QString QStringRef::toString() const {
*/
bool operator==(const QStringRef &s1,const QStringRef &s2)
{ return (s1.size() == s2.size() &&
- (memcmp((char*)s1.unicode(), (char*)s2.unicode(), s1.size()*sizeof(QChar))==0)); }
+ qMemEquals((const ushort *)s1.unicode(), (const ushort *)s2.unicode(), s1.size()));
+}
/*! \relates QStringRef
@@ -7701,7 +7766,8 @@ bool operator==(const QStringRef &s1,const QStringRef &s2)
*/
bool operator==(const QString &s1,const QStringRef &s2)
{ return (s1.size() == s2.size() &&
- (memcmp((char*)s1.unicode(), (char*)s2.unicode(), s1.size()*sizeof(QChar))==0)); }
+ qMemEquals((const ushort *)s1.unicode(), (const ushort *)s2.unicode(), s1.size()));
+}
/*! \relates QStringRef