QString: optimize compare_helper

For a long time now we have fast mixed UTF-16/UTF-8 comparisons. But
no-one told this ol' relic, which still converted UTF-8 to UTF-16 for
comparison.

Fix by using QtPrivate::compareStrings(QSV, QU8SV), which, as the
central entry point, uses the fast-path.

Consequently, compare_helper can now be noexcept.

Pick-to: 6.3 6.2
Change-Id: I4cc9f07d9bc48628f1fe695e80015a9a07b79d6f
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Marc Mutz 2022-01-26 14:00:20 +01:00
parent 88fd9a281a
commit cd3569d69a
2 changed files with 4 additions and 7 deletions

View File

@ -6267,7 +6267,7 @@ int QString::compare(QLatin1String other, Qt::CaseSensitivity cs) const noexcept
\since 5.0 \since 5.0
*/ */
int QString::compare_helper(const QChar *data1, qsizetype length1, const char *data2, qsizetype length2, int QString::compare_helper(const QChar *data1, qsizetype length1, const char *data2, qsizetype length2,
Qt::CaseSensitivity cs) Qt::CaseSensitivity cs) noexcept
{ {
Q_ASSERT(length1 >= 0); Q_ASSERT(length1 >= 0);
Q_ASSERT(data1 || length1 == 0); Q_ASSERT(data1 || length1 == 0);
@ -6275,11 +6275,8 @@ int QString::compare_helper(const QChar *data1, qsizetype length1, const char *d
return length1; return length1;
if (Q_UNLIKELY(length2 < 0)) if (Q_UNLIKELY(length2 < 0))
length2 = qsizetype(strlen(data2)); length2 = qsizetype(strlen(data2));
// ### make me nothrow in all cases return QtPrivate::compareStrings(QStringView(data1, length1),
QVarLengthArray<ushort> s2(length2); QUtf8StringView(data2, length2), cs);
const auto beg = reinterpret_cast<QChar *>(s2.data());
const auto end = QUtf8::convertToUnicode(beg, QByteArrayView(data2, length2));
return QtPrivate::compareStrings(QStringView(data1, length1), QStringView(beg, end - beg), cs);
} }
/*! /*!

View File

@ -1106,7 +1106,7 @@ private:
Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept; Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
static int compare_helper(const QChar *data1, qsizetype length1, static int compare_helper(const QChar *data1, qsizetype length1,
const char *data2, qsizetype length2, const char *data2, qsizetype length2,
Qt::CaseSensitivity cs = Qt::CaseSensitive); Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
static int localeAwareCompare_helper(const QChar *data1, qsizetype length1, static int localeAwareCompare_helper(const QChar *data1, qsizetype length1,
const QChar *data2, qsizetype length2); const QChar *data2, qsizetype length2);
static QString toLower_helper(const QString &str); static QString toLower_helper(const QString &str);