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

Smith Martin Martin.Smith at theqtcompany.com
Fri Jul 10 13:35:34 CEST 2015


>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.

>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)

martin
________________________________________
From: development-bounces+martin.smith=theqtcompany.com at qt-project.org <development-bounces+martin.smith=theqtcompany.com at qt-project.org> on behalf of Giuseppe D'Angelo <giuseppe.dangelo at kdab.com>
Sent: Friday, July 10, 2015 1:26 PM
To: development at qt-project.org
Subject: Re: [Development] HEADS UP: Don't use QList, use Q_DECLARE_TYPEINFO

Il 10/07/2015 13:21, Smith Martin ha scritto:
> But I'm still not clear on one point. If I use QList<C>, and it is implemented as QList<C*> because my C is not a "good" C, why is this inefficent if the only copy of each C is the one that gets allocated on the heap? I just create the list to store things. It won't ever be moved. I will just process the list when it's time comes.
>
> Why is that inefficient?

Because

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.;

2) you make the compiler produce more inefficient code by introducing
the layer of indirection;

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);

and so on.

Cheers,

--
Giuseppe D'Angelo | giuseppe.dangelo at kdab.com | Software Engineer
KDAB (UK) Ltd., a KDAB Group company | Tel: UK +44-1625-809908
KDAB - The Qt Experts




More information about the Development mailing list