Optimize QString::compare_helper(QChar*, int, char*, int, cs)

... by using the recently-added QUtf8::convertToUnicode()
and a QVarLengthArray instead of QString::fromUtf8().

Like elsewhere in QString, use a QVarLengthArray<ushort>
instead of the more natural <QChar> to avoid instantiating
another QVLA.

Assume that length2 is usually set to a non-negative value.
Not because that's necessarily the frequent case, but
because a negative length2 leads to an expensive strlen,
that usually dwarfs the additional branch cost.

Check for data2 == nullptr early to avoid having to check
it later twice.

Change-Id: I04bda44ed857451efdf04c3283b5726480dd8c0d
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Marc Mutz 2016-06-08 11:17:11 +02:00 committed by Simon Hausmann
parent 31520c4fc0
commit 4a40c717f3

View File

@ -5450,9 +5450,15 @@ int QString::compare(QLatin1String other, Qt::CaseSensitivity cs) const Q_DECL_N
int QString::compare_helper(const QChar *data1, int length1, const char *data2, int length2,
Qt::CaseSensitivity cs)
{
// ### optimize me
const QString s2 = QString::fromUtf8(data2, length2 == -1 ? (data2 ? int(strlen(data2)) : -1) : length2);
return compare_helper(data1, length1, s2.constData(), s2.size(), cs);
if (!data2)
return int(bool(data1));
if (Q_UNLIKELY(length2 < 0))
length2 = int(strlen(data2));
// ### make me nothrow in all cases
QVarLengthArray<ushort> s2(length2);
const auto beg = reinterpret_cast<QChar *>(s2.data());
const auto end = QUtf8::convertToUnicode(beg, data2, length2);
return compare_helper(data1, length1, beg, end - beg, cs);
}
/*!