summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAhmad Samir <a.samirh78@gmail.com>2022-10-27 11:34:05 +0200
committerThiago Macieira <thiago.macieira@intel.com>2022-11-17 02:15:28 +0000
commitd9637d07816af472bd40bb8e6e9f63f1c48085fe (patch)
treefc3fc7a05fcd452043666522f4b0f458a7d6dc35 /src
parentd27360818d78adc412c4fb7012c8d5c0b2f38d98 (diff)
QString: don't detach in removeStringImpl()
- If this string is not shared, modify it directly - If this string is shared, instead of detaching copy the characters from this string, except the ones that are going to be removed, to a new string and swap it. This is more efficient than detaching, which would copy the whole string including the characters that are going to be removed. This affects: remove(const QString &str, Qt::CaseSensitivity cs) remove(QLatin1StringView str, Qt::CaseSensitivity cs) Adjust the unittests to test both code paths. [ChangeLog][QtCore][QString] Improved the performance of QString::remove() by avoiding unnecessary data copying. Now, if this string is (implicitly) shared with another, instead of copying everything and then removing what we don't want, the characters from this string are copied to the destination, except the ones that need to be removed. Task-number: QTBUG-106181 Change-Id: Id8eba59a44bab641cc8aa662eb45063faf201183 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src')
-rw-r--r--src/corelib/text/qstring.cpp40
1 files changed, 27 insertions, 13 deletions
diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp
index 2ac8306eb4..3ec9682935 100644
--- a/src/corelib/text/qstring.cpp
+++ b/src/corelib/text/qstring.cpp
@@ -3325,19 +3325,33 @@ static void removeStringImpl(QString &s, const T &needle, Qt::CaseSensitivity cs
if (i < 0)
return;
- auto beg = s.begin(); // detaches
- auto dst = beg + i;
- auto src = beg + i + needleSize;
- auto end = s.end();
- // loop invariant: [beg, dst[ is partial result
- // [src, end[ still to be checked for needles
- while (src < end) {
- i = s.indexOf(needle, std::distance(beg, src), cs);
- auto hit = i == -1 ? end : beg + i;
- dst = std::copy(src, hit, dst);
- src = hit + needleSize;
- }
- s.truncate(std::distance(beg, dst));
+ QString::DataPointer &dptr = s.data_ptr();
+ auto begin = dptr.begin();
+ auto end = dptr.end();
+
+ auto copyFunc = [&](auto &dst) {
+ auto src = begin + i + needleSize;
+ while (src < end) {
+ i = s.indexOf(needle, std::distance(begin, src), cs);
+ auto hit = i == -1 ? end : begin + i;
+ dst = std::copy(src, hit, dst);
+ src = hit + needleSize;
+ }
+ return dst;
+ };
+
+ if (!dptr->needsDetach()) {
+ auto dst = begin + i;
+ dst = copyFunc(dst);
+ s.truncate(std::distance(begin, dst));
+ } else {
+ QString copy{s.size(), Qt::Uninitialized};
+ auto copy_begin = copy.begin();
+ auto dst = std::copy(begin, begin + i, copy_begin); // Chunk before the first hit
+ dst = copyFunc(dst);
+ copy.resize(std::distance(copy_begin, dst));
+ s.swap(copy);
+ }
}
/*!