CodeBlanks Logo Icon CodeBlanks

Burst compiler

Learn high-performance code with the Burst compiler.

What Is Burst Compiler?

Burst Compiler is a compiler developed by Unity that converts certain C# code into high-performance machine code. It helps improve execution speed, especially in systems with intensive mathematical operations. Burst can take advantage of processor features such as SIMD in supported code.

using Unity.Burst;

[BurstCompile]
public struct CalculationJob
{
    // Calculation operations
}

To use Burst, the relevant package must be present in the project and supported code structures must be used.

The BurstCompile Attribute

You can add the BurstCompile attribute to a supported method or job structure that you want Burst to compile. This attribute allows the Burst compiler to compile the relevant code. Burst can be useful especially for mathematical operations and computation-intensive tasks.

using Unity.Burst;

public static class MathOperations
{
    [BurstCompile]
    public static float DoubleValue(float value)
    {
        return value * 2f;
    }
}

Burst does not support every C# feature, so the compatibility of the code being used is important.

Practice Create a Burst-Enabled Calculation Class

Create a static class named BurstMath. Define a static method named Multiply and add the BurstCompile attribute above the method. The method must accept two float parameters and return their product. Print the result to the console.

Starter code

Player.cs
using Unity.Burst;
using UnityEngine;

// Statik sınıfı ve Multiply metodunu tanımla

public class BurstMathTest : MonoBehaviour
{
    void Start()
    {
        // Multiply metodunu çağır ve sonucu yazdır
    }
}

Sample solution

Player.cs
using Unity.Burst;
using UnityEngine;

public static class BurstMath
{
    [BurstCompile]
    public static float Multiply(float a, float b)
    {
        return a * b;
    }
}

public class BurstMathTest : MonoBehaviour
{
    void Start()
    {
        Debug.Log("Result: " + BurstMath.Multiply(3f, 4f));
    }
}

Expected output

Result: 12

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What is the main purpose of Burst Compiler?

  • To create scene objects automatically
  • To compile C# code into high-performance machine code Correct answer
  • To edit visual materials
  • To compress audio files

Explanation: Burst Compiler aims to improve performance by converting supported C# code into optimized machine code.

2 Add the Burst Attribute

Question
using Unity.Burst;

____
public struct CalculationJob
{
    public float Value;
}
Answer
using Unity.Burst;

[BurstCompile]
public struct CalculationJob
{
    public float Value;
}

Explanation: The BurstCompile attribute allows the Burst compiler to compile the supported code.

3 Burst Performance Concepts

Term Meaning
Burst Compiler Enables code optimization
BurstCompile Is a compilation attribute
SIMD Multiple data in one instruction
Machine Code Code executed by the processor

All lessons in Expert