diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2023-12-05 18:21:11 -0800 |
---|---|---|
committer | Thiago Macieira <thiago.macieira@intel.com> | 2023-12-21 11:13:32 -0800 |
commit | 5ea4e27661e1805a6ed01c7a61643f9a50c15d62 (patch) | |
tree | 442dbe25ae1ee1acc351842ac2e46750c0b88999 /src/corelib/text/qbytearrayview.h | |
parent | dfe968e9702c740b3522715fa9f0e9a281c92885 (diff) |
QByteArray: inline QByteArray::indexOf() and use a char overload
QByteArray::indexOf() had the best implementation of the two *because*
it was out-of-line, in qbytearray.cpp. The compiler could thus see the
body of the QtPrivate::findByteArray() function and inline it, with
constant-propagation, so only findCharHelper() was expanded and we had a
fast path towards memchr().
On the other hand, QByteArrayView::indexOf() was inline, so the
compiler emitted the call to QtPrivate::findByteArray() in user code,
causing the full function to be executed. We fix that by adding a char
overload for QtPrivate::findByteArray() and lastIndexOf(), so we get to
memchr() more quickly.
Also, inline QByteArray::indexOf() and lastIndexOf().
Before:
QByteArray::indexOf():
8.83287376 nsecs per iteration
22.01766832 CPU cycles per iteration, 2.49 GHz
62.00000330 instructions per iteration, 2.816 instr/cycle
21.00000281 branch instructions per iteration, 2.38 G/sec
QByteArrayView::indexOf():
9.64034694 nsecs per iteration
24.03001151 CPU cycles per iteration, 2.49 GHz
68.00000355 instructions per iteration, 2.830 instr/cycle
23.00000306 branch instructions per iteration, 2.39 G/sec
After (same result for both, requires recompilation):
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
The inlining appears to have cut 2 instructions, but the effect is not
measurable in this benchmark (the entire code is in cache and the Branch
Predictor is probably primed).
Fixes: QTBUG-119750
Change-Id: Ica7a43f6147b49c187ccfffd179e1cb4b306fc62
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
Diffstat (limited to 'src/corelib/text/qbytearrayview.h')
-rw-r--r-- | src/corelib/text/qbytearrayview.h | 4 |
1 files changed, 2 insertions, 2 deletions
diff --git a/src/corelib/text/qbytearrayview.h b/src/corelib/text/qbytearrayview.h index d8ad65ae1e..0541436df9 100644 --- a/src/corelib/text/qbytearrayview.h +++ b/src/corelib/text/qbytearrayview.h @@ -263,7 +263,7 @@ public: [[nodiscard]] qsizetype indexOf(QByteArrayView a, qsizetype from = 0) const noexcept { return QtPrivate::findByteArray(*this, from, a); } [[nodiscard]] qsizetype indexOf(char ch, qsizetype from = 0) const noexcept - { return QtPrivate::findByteArray(*this, from, QByteArrayView(&ch, 1)); } + { return QtPrivate::findByteArray(*this, from, ch); } [[nodiscard]] bool contains(QByteArrayView a) const noexcept { return indexOf(a) != qsizetype(-1); } @@ -275,7 +275,7 @@ public: [[nodiscard]] qsizetype lastIndexOf(QByteArrayView a, qsizetype from) const noexcept { return QtPrivate::lastIndexOf(*this, from, a); } [[nodiscard]] qsizetype lastIndexOf(char ch, qsizetype from = -1) const noexcept - { return QtPrivate::lastIndexOf(*this, from, QByteArrayView(&ch, 1)); } + { return QtPrivate::lastIndexOf(*this, from, ch); } [[nodiscard]] qsizetype count(QByteArrayView a) const noexcept { return QtPrivate::count(*this, a); } |