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 <Friedemann.Kleint@theqtcompany.com> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>bb10
parent
b196573c9a
commit
160f4191d4
|
|
@ -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<quint32> 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;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue