From a8d5f3853741b6027658ad2c8a8d2b72a58de852 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Thu, 21 Nov 2019 11:59:45 +0100 Subject: Avoid allocating a d-pointer for empty strings Those can simply be handled as compile time constant strings pointing to the empty (Q)Char. Change-Id: I1f6f6ab923a30c68a720003ca68c34c572aa29da Reviewed-by: Thiago Macieira --- src/corelib/text/qbytearray.cpp | 16 ++++++---------- src/corelib/text/qstring.cpp | 21 ++++++++------------- src/corelib/text/qstring.h | 2 +- 3 files changed, 15 insertions(+), 24 deletions(-) (limited to 'src/corelib/text') diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp index 26e3b64c9d..350fa3be25 100644 --- a/src/corelib/text/qbytearray.cpp +++ b/src/corelib/text/qbytearray.cpp @@ -1149,12 +1149,10 @@ QByteArray &QByteArray::operator=(const QByteArray & other) noexcept QByteArray &QByteArray::operator=(const char *str) { - if (!str || !*str) { - if (!str) { - d.clear(); - } else { - d = QByteArrayData(Data::allocate(0), 0); - } + if (!str) { + d.clear(); + } else if (!*str) { + d = DataPointer::fromRawData(&_empty, 0); } else { const int len = int(strlen(str)); const uint fullLen = uint(len) + 1; @@ -1612,7 +1610,7 @@ QByteArray::QByteArray(const char *data, int size) QByteArray::QByteArray(int size, char ch) { if (size <= 0) { - d = DataPointer(Data::allocate(0), 0); + d = DataPointer::fromRawData(&_empty, 0); } else { d = DataPointer(Data::allocate(uint(size) + 1u), size); memset(d.data(), ch, size); @@ -2885,9 +2883,7 @@ QByteArray QByteArray::mid(int pos, int len) const return QByteArray(); case QContainerImplHelper::Empty: { - auto alloc = Data::allocate(0); - QByteArray::DataPointer empty = { alloc.first, alloc.second, 0 }; - return QByteArray(empty); + return QByteArray(DataPointer::fromRawData(&_empty, 0)); } case QContainerImplHelper::Full: return *this; diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index 985c00ae6b..8e572691c6 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -2130,7 +2130,7 @@ QString::QString(const QChar *unicode, int size) ++size; } if (!size) { - d = DataPointer(Data::allocate(0), 0); + d = DataPointer::fromRawData(&_empty, 0); } else { d = DataPointer(Data::allocate(size + 1), size); memcpy(d.data(), unicode, size * sizeof(QChar)); @@ -2148,7 +2148,7 @@ QString::QString(const QChar *unicode, int size) QString::QString(int size, QChar ch) { if (size <= 0) { - d = DataPointer(Data::allocate(0), 0); + d = DataPointer::fromRawData(&_empty, 0); } else { d = DataPointer(Data::allocate(size + 1), size); d.data()[size] = '\0'; @@ -4548,11 +4548,7 @@ QString QString::mid(int position, int n) const case QContainerImplHelper::Null: return QString(); case QContainerImplHelper::Empty: - { - QPair pair = Data::allocate(0); - DataPointer empty = { pair.first, pair.second, 0 }; - return QString(std::move(empty)); - } + return QString(DataPointer::fromRawData(&_empty, 0)); case QContainerImplHelper::Full: return *this; case QContainerImplHelper::Subset: @@ -5010,7 +5006,7 @@ QString::DataPointer QString::fromLatin1_helper(const char *str, int size) if (!str) { // nothing to do } else if (size == 0 || (!*str && size < 0)) { - d = DataPointer(Data::allocate(0), 0); + d = DataPointer::fromRawData(&_empty, 0); } else { if (size < 0) size = qstrlen(str); @@ -5065,11 +5061,10 @@ QString QString::fromLocal8Bit_helper(const char *str, int size) { if (!str) return QString(); - if (size == 0 || (!*str && size < 0)) { - QPair pair = Data::allocate(0); - QString::DataPointer empty = { pair.first, pair.second, 0 }; - return QString(std::move(empty)); - } + if (size < 0) + size = qstrlen(str); + if (size == 0) + return QString(DataPointer::fromRawData(&_empty, 0)); QStringDecoder toUtf16(QStringDecoder::System, QStringDecoder::Flag::Stateless); return toUtf16(str, size); } diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h index da553ddde4..2db2e8cb85 100644 --- a/src/corelib/text/qstring.h +++ b/src/corelib/text/qstring.h @@ -1469,7 +1469,7 @@ public: inline const QChar *unicode() const { if (!m_string) - return reinterpret_cast(QString::Data::sharedNullData()); + return reinterpret_cast(&QString::_empty); return m_string->unicode() + m_position; } inline const QChar *data() const { return unicode(); } -- cgit v1.2.3