Ok I think I need information.
Obviously, redundancy is not supported outside of all methods. Especially clear() and operator=() seem to cancel the reservation. In the case of operator=() it will actually be impossible to maintain the reservation due to the implicit data sharing that operator=(QByteArray) uses.
It also means that the QByteArray backup engine has been created for another use case. An attempt to make a reservation is maintained throughout the life of the QByteArray.
In my use case, it seems to use a workaround using truncate(0) instead of clear() or operator=() :
QByteArray buffer; buffer.reserve(1000); buffer.append("foo"); qDebug() << "buffer" << buffer.capacity() << buffer; buffer.truncate(0); buffer.append("bar"); qDebug() << "buffer" << buffer.capacity() << buffer;
Fingerprints:
buffer 1000 "foo" buffer 1000 "bar"
(Thanks to Alejandro)
However, a more robust approach would be to make a reserve() call before each data collection / upload sequence. This does not reduce the redistribution to one in a lifetime QByteArray, but at least it uses exactly one redistribution per data sequence, where otherwise many redistributions would be required. I think this is an acceptable solution.
In any case, before using reserve() in the Qt container, you need to test the behavior in detail, because otherwise it may happen that the container behaves much differently than expected. This is also important because these basic implementation details are not documented and are subject to change without further notice in future versions of Qt.
source share