Page MenuHome

Geometry Nodes: Support unity builds for the bf_nodes_geometry module.
ClosedPublic

Authored by Jacques Lucke (JacquesLucke) on Nov 23 2021, 6:20 PM.

Details

Summary

Using unity builds reduces compile times significantly. In my tests it's usually 2-4x faster.
When using unity builds, cmake generates new .cpp like the one below and compiles those instead of our .cc files.

Our source code is already prepared to support this without introducing name collisions (rB1df8abff257030ba79bc23dc321f35494f4d91c5).

Unity builds improve build times in multiple ways (in order of importance):

  • Headers are parsed less often, because they only have to be parsed once for each set of files.
  • Fewer duplicated template instantiations. Many nodes perform the same template instantiations (e.g. Vector<int>::append). By compiling multiple files at once, each symbol is only instantiated once per set of files.
  • Faster link time, due to fewer duplicated symbols that the linker has to deal with.

If this goes well, we could consider using unity builds in other parts of Blender as well.

Example file generated by cmake (just removed empty lines):

/* generated by CMake */
#include "/home/jacques/blender-git/blender/source/blender/nodes/geometry/nodes/node_geo_curve_fillet.cc"
#include "/home/jacques/blender-git/blender/source/blender/nodes/geometry/nodes/node_geo_curve_handle_type_selection.cc"
#include "/home/jacques/blender-git/blender/source/blender/nodes/geometry/nodes/node_geo_curve_length.cc"
#include "/home/jacques/blender-git/blender/source/blender/nodes/geometry/nodes/node_geo_curve_parameter.cc"
#include "/home/jacques/blender-git/blender/source/blender/nodes/geometry/nodes/node_geo_curve_primitive_bezier_segment.cc"
#include "/home/jacques/blender-git/blender/source/blender/nodes/geometry/nodes/node_geo_curve_primitive_circle.cc"
#include "/home/jacques/blender-git/blender/source/blender/nodes/geometry/nodes/node_geo_curve_primitive_line.cc"
#include "/home/jacques/blender-git/blender/source/blender/nodes/geometry/nodes/node_geo_curve_primitive_quadratic_bezier.cc"
#include "/home/jacques/blender-git/blender/source/blender/nodes/geometry/nodes/node_geo_curve_primitive_quadrilateral.cc"
#include "/home/jacques/blender-git/blender/source/blender/nodes/geometry/nodes/node_geo_curve_primitive_spiral.cc"

Diff Detail

Repository
rB Blender
Branch
unity-build (branched from master)
Build Status
Buildable 18921
Build 18921: arc lint + arc unit

Event Timeline

Jacques Lucke (JacquesLucke) requested review of this revision.Nov 23 2021, 6:20 PM
Jacques Lucke (JacquesLucke) created this revision.
Ray Molenkamp (LazyDodo) requested changes to this revision.Nov 23 2021, 6:30 PM
Ray Molenkamp (LazyDodo) added inline comments.
CMakeLists.txt
190

I'd move the cmake version check over here, and just hardcode WITH_UNITY_BUILD to OFF in the else() branch, so when we later on have more modules that support it we don't have to sprinkle version checks all over the place.

This revision now requires changes to proceed.Nov 23 2021, 6:30 PM
Jacques Lucke (JacquesLucke) retitled this revision from Geometry Nodes: Support unity builds. to Geometry Nodes: Support unity builds for the bf_nodes_geometry module..Nov 23 2021, 6:32 PM
Jacques Lucke (JacquesLucke) edited the summary of this revision. (Show Details)

Yes! I'd accept based on that change, but having some build issues on windows. looking into it

  • Merge branch 'master' into unity-build

Fixed the MSVC issue in master, the speed difference makes this a no-brainer

for building bf_nodes_geometry with MSVC 2019

WITH_UNITY_BUILDTime
Off145s
On55s

It's good to note the individual files are still visible in the Visual Studio IDE , and can be edited as normal even compiling the files individually (ie for a syntax check, or wanting to see the preprocessor output) still works, may be worth checking XCode's behavior here before landing.

This revision is now accepted and ready to land.Nov 23 2021, 7:14 PM

Do you know who is using XCode and could check this?

Ankit Meel (ankitm) added a comment.EditedNov 23 2021, 9:38 PM

For bf_nodes_geometry build time debug, full

unity optionseconds
Off277
ON97

Compiling individual file: 'node_geo_curve_primitive_quadratic_bezier.cc' is not a member of any targets in the current scheme that build for running is shown even if current scheme is ALL_BUILD or INSTALL or bf_nodes_geometry. Workaround is finding the file that includes it, and compiling that file.

Looks after enabling Unity:


Do people compile individual files like that?

I'm not used to the look of Blender's source code in an IDE, is this looking good or bad or good enough? Can I merge the patch?

This revision now requires review to proceed.Nov 24 2021, 10:20 AM

Someone with an above average machine would just go for target compilation to see compile errors instead of a file, now that there's a faster way (unity). I mentioned it since Ray did.
As for the compilation failure, will file a bug report and see what CMake folks have to say.

The code navigator didn't remove the original files, so it's good enough. Syntax highlight, autocomplete works.

This revision is now accepted and ready to land.Nov 24 2021, 11:05 AM