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

Thiago Macieira thiago.macieira at intel.com
Mon Jul 27 22:13:26 CEST 2015


On Monday 27 July 2015 21:44:32 Kevin Kofler wrote:
> > For "bad" types for QList, that O(1) is hiding two indirections. Only
> > one with QVector. That's a huge cost you're not talking about.
> 
> It's a factor 2, vs. a factor sizeof(T)/sizeof(T*) in the opposite
> direction  for the middle insertions/removals. For any nontrivial class,
> sizeof(T)/sizeof(T*) can be a lot larger than 2.

You're comparing apples and oranges here.

The total memory allocated for QList is always equal to or bigger than QVector 
for the same size, regardless of T. So in addition to having the same memory 
size, you access the data through one extra indirection.

The only benefit QList has is that it's really quick to resize and insert at 
any point compared to a QVector of a complex or large type.


QList<T> and QVector<T> allocate the same amount of memory if sizeof(T) == 
sizeof(T*) and T is movable.

QVector<T> allocates less memory if:
 - sizeof(T) < sizeof(T*) and T is movable
 - sizeof(T) >= sizeof(T*)
 - T is not movable

In the first case, QVector allocates n * sizeof(T) and QList allocates 
n * sizeof(T*).

In the latter two cases, , then QList allocates 
	n * (sizeof(T) + sizeof(T*))
while QVector allocates
	n * sizeof(T)

And this is not including the memory overhead of malloc, which is 16 bytes on 
64-bit systems. QVector does exactly one malloc, whereas QList for the non-
movable case will do n + 1, which increases the memory consumption by 16 * n 
bytes.

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center




More information about the Development mailing list