From aa504fc2fa764b44d37d9629b9ddf1f114210759 Mon Sep 17 00:00:00 2001 From: David Faure Date: Mon, 2 Dec 2019 21:16:24 +0100 Subject: Optimize qLastIndexOf to not detach the QString The call to data() on a non-const QString led to a detach(), which is unexpected and unwanted from QString::lastIndexOf() const. Found by looking at why QFileSystemEntry::fileName() was expensive, in the hotspot profiler. The solution is to instanciate QLastIndexOf with QStringView() rather than QString(). I added a deleted QString overload to make sure nobody ever instanciates it with a QString argument again. Change-Id: I06a1b2f937425e83f0779eb215e099aef78c50a7 Reviewed-by: Anton Kudryavtsev Reviewed-by: Thiago Macieira --- src/corelib/text/qstring.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src/corelib/text/qstring.cpp') diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index 51aa0b7512..4d83f19db7 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -146,6 +146,9 @@ qsizetype qFindStringBoyerMoore(QStringView haystack, qsizetype from, QStringVie static inline qsizetype qFindChar(QStringView str, QChar ch, qsizetype from, Qt::CaseSensitivity cs) noexcept; template static inline qsizetype qLastIndexOf(Haystack haystack, QChar needle, qsizetype from, Qt::CaseSensitivity cs) noexcept; +template <> +inline qsizetype qLastIndexOf(QString haystack, QChar needle, + qsizetype from, Qt::CaseSensitivity cs) noexcept = delete; // unwanted, would detach static inline qsizetype qt_string_count(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs); static inline qsizetype qt_string_count(QStringView haystack, QChar needle, Qt::CaseSensitivity cs); @@ -3817,7 +3820,7 @@ int QString::indexOf(const QStringRef &str, int from, Qt::CaseSensitivity cs) co int QString::lastIndexOf(const QString &str, int from, Qt::CaseSensitivity cs) const { // ### Qt6: qsizetype - return int(QtPrivate::lastIndexOf(*this, from, str, cs)); + return int(QtPrivate::lastIndexOf(QStringView(*this), from, str, cs)); } #endif // QT_STRINGVIEW_LEVEL < 2 @@ -3856,7 +3859,7 @@ int QString::lastIndexOf(QLatin1String str, int from, Qt::CaseSensitivity cs) co int QString::lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const { // ### Qt6: qsizetype - return int(qLastIndexOf(*this, ch, from, cs)); + return int(qLastIndexOf(QStringView(*this), ch, from, cs)); } #if QT_STRINGVIEW_LEVEL < 2 -- cgit v1.2.3