From 3d3837287a3c7f810bef4e73fc447d7b91692ada Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 6 Sep 2023 14:53:15 +0200 Subject: [PATCH] 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 Reviewed-by: Thiago Macieira Reviewed-by: Ivan Solovev (cherry picked from commit 72d51f1c42b83c26d15cb626f1b22905fbbea474) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/plugin/quuid.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/corelib/plugin/quuid.cpp b/src/corelib/plugin/quuid.cpp index 1e263a44e8f..63aef7fc676 100644 --- a/src/corelib/plugin/quuid.cpp +++ b/src/corelib/plugin/quuid.cpp @@ -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(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; }