[Development] [SPAM] How bad QList really is

Giuseppe D'Angelo giuseppe.dangelo at kdab.com
Wed Apr 29 01:41:46 CEST 2020


Il 28/04/20 21:45, Matthew Woehlke ha scritto:
>> * QList gets adapted so that its internal array allocates 3 *
>> sizeof(void*) per element, so that e.g. Q6StringList won't require a
>> per-item allocation. This would then also avoid allocations for other
>> datatypes, e.g. QStringView, QImage, maybe QVariant and QColor; but of
>> course waste a ton of space for the ones which remain small (most of Qt
>> implicitly shared datatypes).
> Uh... can't it allocate sizeof(T) if T meets some criteria? IOW, I don't
> see the second case penalizing smaller types unless the implementation
> is poorly done.

This way of working is the *key* of QList design. QList is always a 
vector of void* (so it's nice for Qt pimpled types). This allows to type 
erase the entire management of the vector (it's always a vector of 
void*), reducing the amount of template code that needs to be instantiated.

The only type specific operations are setting/retrieving/deleting a 
value in that vector, thus giving us the rule:

- if the type is small and relocatable, just put it in the vector
- otherwise, heap allocate and put the pointer in the vector

There are no exceptions (which is why e.g. Q5List<int> on 32 bits is 
wasteful), again by design. And, because noone is volunteering to do the 
work while QVector is already there...

My 2 c,
-- 
Giuseppe D'Angelo | giuseppe.dangelo at kdab.com | Senior Software Engineer
KDAB (France) S.A.S., a KDAB Group company
Tel. France +33 (0)4 90 84 08 53, http://www.kdab.com
KDAB - The Qt, C++ and OpenGL Experts

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4329 bytes
Desc: Firma crittografica S/MIME
URL: <http://lists.qt-project.org/pipermail/development/attachments/20200429/80c11eb1/attachment.bin>


More information about the Development mailing list