From 160f4191d4de89c37d2f69b8023b3bfa457766cb Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 27 Aug 2014 00:04:11 +0200 Subject: Micro-optimize QKeySequence datastream operator (II) Instead of reading a QList with the keys and then assigning the list contents to the keysequence internals without any further checking (not even for the size of the list, even though op<< creates lists with just one element), do the processing by hand. The greatest benefit is that there is are no memory allocations anymore, except for the detach and whatever QDataStream does internally. The other benefit is that the output key sequence is not touched until the necessary values have been successfully read. This includes the detach. For this, some very basic error checking has been added. Also removed the magic number 4 in favor of the recently introduced QKeySequencePrivate::MaxKeyCount. Change-Id: If70f75cc043468d2774a7bb03eebdbf39422043a Reviewed-by: Friedemann Kleint Reviewed-by: Thiago Macieira --- src/gui/kernel/qkeysequence.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/gui/kernel/qkeysequence.cpp b/src/gui/kernel/qkeysequence.cpp index 9dc06138a6..275066ab03 100644 --- a/src/gui/kernel/qkeysequence.cpp +++ b/src/gui/kernel/qkeysequence.cpp @@ -1599,11 +1599,19 @@ QDataStream &operator<<(QDataStream &s, const QKeySequence &keysequence) */ QDataStream &operator>>(QDataStream &s, QKeySequence &keysequence) { + const quint32 MaxKeys = QKeySequencePrivate::MaxKeyCount; + quint32 c; + s >> c; + quint32 keys[MaxKeys] = {0}; + for (uint i = 0; i < qMin(c, MaxKeys); ++i) { + if (s.atEnd()) { + qWarning("Premature EOF while reading QKeySequence"); + return s; + } + s >> keys[i]; + } qAtomicDetach(keysequence.d); - QList list; - s >> list; - for (int i = 0; i < 4; ++i) - keysequence.d->key[i] = list.value(i); + std::copy(keys, keys + MaxKeys, keysequence.d->key); return s; } -- cgit v1.2.3