diff options
author | Lars Knoll <lars.knoll@qt.io> | 2019-11-21 11:59:45 +0100 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2020-07-06 21:30:12 +0200 |
commit | a8d5f3853741b6027658ad2c8a8d2b72a58de852 (patch) | |
tree | 296757bd43ef9f350354977a54296791b377ee4c /src/corelib/text | |
parent | a46caf087ca70a7482e065defa4d9547ae1335bd (diff) |
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 <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/text')
-rw-r--r-- | src/corelib/text/qbytearray.cpp | 16 | ||||
-rw-r--r-- | src/corelib/text/qstring.cpp | 21 | ||||
-rw-r--r-- | src/corelib/text/qstring.h | 2 |
3 files changed, 15 insertions, 24 deletions
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<Data *, char16_t *> 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<Data *, char16_t *> 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<const QChar *>(QString::Data::sharedNullData()); + return reinterpret_cast<const QChar *>(&QString::_empty); return m_string->unicode() + m_position; } inline const QChar *data() const { return unicode(); } |