Burst is primarily designed to work efficiently with the Job system.
You can start using the Burst compiler in your code by simply decorating a Job struct with the attribute [BurstCompile]
using Unity.Burst;
using Unity.Collections;
using Unity.Jobs;
using UnityEngine;
public class MyBurst2Behavior : MonoBehaviour
{
void Start()
{
var input = new NativeArray<float>(10, Allocator.Persistent);
var output = new NativeArray<float>(1, Allocator.Persistent);
for (int i = 0; i < input.Length; i++)
input[i] = 1.0f * i;
var job = new MyJob
{
Input = input,
Output = output
};
job.Schedule().Complete();
Debug.Log("The result of the sum is: " + output[0]);
input.Dispose();
output.Dispose();
}
// Using BurstCompile to compile a Job with Burst
// Set CompileSynchronously to true to make sure that the method will not be compiled asynchronously
// but on the first schedule
[BurstCompile(CompileSynchronously = true)]
private struct MyJob : IJob
{
[ReadOnly]
public NativeArray<float> Input;
[WriteOnly]
public NativeArray<float> Output;
public void Execute()
{
float result = 0.0f;
for (int i = 0; i < Input.Length; i++)
{
result += Input[i];
}
Output[0] = result;
}
}
}
By default (only within the Editor - See AOT vs JIT), Burst JIT compiles jobs asynchronously, but the example above uses the option CompileSynchronously = true
to make sure that the method is compiled on the first schedule. In general, you should use asynchronous compilation. See [BurstCompile]
options
The Burst package adds a few menu entries to the Jobs menu for controlling Burst behavior:
[BurstCompile]
. Default is checked.NativeArray<T>
). Checks include job data dependency and container indexes out of bounds. This is the default.DisableSafetyChecks = true
. Prior to reporting any Burst bug this option should be set to rule out any problems that safety checks could have caught.[BurstCompile]
options. Default is unchecked.The Burst Inspector window displays all the Jobs and other Burst compile targets in the project. Open the Inspector from the Jobs menu (Jobs > Burst Inspector).
The inspector allows you to view all the Jobs that can be compiled, you can also then check the generated intermediate and native assembly code.
On the left pane of the window, Compile Targets provides an alphabetically sorted list of the Jobs in the project that Burst can compile. Note that the disabled Jobs in the list don’t have the [BurstCompile]
attribute.
On the right pane, the window displays options for viewing the assembly and intermediate code for the selected compile target.
To view the disassembly for a Job:
<color=#444444>foo</color>
tags). In general its best to view the coloured output in the inspector, but copy the plain output if you want to move it into an external tool.
You can pass the following options to the Unity Editor on the command line to control Burst:
--burst-disable-compilation
— turns Burst off.--burst-force-sync-compilation
— Burst always compiles synchronously. See [BurstCompile]
options.When working on your projects in the editor (Play Mode), Burst works in a Just-In-Time (JIT) fashion. Burst will compile your code at the point that it is to be used. By default this is done asnychronously which means your code will be running under the default mono JIT until the compilation by Burst has been completed.
You can control this behaviour via [BurstCompile]
options.
However, when you build your project into a Standalone Player, Burst will instead compile all the supported code Ahead-Of-Time (AOT). AOT compilation at present, requires access to some linker tools for the respective platforms (similar to the requirements of IL2CPP). See Burst AOT Requirements.