From 6de49f4ab6e6c8c6e664f7dd6c8c441bf7685a56 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Sun, 11 Oct 2015 15:01:06 +0200 Subject: QString: where possible, re-use existing capacity in op(QChar/QL1S) If the LHS is detached and has existing capacity that is large enough to hold the RHS, re-use the memory instead of allocating a new buffer and throwing away the old. Change-Id: I53d42825da92c264c7301e8e771cba9fb35c321b Reviewed-by: Thiago Macieira --- src/corelib/tools/qstring.cpp | 22 +++++++++++++++++++++- src/corelib/tools/qstring.h | 7 +------ 2 files changed, 22 insertions(+), 7 deletions(-) (limited to 'src/corelib/tools') diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp index d1b5327f5c..71d0be1dcf 100644 --- a/src/corelib/tools/qstring.cpp +++ b/src/corelib/tools/qstring.cpp @@ -1818,6 +1818,17 @@ QString &QString::operator=(const QString &other) Q_DECL_NOTHROW Assigns the Latin-1 string \a str to this string. */ +QString &QString::operator=(QLatin1String other) +{ + if (isDetached() && other.size() <= capacity()) { // assumes d->alloc == 0 → !isDetached() (sharedNull) + d->size = other.size(); + d->data()[other.size()] = 0; + qt_from_latin1(d->data(), other.latin1(), other.size()); + } else { + *this = fromLatin1(other.latin1(), other.size()); + } + return *this; +} /*! \fn QString &QString::operator=(const QByteArray &ba) @@ -1868,7 +1879,16 @@ QString &QString::operator=(const QString &other) Q_DECL_NOTHROW */ QString &QString::operator=(QChar ch) { - return operator=(QString(ch)); + if (isDetached() && capacity() >= 1) { // assumes d->alloc == 0 → !isDetached() (sharedNull) + // re-use existing capacity: + ushort *dat = d->data(); + dat[0] = ch.unicode(); + dat[1] = 0; + d->size = 1; + } else { + operator=(QString(ch)); + } + return *this; } /*! diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h index d21708efb9..1d04bcb457 100644 --- a/src/corelib/tools/qstring.h +++ b/src/corelib/tools/qstring.h @@ -221,7 +221,7 @@ public: inline ~QString(); QString &operator=(QChar c); QString &operator=(const QString &) Q_DECL_NOTHROW; - inline QString &operator=(QLatin1String latin1); + QString &operator=(QLatin1String latin1); #ifdef Q_COMPILER_RVALUE_REFS inline QString(QString && other) Q_DECL_NOTHROW : d(other.d) { other.d = Data::sharedNull(); } inline QString &operator=(QString &&other) Q_DECL_NOTHROW @@ -884,11 +884,6 @@ inline void QString::detach() { if (d->ref.isShared() || (d->offset != sizeof(QStringData))) reallocData(uint(d->size) + 1u); } inline bool QString::isDetached() const { return !d->ref.isShared(); } -inline QString &QString::operator=(QLatin1String s) -{ - *this = fromLatin1(s.latin1(), s.size()); - return *this; -} inline void QString::clear() { if (!isNull()) *this = QString(); } inline QString::QString(const QString &other) Q_DECL_NOTHROW : d(other.d) -- cgit v1.2.3