QUuid: de-pessimize QDataStream operator
Use a stack buffer instead of a QByteArray to hold the 16 bytes for the QUuid serialisation, replacing toRfc4122() with toBytes() and a memcpy(). As drive-bys, drop the needless cast from char* to uchar* (qToLittleEndian() has void* arguments, so char* is fine) and drop {} around single-line if body. Change-Id: I6ffabcf07fc9a730a782e20e113999a0dcf15067 Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Ivan Solovev <ivan.solovev@qt.io> (cherry picked from commit 72d51f1c42b83c26d15cb626f1b22905fbbea474) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
parent
c9cb83d212
commit
3d3837287a
@ -745,13 +745,15 @@ QByteArray QUuid::toRfc4122() const
|
||||
*/
|
||||
QDataStream &operator<<(QDataStream &s, const QUuid &id)
|
||||
{
|
||||
QByteArray bytes;
|
||||
constexpr int NumBytes = sizeof(QUuid);
|
||||
static_assert(NumBytes == 16, "Change the serialization format when this ever hits");
|
||||
char bytes[NumBytes];
|
||||
if (s.byteOrder() == QDataStream::BigEndian) {
|
||||
bytes = id.toRfc4122();
|
||||
const auto id128 = id.toBytes();
|
||||
static_assert(sizeof(id128) == NumBytes);
|
||||
memcpy(bytes, &id128, NumBytes);
|
||||
} else {
|
||||
// we know how many bytes a UUID has, I hope :)
|
||||
bytes = QByteArray(16, Qt::Uninitialized);
|
||||
uchar *data = reinterpret_cast<uchar *>(bytes.data());
|
||||
auto *data = bytes;
|
||||
|
||||
// for historical reasons, our little-endian serialization format
|
||||
// stores each of the UUID fields in little endian, instead of storing
|
||||
@ -769,9 +771,9 @@ QDataStream &operator<<(QDataStream &s, const QUuid &id)
|
||||
}
|
||||
}
|
||||
|
||||
if (s.writeRawData(bytes.data(), 16) != 16) {
|
||||
if (s.writeRawData(bytes, NumBytes) != NumBytes)
|
||||
s.setStatus(QDataStream::WriteFailed);
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user