Memory allocation overhead (Re: KFileItem)

David Faure faure at kde.org
Sun Sep 1 08:46:45 UTC 2013


On Friday 23 August 2013 12:57:15 Frank Reininghaus wrote:
> http://code.woboq.org/userspace/glibc/malloc/malloc.c.html
> 
> A comment in that file [1] already says that the minimum allocated
> size on a system with 8-byte pointers is 24/32 bytes (see below for an
> explanation why it's not one fixed value).

Hi Frank,

Thanks for the detailed explanation!

I'm stunned. It means every class (in Qt and elsewhere) with a d pointer, uses 
32 bytes for the d pointer, rather than the expected 8 bytes.

Maybe in some cases (small private class + class instanciated many times) we 
would save quite some memory by moving some stuff as private members, like

Private *d; // not used for now, always null
int m_value;
int m_min;
int m_max;
int m_step;

=> one less allocation, 32 bytes saved per instance.

I wonder if all malloc implementations commonly used (i.e. including BSD, Mac 
and Windows) behave this way, though (but if they don't, the above doesn't 
lose anything anyway).

In the case of KFileItem, this obviously can't be done (huge private class).
But making it a Q_MOVABLE_TYPE sounds right (since it is movable), we should 
just fix the code that relies on item pointers being stable across changes to 
the QList (e.g. by using QLinkedList).

-- 
David Faure, faure at kde.org, http://www.davidfaure.fr
Working on KDE, in particular KDE Frameworks 5



More information about the Kde-frameworks-devel mailing list