[Interest] Compile Speed Question regarding MOC

andre apoenitz at t-online.de
Wed Feb 19 23:03:02 CET 2014

On Wed, Feb 19, 2014 at 09:21:01AM +0100, Till Oliver Knoll wrote:
> Am 19.02.2014 um 05:10 schrieb Constantin Makshin <cmakshin at gmail.com>:
> > Look at the problem from the other side:
> > 1) separate "moc_*.cpp" has to be compiled only during full [re]builds
> > or when the corresponding class' header file is changed;
> > 2) when "moc_*.cpp" is #include-d into the implementation .cpp file,
> > they are both compiled at every change.
> With regards to 1): A ful rebuild takes the time it takes, no matter what.
> You can speed that up things as others have suggested
> #include the mocced files, AUTOMOC), but that's that.

That's not the full truth. Each compilation unit comes with an overhead of
included system headers. A while ago that easily was 50k+ LOC for a
"standard" C++ translation unit, and less than 20k LOC for a typical "pure
Qt" TU. Luckily, the std side improved a bit, on the other hand some clever
soul decided to pull half of std:: into qmetatype.h on Mar 28, 2013,
effectively killing any competetive advantage "pure Qt" had in this area.

At the end of the day, the lines pulled in from the "system" (Qt or not)
still will outweigh your own code's contribution to a typical TU by a
magnitude, so total compilation time is in first approximation roughly
proportional to the number of TUs.

> [...]
> IMHO what is more important is to speed-up incremental builds 2), as
> those times are most annoying for every developer.
> So here are my general tips to avoid /dependencies/:
> In headers, #include only what is really necessary!

> - Use forward declarations as much as possible


> - Use the "private *d" idiom

True at least for "public" headers. If the header isn't used widely,
this has to be weighed.

>  (there is a name for it which I don't recall at the moment)

> - Does it /need/ to be an "is-a" relation?
> Or wouldn't a "has-a" relation not fit the problem better anyway?
> This is one of the "classic" design questions ;)

True. "Has a" typically decouples better than "is a". So, when in
doubt there should be no doubt what to use...
> In my general experience "people just inherit too much" (and hence
> introduce dependencies on the base class, instead of hiding that
> dependency with a "has-a" relationship, e.g. implemented with the
> d-pointer pattern above).



More information about the Interest mailing list