diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2023-12-05 19:58:33 -0800 |
---|---|---|
committer | Thiago Macieira <thiago.macieira@intel.com> | 2023-12-21 11:13:32 -0800 |
commit | b347d487048cf36d609dd31952dbef1813b5b0e5 (patch) | |
tree | bf8a14a71bcee9bb69587bf66840c74667611ed9 /src/corelib/text/qbytearrayview.h | |
parent | 5ea4e27661e1805a6ed01c7a61643f9a50c15d62 (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/qbytearrayview.h')
-rw-r--r-- | src/corelib/text/qbytearrayview.h | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/src/corelib/text/qbytearrayview.h b/src/corelib/text/qbytearrayview.h index 0541436df9..3ebc2f841c 100644 --- a/src/corelib/text/qbytearrayview.h +++ b/src/corelib/text/qbytearrayview.h @@ -361,6 +361,20 @@ inline quint16 qChecksum(const char *s, qsizetype len, { return qChecksum(QByteArrayView(s, len), standard); } #endif +qsizetype QtPrivate::findByteArray(QByteArrayView haystack, qsizetype from, char needle) noexcept +{ + if (from < 0) + from = qMax(from + haystack.size(), qsizetype(0)); + if (from < haystack.size()) { + const char *const b = haystack.data(); + if (const auto n = static_cast<const char *>( + memchr(b + from, needle, static_cast<size_t>(haystack.size() - from)))) { + return n - b; + } + } + return -1; +} + QT_END_NAMESPACE #endif // QBYTEARRAYVIEW_H |