summaryrefslogtreecommitdiffstats
path: root/src/corelib/text/qstringview.h
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2023-12-05 19:58:33 -0800
committerThiago Macieira <thiago.macieira@intel.com>2023-12-21 11:13:32 -0800
commitb347d487048cf36d609dd31952dbef1813b5b0e5 (patch)
treebf8a14a71bcee9bb69587bf66840c74667611ed9 /src/corelib/text/qstringview.h
parent5ea4e27661e1805a6ed01c7a61643f9a50c15d62 (diff)
QString/QByteArray: further inline the most common indexOf operations
In the case of QString, the vast majority of searches are case- sensitive, so by inlining we make the user code call qustrchr() directly instead of QtPrivate::findString(). In the case of QByteArray, the call is to memchr(), which being a compiler intrinsic, may itself be inlined or even just resolved at compile time. In both cases, a great deal of searches use from=0. Benchmark for QByteArray; before: 8.83207052 nsecs per iteration 22.01568546 CPU cycles per iteration, 2.49 GHz 60.00000331 instructions per iteration, 2.725 instr/cycle 21.00000281 branch instructions per iteration, 2.38 G/sec After: 6.42561493 nsecs per iteration 16.01623130 CPU cycles per iteration, 2.49 GHz 49.00000261 instructions per iteration, 3.059 instr/cycle 18.00000211 branch instructions per iteration, 2.8 G/sec This shaves 6 cycles in the execution and 11 instructions (3 of which were branches), slightly improving the IPC raito, for the QByteArray case. For QByteArrayView, there are 2 fewer instructions (1 a branch), but the number of cycles is the same at 16. Task-number: QTBUG-119750 Change-Id: Ica7a43f6147b49c187ccfffd179e2204ebb6a348 Reviewed-by: Marc Mutz <marc.mutz@qt.io>
Diffstat (limited to 'src/corelib/text/qstringview.h')
-rw-r--r--src/corelib/text/qstringview.h21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/corelib/text/qstringview.h b/src/corelib/text/qstringview.h
index a598d7a445..76acbd92aa 100644
--- a/src/corelib/text/qstringview.h
+++ b/src/corelib/text/qstringview.h
@@ -456,6 +456,27 @@ inline QStringView qToStringViewIgnoringNull(const QStringLike &s) noexcept
R{{char16_t(c), u'\0'}} ;
}
+qsizetype QtPrivate::findString(QStringView str, qsizetype from, QChar ch, Qt::CaseSensitivity cs) noexcept
+{
+ if (from < -str.size()) // from < 0 && abs(from) > str.size(), avoiding overflow
+ return -1;
+ if (from < 0)
+ from = qMax(from + str.size(), qsizetype(0));
+ if (from < str.size()) {
+ const char16_t *s = str.utf16();
+ char16_t c = ch.unicode();
+ const char16_t *n = s + from;
+ const char16_t *e = s + str.size();
+ if (cs == Qt::CaseSensitive)
+ n = qustrchr(QStringView(n, e), c);
+ else
+ n = qustrcasechr(QStringView(n, e), c);
+ if (n != e)
+ return n - s;
+ }
+ return -1;
+}
+
QT_END_NAMESPACE
#endif /* QSTRINGVIEW_H */