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

Milian Wolff milian.wolff at kdab.com
Fri Jul 10 14:04:07 CEST 2015


On Friday 10 July 2015 11:35:34 Smith Martin wrote:
> >1) you put pressure on the memory allocator, by asking it to allocate on
> >the heap each individual C object you put in the list. Every single
> >allocation has a cost, plus the overhead you need for bookkeeping, plus
> >the costs of getting your memory fragmented, etc.;
> 
> This pressure on the memory allocator, what is that? My memory doesn't get
> fragmented, because I only build lists; I never destroy them. But I do have
> to put each C somewhere, and if I use QVector<C>, doesn't it allocate a lot
> of them up front? I don't know how many i will need, but the number is
> usually 0, 1, or 2 -- the number of parameters in a function signature.

Calling malloc/free is not a cheap operation, when you profile most Qt 
applications, you'll find these functions nearly always in the top 10, often 
even the top 5, of functions where most time is spent.

> >2) you make the compiler produce more inefficient code by introducing
> >the layer of indirection;
> 
> But it will process the list one time only, from beginning to end.
>
> >3) you kill caching, as potentially every single access will result in a
> >cache miss (even in the common scenario of a simple forward iteration
> >over your list);
> 
> Why? All the entries in the list are created at the same time (well, during
> the parsing of the function signature)

Huh?! I really don't follow your reasoning to both of these points here. I 
suggest you go watch https://channel9.msdn.com/Events/Build/2013/4-329
 and similar talks out there to educate yourself? Whenever you access/traverse 
a list of pointers, you'll incur cache misses, except in the rare case where 
you are lucky and the pointers point to contiguous memory locations. And even 
then, you'd have an indirection that Peppe mentions.

Bye
-- 
Milian Wolff | milian.wolff at kdab.com | Software Engineer
KDAB (Deutschland) GmbH&Co KG, a KDAB Group company
Tel: +49-30-521325470
KDAB - The Qt Experts



More information about the Development mailing list