Optimize QString::insert()

When the insertion position is not beyond end(), call
resize() instead of expand(), which fills the new size
with spaces, which, however would just be overwritten
by the following memmove().

Add some Q_UNLIKELY to indicate that we strongly
expect the resize() case to be the more common.

Change-Id: Iaf3215dd53c2cbd18f2fd8a5f80af8f6844944da
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Anton Kudryavtsev <a.kudryavtsev@netris.ru>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Marc Mutz 2016-07-12 12:37:16 +02:00
parent 49f9328175
commit 5c46d07a7c

View File

@ -1985,7 +1985,10 @@ QString &QString::insert(int i, QLatin1String str)
return *this;
int len = str.size();
expand(qMax(d->size, i) + len - 1);
if (Q_UNLIKELY(i > d->size))
expand(i + len - 1);
else
resize(d->size + len);
::memmove(d->data() + i + len, d->data() + i, (d->size - i - len) * sizeof(QChar));
qt_from_latin1(d->data() + i, s, uint(len));
@ -2015,7 +2018,10 @@ QString& QString::insert(int i, const QChar *unicode, int size)
return *this;
}
expand(qMax(d->size, i) + size - 1);
if (Q_UNLIKELY(i > d->size))
expand(i + size - 1);
else
resize(d->size + size);
::memmove(d->data() + i + size, d->data() + i, (d->size - i - size) * sizeof(QChar));
memcpy(d->data() + i, s, size * sizeof(QChar));
@ -2035,7 +2041,10 @@ QString& QString::insert(int i, QChar ch)
i += d->size;
if (i < 0)
return *this;
expand(qMax(i, d->size));
if (Q_UNLIKELY(i > d->size))
expand(i);
else
resize(d->size + 1);
::memmove(d->data() + i + 1, d->data() + i, (d->size - i - 1) * sizeof(QChar));
d->data()[i] = ch.unicode();
return *this;