The benefits / disadvantages of unity builds? [duplicate]
Asked Answered
W

3

32

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.

Wincer answered 11/5, 2009 at 12:54 Comment(1)
Bear in mind that a unity build changes the semantics, as there's lots of things that depend on the limits of a translation unit.Trimmer
L
33

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.

Laveta answered 11/5, 2009 at 13:0 Comment(6)
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?Wincer
Yes, it would. Whether that's a bad thing or not depends on how often you have to do a full rebuild already.Goodygoody
If 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 ccache.samba.orgLaveta
While 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!Mezzorelievo
Another trick with c++11 is that you can use explicit template instantiations 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.Pusillanimity
Given 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.Communion
N
15

Lee Winder posted his experiences with the Unity Builds - The Evils of Unity Builds

His conclusion is:

Unity builds. I don’t like them.

Needlepoint answered 23/9, 2011 at 12:12 Comment(5)
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!Wincer
The link can now be found here.Inelegant
@imallett The link is broken too.Verlaverlee
@Verlaverlee The link can now be found here, and archived here in-case it goes down again.Inelegant
Now it's hereAmigo
H
2

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.

Habitat answered 6/2, 2012 at 0:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.