You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
the process: Many .cpp files are merged into one file before compilation
the theory: this improves compile time (less re-parsing of headers) and runtime (greater cross-module visibility, like LTO)
the downside (or is it an upside?): all ODR violations become compile time errors
The process in cmake is relatively easy:
One way: turn on CMAKE_UNITY_BUILD globally, but make sure any targets that are broken with Unity builds have it explicitly disabled.
Gotcha: "warnings as errors" is more important than ever. If one .cpp file defines a macro and a subsequent file redefines the macro, this is normally a warning, not an error, but you have no idea what the impact might be on the compiled code.
Conclusion: on this project we saw 0 perf differences, but we did get a 2x faster build!
Test your project regularly with Unity Builds to know if it has sneaky UB because of ODR violations and decide as a team if you want Unity Builds to be the default on your CI
Hi, I've juste seen this episode and played with that. One of the downside I felt upon is the undesired "merging" of unnamed namespace.
A practical use-case: I've got a bunch of unit-tests separated in different source files. In each source file, I declare a global std::mt19937 variable, within an unnamed namespace, in order to have a single random generator in the translation unit. I gave the same name for the generator in each TU.
But now the unity build decided to merge all my unit tests within a single TU and I've got now a redefinition issue...
Of course I can give a different name to my generator in each file but it's a bit of an overhead for the developer...
Same issue for static global constants and "local" functions...
I can't recall you mentioned that in the video (may be, in this case I apologize) and I felt it was worth mentioning.
Activity
lefticus commentedon Jan 8, 2024
The process in cmake is relatively easy:
One way: turn on
CMAKE_UNITY_BUILDglobally, but make sure any targets that are broken with Unity builds have it explicitly disabled.Gotcha: "warnings as errors" is more important than ever. If one .cpp file defines a macro and a subsequent file redefines the macro, this is normally a warning, not an error, but you have no idea what the impact might be on the compiled code.
Conclusion: on this project we saw 0 perf differences, but we did get a 2x faster build!
Original mold build (Release)
perf
original mold lto build
mold unity build
Note that there are fewer targets because the unity build merged targets together
perf run for unity mold build
File size:
mold unity lto build
Perf run for unity lto build, with mold
lefticus commentedon Jan 9, 2024
Best Practice:
lefticus commentedon Jan 13, 2024
Coming in Ep413
Dharmesh946 commentedon Sep 27, 2024
Hi, I've juste seen this episode and played with that. One of the downside I felt upon is the undesired "merging" of unnamed namespace.
A practical use-case: I've got a bunch of unit-tests separated in different source files. In each source file, I declare a global std::mt19937 variable, within an unnamed namespace, in order to have a single random generator in the translation unit. I gave the same name for the generator in each TU.
But now the unity build decided to merge all my unit tests within a single TU and I've got now a redefinition issue...
Of course I can give a different name to my generator in each file but it's a bit of an overhead for the developer...
Same issue for static global constants and "local" functions...
I can't recall you mentioned that in the video (may be, in this case I apologize) and I felt it was worth mentioning.