Since starting at a new company I've noticed that they use unity cpp files for the majority of our solution, and I was wondering if anyone is able to give me a definitive reason as to why and how these speed up the build process? I would've thought that editing one cpp file in the unity files will force recompilation of all of them.
-
5Bear in mind that a unity build changes the semantics, as there's lots of things that depend on the limits of a translation unit. – David Thornley May 14 '09 at 13:41
3 Answers
Very similar question and good answers here: #include all .cpp files into a single compilation unit?
The summary seems to be that less I/O overhead is the major benefit.
See also The Magic Of Unity Builds as linked in the above question as well.
-
excellent answer, thanks a lot for your help, after reading those links im still none the wiser if editing files within the unit cpp force a complete recompilation of the entire source, any ideas about this? – Stowelly May 11 '09 at 13:17
-
Yes, it would. Whether that's a bad thing or not depends on how often you have to do a full rebuild already. – Head Geek May 11 '09 at 13:26
-
2If one file changes, any decent build system would notice a changed dependency and recompile the lot. You may be able to mitigate some of that time with a compiler cache like the excellent ccache -- see http://ccache.samba.org/ – akent May 11 '09 at 13:30
-
1While day to day dev is definintely the big winner, Unity Builds make for rapid release builds compared to the old style builds. For any full rebuild, you can't beat a unity build. If you have a large codebase, you can even break up in the single unity file into a few smaller ones. Couple it with something like Incredibuild and you'll hardly have to wait for a build ever again! – OJ. May 18 '09 at 22:14
-
1Another trick with c++11 is that you can use [explicit template instantiations](http://en.cppreference.com/w/cpp/language/class_template) and 'extern template instantiations' to reduce compilation times significantly. The extern promises the compiler that the instantiations are happening in another translation unit. That means the compiler will not instantiate the specified class in the current translation unit. – Mani Zandifar Oct 22 '15 at 05:36
-
1Given that you have enough RAM the savings are not because of disk I/O. The file cache will keep all the third party headers in memory. The redundant parsing/code generation and subsequent duplicate removal in the linker is what makes C++ builds slow. – Trass3r Feb 20 '17 at 13:12
Lee Winder posted his experiences with the Unity Builds - The Evils of Unity Builds
His conclusion is:
Unity builds. I don’t like them.
-
Very usefull article. I have been noticing recently that making several changes to files accross multiple projects within the solution can take up to half an hour to build (even with incredibuild) but cleaning solution and building from scratch takes about 5 mins, so is definitly a lot of valid points there! – Stowelly Sep 26 '11 at 10:58
-
1The link can now be found [here](http://engineering-game-dev.com/2009/12/15/the-evils-of-unity-builds/). – geometrian Dec 20 '14 at 03:11
-
-
3@glauxosdever The link can now be found [here](https://engineering.leewinder.co.uk/the-evils-of-unity-builds-2dbb07c3cae7#.8jn4cupf9), and archived [here](http://web.archive.org/web/20140719090927/http://www.altdev.co/2011/08/14/the-evils-of-unity-builds/) in-case it goes down again. – geometrian Nov 15 '16 at 18:11
-
1Now it's [here](https://engineering.leewinder.co.uk/2009/12/15/the-evils-of-unity-builds/) – wreckgar23 Nov 24 '17 at 14:01
It's because it saves redundant work. Redundant parsing and compilation for dependencies. Linking is also much more complex -- you either have your exports all in one object (or a few), or it's separate redundant exports across most of the target's object files. Fewer objects result in less I/O and reduced link times. Depending on your setup, inclusion could be a problem -- on the "unity build" system I use, the build is ultimately CPU and/or memory bound.

- 104,054
- 14
- 179
- 226