Migration to C++ (?)

Christian Dietrich stettberger at dokucode.de
Tue Jul 22 09:12:47 CEST 2014


Zsolt Udvari <udvzsolt at gmail.com> writes:

> 2014-07-22 4:36 GMT+02:00 Christopher Meng <cickumqt at gmail.com>:
>> Any plan of C++ on Boost?

> Is it serious? Herbstluftwm is a small, lightweight window manager, I
> think, depends on huge boost is a very-very bad idea...

Ok, so let's check the facts here. Boost is a C++ Template
Library. Boost itself is a large project that is right, but is it a
inseperable blob? Or to give the question in other words: Do we always
pay the full price of using boost, when only using a small subset of all
features. Because that would be bloat.

Ok, first of all, there are nearly no shared libraries in boost:

-rw-r--r-- 1 root root 128K Jun  4  2013 /usr/lib/libboost_filesystem.so.1.49.0
-rw-r--r-- 1 root root 1.1M Jun  4  2013 /usr/lib/libboost_regex.so.1.49.0
-rw-r--r-- 1 root root  15K Jun  4  2013 /usr/lib/libboost_system.so.1.49.0
-rw-r--r-- 1 root root 107K Jun  4  2013 /usr/lib/libboost_thread.so.1.49.0

And you "infect" your software only with with those libraries, when you
use the corresponding boost feature. So, here a module separation is in
place. But is this bad? herbstluftwm already links again various
libraries.

$ ldd =herbstluftwm | cut -d " " -f 3 | xargs ls -lhL
-rwxr-xr-x 1 root root 1.8M Apr 12 12:38 /lib/x86_64-linux-gnu/libc.so.6
-rw-r--r-- 1 root root  15K Apr 12 12:38 /lib/x86_64-linux-gnu/libdl.so.2
-rw-r--r-- 1 root root 1.1M Apr  3 16:05 /lib/x86_64-linux-gnu/libglib-2.0.so.0
-rw-r--r-- 1 root root 247K Dec  3  2013 /lib/x86_64-linux-gnu/libpcre.so.3
-rwxr-xr-x 1 root root 139K Apr 12 12:38 /lib/x86_64-linux-gnu/libpthread.so.0
-rw-r--r-- 1 root root 1.3M Dec  4  2013 /usr/lib/x86_64-linux-gnu/libX11.so.6
-rw-r--r-- 1 root root  15K Jun 15  2013 /usr/lib/x86_64-linux-gnu/libXau.so.6
-rw-r--r-- 1 root root  23K Apr 30  2012 /usr/lib/x86_64-linux-gnu/libXdmcp.so.6
-rw-r--r-- 1 root root  72K Jun 30  2013 /usr/lib/x86_64-linux-gnu/libXext.so.6
-rw-r--r-- 1 root root  11K Jul  1  2013 /usr/lib/x86_64-linux-gnu/libXinerama.so.1
-rw-r--r-- 1 root root 123K Jan 15  2014 /usr/lib/x86_64-linux-gnu/libxcb.so.1

So, how much bloat are the template instantiations? Let's do a little
experiment, just to get an impression and not to give full evidence. How
is it with the boost::bind library?

////////////////////////////////////////////////////////////////
L qy03fugy at faui49r /tmp % cat test.cc
#include <iostream>
#ifdef USE_BOOST
#include "boost/bind.hpp"
#endif

void nine_arguments(
                    int i1,int i2,int i3,int i4,
                    int i5,int i6,int i7,int i8, int i9) {
    std::cout << i1 << i2 << i3 << i4 << i5
              << i6 << i7 << i8 << i9 << '\n';
}

void(*funcptr)(int, int, int, int, int,
               int, int, int, int) = nine_arguments;

int main(int argc, int *argv[]) {
    int i1=1,i2=2,i3=3,i4=4,i5=5,i6=6,i7=7,i8=8,i9=9;

#ifdef USE_BOOST
    (boost::bind(funcptr,_9,_2,_1,_6,_3,_8,_4,_5,_7))
        (i1,i2,i3,i4,i5,i6,i7,i8,i9);
#else
    funcptr(i9, i2, i1, i6, i3, i8, i4, i5, i7);
#endif
    return 0;
}
L qy03fugy at faui49r /tmp % g++ test.cc -D USE_BOOST -o with-boost; g++ test.cc -D USE_BOOST -o with-boost
test.cc:16:5: warning: second argument of ‘int main(int, int**)’ should be ‘char **’ [-Wmain]
[...]
L qy03fugy at faui49r /tmp % ./with-boost; ./without-boost
921638457
921638457
L qy03fugy at faui49r /tmp % ls -lh with*
-rwxr-xr-x 1 qy03fugy immdstud  22K Jul 22 09:05 with-boost*
-rwxr-xr-x 1 qy03fugy immdstud 8.9K Jul 22 08:53 without-boost*


////////////////////////////////////////////////////////////////

So here we see a little bloat, but we used g++ without
optimizations. But what happens if we enable the least optimization
level -O1?

L qy03fugy at faui49r /tmp % g++ test.cc -D USE_BOOST -o with-boost -O1; g++ test.cc -D USE_BOOST -o with-boost -O1
test.cc:16:5: warning: second argument of ‘int main(int, int**)’ should be ‘char **’ [-Wmain]
[...]
L qy03fugy at faui49r /tmp % ./with-boost; ./without-boost
921638457
921638457
L qy03fugy at faui49r /tmp % ls -lh with*
-rwxr-xr-x 1 qy03fugy immdstud 8.9K Jul 22 09:05 with-boost*
-rwxr-xr-x 1 qy03fugy immdstud 8.9K Jul 22 08:53 without-boost*

////////////////////////////////////////////////////////////////

And suddenly all my macro "bloat" disappears. Because macros are often
used to hide boiler plate code in a nice API. When you want to know more
about C++ template programming I would recommend reading

   Modern C++ Design; Andrei Alexandescu

So, after I've shown that boost does not necessarily mean bloat, I want
to empasize that I do *not* recommend the usage of boost in all cases or
that it should be used extensively in herbstluftwm. BUT, if you make the
claim, that boost is out of hell all is big, large and whatever, please
check the facts first. Using Boost it not equal to Using Boost. That
boot inhibits always bloat is a myth.

chris



More information about the hlwm mailing list