[Development] HEADS UP: Don't use QList, use Q_DECLARE_TYPEINFO

Kevin Kofler kevin.kofler at chello.at
Tue Jul 21 02:26:41 CEST 2015


Randall O'Reilly wrote:
> I’m surprised nobody has mentioned that, because of the private data
> nature of most Qt classes, they are a) already allocating the private data
> object on the heap all the time and incurring all that overhead and memory
> allocation badness, and b) are essentially a pointer to an object already
> — thus QList adds another redundant pointer on top of that, and hence
> QVector functions much like a QList might have worked for objects with
> significant amounts of their own member data.  Seems like a compelling
> argument in favor of QVector to me..

For the implicitly shared data types, QList actually does NOT add another 
layer of indirection (as documented: "If T is itself a pointer type or a 
basic type that is no larger than a pointer, or if T is one of Qt's shared 
classes, then QList<T> stores the items directly in the pointer array."), so 
in that case, it is almost the same as QVector, except that it allows 
prepending items much more efficiently (O(1) in many cases, whereas it's 
always O(n) in QVector).

And if your data is NOT of such an implicitly shared class, using QVector 
will make any insertions or deletions REALLY expensive for any non-trivial 
classes as the vectors grow large. (You are at the very least memmove-ing a 
lot of data rather than only small pointers, or worse, actually calling 
thousands of copy constructors and destructors.)

        Kevin Kofler




More information about the Development mailing list