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>
This commit is contained in:
Lars Knoll 2019-11-21 11:59:45 +01:00
parent a46caf087c
commit a8d5f38537
3 changed files with 15 additions and 24 deletions

View File

@ -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;

View File

@ -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);
}

View File

@ -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(); }