diff options
author | Dennis Oberst <dennis.oberst@qt.io> | 2023-08-09 14:53:23 +0200 |
---|---|---|
committer | Dennis Oberst <dennis.oberst@qt.io> | 2023-08-11 14:11:44 +0200 |
commit | 3c38efbfe46300ea480a842a2704e2f1a0665658 (patch) | |
tree | 03567c8c2c857a4d9c88500092a6d1f83ad2a9c0 /src/corelib/text/qbytearray.cpp | |
parent | 70835a93351b3288a8df4361e648b9cd951ad436 (diff) |
QByteArray: use new assign() in operator=(const char *)
operator=(~) and assign(~) share similar names but, until now, have not
shared the same functionality. This patch introduces the usage of
QByteArray::assign() within the non-sharing assignment operator to
effectively boost efficiency by reusing the available capacity.
Since these assignment operators are frequently used in many places,
both within Qt and non-Qt code, this patch comes with benchmarks.
The preview of the benchmark results are compared with this patch and
before this patch. The findings indicate a slight enhancement in
performance associated with the assignment operator. Despite the results
displaying only a minor improvement, progress has been made. Therefore
use assign(QByteArrayView) as replacement.
(x86_64-little_endian-lp64 shared (dynamic) release build (O3); by
gcc 13.2.1, endeavouros ; 13th Gen Intel(R) Core(TM) i9-13900K
benchmarks executed with -perf -iterations 1000000
* The last value at the EOL represent the string size.
QByteArray &operator=(const char *ch) (current)
65 cycles/iter; 317 instructions/iter; 16.0 nsec/iter (5)
71.7 cycles/iter; 383 instructions/iter; 13.0 nsec/iter (10)
59.8 cycles/iter; 318 instructions/iter; 10.9 nsec/iter (20)
70.8 cycles/iter; 340 instructions/iter; 12.9 nsec/iter (50)
80.2 cycles/iter; 419 instructions/iter; 14.6 nsec/iter (100)
164.2 cycles/iter; 899 instructions/iter; 29.9 nsec/iter (500)
260.5 cycles/iter; 1522 instructions/iter; 45.6 nsec/iter (1'000)
QByteArray &operator=(const char *ch) (before)
66.8 cycles/iter; 317 instructions/iter; 16.9 nsec/iter (5)
76.5 cycles/iter; 383 instructions/iter; 13.9 nsec/iter (10)
63.7 cycles/iter; 318 instructions/iter; 11.6 nsec/iter (20)
71.6 cycles/iter; 344 instructions/iter; 13.0 nsec/iter (50)
77.5 cycles/iter; 419 instructions/iter; 14.1 nsec/iter (100)
143.4 cycles/iter; 893 instructions/iter; 26.1 nsec/iter (500)
270.8 cycles/iter; 1516 instructions/iter; 48.2 nsec/iter (1'000)
Task-number: QTBUG-106201
Change-Id: I0745c33f0f61f1d844a60960cc55f565320d5945
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/text/qbytearray.cpp')
-rw-r--r-- | src/corelib/text/qbytearray.cpp | 9 |
1 files changed, 1 insertions, 8 deletions
diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp index 42921e99d7..4628c55ce5 100644 --- a/src/corelib/text/qbytearray.cpp +++ b/src/corelib/text/qbytearray.cpp @@ -1342,14 +1342,7 @@ QByteArray &QByteArray::operator=(const char *str) } else if (!*str) { d = DataPointer::fromRawData(&_empty, 0); } else { - const qsizetype len = qsizetype(strlen(str)); - const auto capacityAtEnd = d->allocatedCapacity() - d.freeSpaceAtBegin(); - if (d->needsDetach() || len > capacityAtEnd - || (len < size() && len < (capacityAtEnd >> 1))) - // ### inefficient! reallocData() does copy the old data and we then overwrite it in the next line - reallocData(len, QArrayData::KeepSize); - memcpy(d.data(), str, len + 1); // include null terminator - d.size = len; + assign(str); } return *this; } |