Unity

Unity Burst Compiler: Performance Optimization Made Easy

The Unity Burst Compiler transforms your C# code into highly optimized machine code.

This year we saw the release of Red Dead Redemption 2 and Spider-Man – two AAA titles that pushed modern consoles to the absolute limit. Each game features a massive, simulated game world that raises the bar for game development both artistically and technologically.

Neither one would have been possible, however, without some extremely sophisticated code that was optimized to run efficiently on each individual console.

But these games were created by massive studios with specialized teams that could focus specifically on performance. How can smaller studios and indie developers keep up with the rate that the industry is changing?

In this post I’m going to show you how the Unity Burst Compiler allows you to write extremely optimized game code without a specialized team and with very little effort.

Performance and Optimzation

So how do you squeeze the most performance out of your code in Unity?

Well, for one, you can leverage multithreading using the C# Job System. Multithreaded code utilizes all of the cores in your processor so it executes faster.

Next, you can optimize the memory layout of your code using Unity ECS. Data that’s organized properly in memory is much easier for your CPU to access.

And lastly, you can transform your .NET bytecode into highly optimized machine code that’s designed to run natively on the processor of your target system.

And that’s where the Unity Burst Compiler comes in.

Getting the Most Out of Your Hardware

At the end of the day, achieving the best possible performance comes down utilizing the hardware to its fullest. And to do that, you need to optimize your code at the lowest level.

That means writing logic in languages like C++, or even C, that can be executed directly on the processor. This opens up the door to some insane optimizations like SIMD instructions, custom assemblers, and others that go beyond the scope of this post.

The drawback, however, – Well, besides having to right painfully complex code – Is that this approach blocks you from one of Unity’s most important features: the ability to publish your game on multiple platforms.

Mono and Multiplatform Support

You see, Unity’s entire scripting platform is based on an open-source project called Mono.

Mono is an implementation of the .NET Framework that can compile and run C Sharp code on a bunch of different software systems. On top of Windows, macOS, and most Linux distributions, Mono can even run on PlayStation, Xbox, and Wii.

It does this by compiling C Sharp scripts into managed code, which is a type of program code that’s executed by something called the Common Language Runtime virtual machine, or CLRVM.

The CLRVM is what makes multiplatform building possible.

It manages the memory, garbage collection, and execution of your code in a system agnostic way. And with it you can run C Sharp code on any system that supports Mono.

So it’s obviously pretty important because it provides so much flexibility. But where you gain in flexibility you pay in performance.

Achieving Highly Optimized Machine Code

Unfortunately, the cost of being able to execute your code on multiple platforms is fairly high.

And, while the great developers behind Mono have put a lot of work into optimizing the platform, the performance of managed code will never come close to that of unmanaged, native machine code.

So, what? Now you have to learn how to write complex, low level code on top of all the complicated game logic you’re already writing?

Luckily, the answer is “no”. Thanks to the Unity Burst Compiler, you don’t have to!

The Unity Burst Compiler

Burst is a new math-aware compiler developed by Unity. It can produce highly optimized machine code that takes full advantage of the platforms you’re compiling for.

It accomplishes this using some pretty sophisticated technology that utilizes the open-source LLVM project, which handles a lot of the heavy lifting.

And the best part is that it does this all automatically.

All you need to do is add the Unity.Burst package to your project. Then mark your C# jobs with the BurstCompile attribute.

Once you’ve done that, Unity will take care of the rest, compiling all of your Unity Job System code into highly optimized machine code.

The Current State of Burst

Now, before you run off to update all your code, you should keep in mind that the Burst Compiler is still in preview mode. And as of the date this article was posted, it’s not ready for production.

But, that doesn’t mean you can’t get a head start!

Burst is slated to be fully released in 2019 and you’ll want to be ready to take full advantage of it once it’s out.

Summary

So, thanks to the Unity Burst Compiler, you don’t need a specialized team of developers that only focus on optimizing your game code.

And you certainly don’t need to add complex, low-level code to the long list of things you need to learn in order to be a successful indie game developer.

All you need to do is leverage this new, amazing technology and you can easily keep up with the pace of the industry.

If you enjoyed this post, please a comment letting me know what you thought.

And for more Unity article just like this one, don’t forget to subscribe to our newsletter.