QCborStreamReader: micro optimization: use constBegin() not constData()

Both QString and QByteArray's constData() have a check for null and
return the address of the corresponding _empty. constBegin() has no such
check and is therefore marginally faster because it could return null.

It will never in any of these conditions. Our internal buffer is never
empty at the places we're trying to get to its data; the params->string
and params->array have just been resized, so they are detached from null
even if newSize == 0. That leaves one case of a nullptr coming from the
user, in addData(), but QByteArray::append(nullptr, 0) works just fine
too.

Change-Id: I799e5c06638d393df793fffd502c25028c0f9c2e
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
This commit is contained in:
Thiago Macieira 2025-02-12 21:55:38 -08:00
parent 00f3ca950a
commit 750be8ec51

View File

@ -574,7 +574,7 @@ public:
char *bufferPtr()
{
Q_ASSERT(buffer.isDetached());
return const_cast<char *>(buffer.constData()) + bufferStart;
return const_cast<char *>(buffer.constBegin()) + bufferStart;
}
void preread()
@ -671,7 +671,7 @@ static void *qt_cbor_decoder_read(void *token, void *userptr, size_t offset, siz
// we must have pre-read the data
Q_ASSERT(len + offset <= size_t(self->buffer.size() - self->bufferStart));
return memcpy(userptr, self->buffer.constData() + self->bufferStart + offset, len);
return memcpy(userptr, self->buffer.constBegin() + self->bufferStart + offset, len);
}
static CborError qt_cbor_decoder_transfer_string(void *token, const void **userptr, size_t offset, size_t len)
@ -840,7 +840,7 @@ QIODevice *QCborStreamReader::device() const
*/
void QCborStreamReader::addData(const QByteArray &data)
{
addData(data.constData(), data.size());
addData(data.constBegin(), data.size());
}
/*!
@ -1752,7 +1752,7 @@ QCborStreamReaderPrivate::readStringChunk_byte(ReadStringChunk params, qsizetype
return -1;
}
ptr = const_cast<char *>(params.array->constData()) + oldSize;
ptr = const_cast<char *>(params.array->constBegin()) + oldSize;
}
if (device) {
@ -1772,7 +1772,7 @@ QCborStreamReaderPrivate::readStringChunk_byte(ReadStringChunk params, qsizetype
}
} else {
actuallyRead = toRead;
memcpy(ptr, buffer.constData() + bufferStart, toRead);
memcpy(ptr, buffer.constBegin() + bufferStart, toRead);
}
return actuallyRead;
@ -1798,12 +1798,12 @@ QCborStreamReaderPrivate::readStringChunk_unicode(ReadStringChunk params, qsizet
return -1;
}
QChar *begin = const_cast<QChar *>(params.string->constData());
QChar *begin = const_cast<QChar *>(params.string->constBegin());
QChar *ptr = begin + currentSize;
QStringConverter::State cs(QStringConverter::Flag::Stateless);
if (device == nullptr) {
// Easy case: we can decode straight from the buffer we already have
ptr = QUtf8::convertToUnicode(ptr, { buffer.constData() + bufferStart, utf8len }, &cs);
ptr = QUtf8::convertToUnicode(ptr, { buffer.constBegin() + bufferStart, utf8len }, &cs);
} else {
// read in chunks, to avoid creating large, intermediate buffers
constexpr qsizetype StringChunkSize = 16384;