Speeding up C++ builds with Unity Builds and reduced header dependencies

1k views Asked by At

I just converted an Objective-C(++) project to plain C++. While moving more and more code over, I noticed the build time increase quite a lot.

My project is currently split up into several frameworks/dylibs and a main project which uses these frameworks.

I did some research and found that there are basically three things recommended to reduce the build time:

  • reducing header dependencies
  • using unity builds
  • using a tool like ccache to not redo unneeded work all the time

I implemented ccache and it works great and I was able to decrease the build time quite a bit.

I'm a bit unsure though about reducing the header dependencies and the unity builds. I read that a big downside of the unity builds is that you need to recompile everything if you make changes in one source file which makes sense. That however would not be a problem for the frameworks as they will need to be recompiled anyways if they change.

I read that it's bad practice to use "umbrella headers" such as "MyFramework.h" which will include all the public headers of a given framework although you may only need a few of them.

Cocoa uses umbrella headers everywhere and it's of course much easier than to pick the exact headers needed for each source file. However, when using unity builds I will only have one header per framework, correct?

Does it still make sense to pick the individual headers or will using "umbrella headers" be ok with unity builds?

Tapping a bit in the dark here and don't want to spend time implementing a technique which doesn't help in the end.

Thanks for your help!

1

There are 1 answers

11
Öö Tiib On

It feels like question for opinionated answers. Mine are such:

Always reduce header dependencies. Reduced dependencies make overall architecture cleaner. Independent little individual modules with clear responsibilities loosely coupled are always better to work with than spaghetti.

Use precompiled headers for compiling rarely changing headers. The third party, library and framework umbrella headers change rarely and so need to be rarely parsed and recompiled too.

Work most of the time with separate units (few cpp files) and unit tests for those. Otherwise you build whole program, then navigate in it to situation of interest then step with debugger there and so on. May be you like it but I'm too lazy, it is boring repetitive and wastes my time. Only linking of whole C++ program (worth anything) takes usually ten minutes or more and I don't need so many coffee-breaks.

Do not use unity builds, better use continuous integration that when you push automatically builds and runs all the unit tests of whole program and prepares binaries on other computer (or farm). You will be notified when it is done (or did fail) and then you can take the binaries and debug whole program too if you want to.