Is compilation multi-threaded?

Is compiling c# scripts multi-threaded? Will purchasing a computer with more CPUs speed up compilation?
How about builds?

1 Like

Unity itself isnā€™t multi-threaded and may of the gameā€™s API calls can only be called from the main thread. You can multithread in Unity, but itā€™s not for the faint of heart, and very difficult to debug. Newer Unity technologies aim to alleviate this (ECS, Burst Compiler, Computer Shaders), but ā€˜classicā€™ multi-threading in the C# way isnā€™t (yet) fully supported in Unity.

1 Like

Iā€™m not sure if compilation is multi-threaded, although my guess is it probably is. However, if you currently havenā€™t got an SSD, then getting a PC with one will help massively with compilation and build times. When I recently upgraded my PC with an SSD I saw a 2-3 fold reduction in compilation/build times.

I did a bit of research and it looks like C# compilers are single threaded.

1 Like

Make sure you get a PC with an SSD then, because thatā€™s where youā€™ll gain the most benefit.

1 Like

The compilers, yes. But some system can run multiple compile threads over multiple source files (XCode does this), so you can have multiple compilation threads running at the same time. Compilation time of a single source file will not improve, but overall compilation of the project will. And itā€˜s a sight to behold when 12 cores let it rip :slight_smile:

Not sure about Unity though, but in principle thereā€˜s nothing against multiple compilation threads once the dependencies are resolved.

In my limited experience of watching the build process (Iā€™ve only recently had a build big enough that it took a lengthy period of time) builds are a mixture of single- and multi-threaded tasks.

Unityā€™s SRP, which took the majority of the two hour build process for the project, requires building shader variants which will be combined at runtime to create the effects of the pipeline and the shader compiler completely maxed out my AMD Ryzen 5 1600X (6-Core/12-Thread) CPU.

is there any definite answer regarding unity compilation ?

Thanks

This question is difficult to answer properly as it already depends on what you understand by ā€œunity compilationā€. If you just talk about C# code compilation, that was already answered: no, the C# compiler itself is single threaded. About the mentioning that multiply compiler can of course run in parallel, thatā€™s almost never happening. In C#, unlike C++ where every cpp file is compiled seperately, all code that makes up an assembly is compiled at the same time. By default Unity only creates a single assembly for your code (not entirely true if you consider the first pass and the editor code assemblies). Assembly definition files can cut down the compilation time but they can also increase it. This mainly depends on what code is changed and what need to be recompiled. Assembly definition files can specify dependencies on other assemblies. Whenever one dependency need to be recompiled, all assemblies that depending on that one have to be recompiled as well. This chain of compilation can not be carried out in parallel since the next assembly requires the dependencies to be compiled first.

I think one can use Assembly definition files when the general code structure is specifically setup to benefit from them. So avoiding direct dependencies between assemblies helps to keep the compilation time low. Common structures and interfaces should be placed in a seperate assembly that all the others depends on. Of course changes to this assembly essentially enforces a complete recompilation. Though the main advantage is that small changes in the ā€œleafā€ assemblies would not cause a complete recompilation. Thatā€™s where most of the iterative coding should take place.

If by unity compilation you mean the building process, Unity may use multithreading for certain parts of the build pipeline internally, but as others have said the biggest bottleneck is usually the HDD. Most tasks while building the game need to be sequencial. Some sub tasks may benefit from multithreading, but I donā€™t think itā€™s a huge game changer. Apart from that almost all CPUs nowadays already have at least 2 but usually 4 cores anyways.

In 2018.2.21 when creating an IL2CPP build I primarily see just 1 core under load. In 2019.4.16, at some points during the build process all of my 8 cores are at full load while at other points only a single core (most of the process is not using all cores). I donā€™t know exactly what part of the build process is utilizing all cores.

With CPU lightmap baking, youā€™d frequently find yourself using all cores waiting for your scenes to bake, and might find yourself doing this several times per day. With the newer GPU lightmap baking option, the need for large core counts isnā€™t as important if you have a sufficiently high performing GPU. Unity is getting better at utilizing multiple cores, the DOTS/ECS system is intended for multiple core utilization, and you may be using other tools which use high core count CPUā€™s well. But Unity primarily still bottlenecks on its main thread, so high single core performance is probably more important than high core counts now if youā€™re not using CPU lightmap baking.

The IL2CPP compilation process can roughly be broken down into two steps:

  • Conversion from IL ā†’ C++ code
  • Compilation of the resulting C++ code

Starting with Unity 2020.2, that first conversion step is multi-threaded, prior to Unity 2020.2, it is single threaded. The C++ compilation step has used multiple cores (one per C++ file) since the Unity 5 days. From the IL2CPP side of things, improved build performance is our top development priority now. In 2021 weā€™ll release more performance improvements.

7 Likes

Well, that aged like milk. Unity is slower than ever.
Please do not release any more ā€œperformance improvementsā€.

1 Like

FWIW the player build performance is much faster with IL2CPP in Unity 2021 and Unity 2022, especially for incremental player builds, when only a few portions of the code are changed. If you find that player builds are not faster, please let me know, as that is something we would like to investigate.

With that said though, yes, other parts of Unity have gotten slower over that time.

1 Like

Weā€™re on the cusp of ordering several expensive ($7-10K) systems. To date on Unity 2022 our greatest time sink is compiling shader variants.

Will a high core CPU offer us benefit in reducing build times over a higher Ghz, lower core CPU? (Ala 32 core Threadripper vs Ryzen 9 vs Intel I-9)?

Be sure to come back and let us know!

I donā€™t have any good answers for this, sorry. Iā€™m not too familiar with the shader compilation pipeline.

For IL2CPP and C++ compilation, weā€™ve found that the best option is usually fast disk access plus fewer, faster cores.

Thanks for the response, do you know who we could @ that would be able to offer some insight on the shader pipeline?

Iā€™ve got a note on the RFP to prefer PCIE 5.0 SSDā€™s over 4.0 SSDā€™s as the speed increase is insane. 10-15GBps will make a sizeable dent in the random I/O portion of a build

Hi, Reahreic. Sorry to hear your build times are way up because of shader variant compilation. This is a common issue for customers in Unity 2021 because of all the features that were added to URP which are turned on by default. The downside of that power and convenience is longer builds. Weā€™ve done quite a bit of work to try to minimize the problem and get build times back down. If you look in the Shaders forum, youā€™ll see a lot of threads about this exact topic! The Shader Management team has recently published a blog post talking about some of the improvements weā€™ve made to the build process to speed up variant compilation. We also cover some tips you can apply to reduce the number of shader variants that Unity creates.

Hopefully that helps get your overall build times back down! In terms of hardware, we do multi-thread the shader compilation and we usually max out all available cores - but there are unavoidable sync points and single-threaded phases still. Our general recommendation is to prioritize (in order):

  • CPU Clock
  • RAM
  • Disk IO
  • CPU Cores
2 Likes

Thank you, for taking the time to reply, Iā€™ll certainly take a look at the blog.

Shader compilation is now multi-threaded (as of Unity 2023), and the speedup from having more CPU cores (16 in my case) is absolutely insane. I went from 20 minute initial build time to maybe 5 (including IL2CPP and other).

A rebuild is of course much faster.

1 Like