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

Milian Wolff milian.wolff at kdab.com
Mon Jul 13 14:42:38 CEST 2015


On Monday 13 July 2015 13:26:03 Ulf Hermann wrote:
> Mind that all this talk about QList being bad does *not* hold for
> primitively movable types of exactly sizeof(void *). For example
> QByteArray, which only consists of a single d-pointer. For those the
> disadvantages of QList against QVector don't manifest themselves, but the
> generated code is smaller. Consider the following simple program:
> 
> int main(int, char **)
> {
>      CONTAINER<QByteArray> horst;
>      horst << "baum" << "baum";
>      qDebug() << horst;
>      return 0;
> }
> 
> Replace CONTAINER with QVector, and I get a binary of 16kb, on my 64bit
> linux machine. Replace it with QList and I get 12kb:
> 
> 
> ulf at zebra:~/dev$ ls -l build-untitled97-5_6-*
> build-untitled97-5_6-Release:
> total 68
> -rw-r--r-- 1 ulf ulf 14568 Jul 13 13:17 main.o
> -rw-r--r-- 1 ulf ulf 35953 Jul 13 13:17 Makefile
> -rwxr-xr-x 1 ulf ulf 16016 Jul 13 13:17 untitled97
> 
> 
> ulf at zebra:~/dev$ ls -l build-untitled97-5_6-*
> build-untitled97-5_6-Release:
> total 60
> -rw-r--r-- 1 ulf ulf  8824 Jul 13 13:18 main.o
> -rw-r--r-- 1 ulf ulf 35953 Jul 13 13:17 Makefile
> -rwxr-xr-x 1 ulf ulf 12264 Jul 13 13:18 untitled97

I can reproduce this. But apparently a lot of the bloat comes from qDebug, b/c 
if I rewrite the code to be compatible to std::vector, I get these numbers:

-rwxr-xr-x  1 milian users  7784 Jul 13 14:39 vector
-rwxr-xr-x  1 milian users  9856 Jul 13 14:39 qvector
-rwxr-xr-x  1 milian users  8464 Jul 13 14:39 qlist

The code I use is:
#include <QByteArray>
#include <QVector>
#include <QList>
#include <QDebug>
#include <vector>

using namespace std;

int main()
{
    CONTAINER<QByteArray> list;
    list.push_back("baum");
    list.push_back("baum");
    foreach (const auto& item, list) {
        qDebug() << item;
    }
    return 0;
}

And I compile with

g++ -Os -std=c++11 -I/usr/include/qt -I/usr/include/qt/QtCore -lQt5Core 
test.cpp -fPIC -DCONTAINER=vector -o vector
g++ -Os -std=c++11 -I/usr/include/qt -I/usr/include/qt/QtCore -lQt5Core 
test.cpp -fPIC -DCONTAINER=QList -o qlist
g++ -Os -std=c++11 -I/usr/include/qt -I/usr/include/qt/QtCore -lQt5Core 
test.cpp -fPIC -DCONTAINER=QVector -o qvector

and `strip` the binaries afterwards. So it's still ~2KB worse off - any chance 
for using `extern templates` or similar to reduce this code bloat?

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