D7110: Reduce heap allocs by using QVector instead of QList

Denis Kurz noreply at phabricator.kde.org
Fri Aug 4 11:44:45 BST 2017


dkurz added a comment.


  It's the size of the container itself (and its heap allocations) that we reduce. The old code constructs an empty array list, and then calls operator<< n times. For every such call, QList allocates a new array on the heap, one element larger in size than the last one, and then moves the old contents to the new location. That's what I figured out by looking at QList's source code (QList::operator(t)<< calls QList::append(t) calls QListData::append() calls QListData::append(1), which does exactly what I just described) , but maybe I missed some mechanism that batch allocates more space than needed, just as QVector does. But even if it did, QList cannot know at the beginning of the << sequence how much space it would need eventually. So even with vectors' trick to allocate some constant fraction of the previously used amount of memory when size hits capacity, we would have log(n) memory allocations for n insertions. Using the ctor directly reduces this to only one heap allocation.
  
  Using the initializer list ctor of QList might be just as efficient in this case. Frankly, I'm not an expert on Qt containers. I just watched Milians CppCon15 talk on Qt yesterday, where he said that QList should probably die in favor of QVector, and only be used where it has to be passed to some function that only excepts QList, so I switched.

REPOSITORY
  R206 KMail

REVISION DETAIL
  https://phabricator.kde.org/D7110

To: dkurz, #kde_pim_kmail
Cc: mlaurent, #kde_pim, dvasin, winterz, vkrause, knauss, dvratil
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.kde.org/pipermail/kde-pim/attachments/20170804/cb92a34c/attachment.html>


More information about the kde-pim mailing list