Lesson 61 +10 XP

C# Job System & Burst Compiler

C# Job System & Burst Compiler

To maximize game performance, Unity provides the Data-Oriented Technology Stack (DOTS), which includes the C# Job System and the Burst Compiler.

C# Job System

Normally, Unity scripts run on a single thread (the main thread). The C# Job System allows you to write safe, multithreaded code that takes advantage of multi-core CPUs.

  • It prevents race conditions (two threads writing to the same memory at once) through safety checks.
  • Jobs only work with value types (structs and native containers, not class references).

IJobParallelFor is a common job interface used to compute math over large arrays of data:

using Unity.Jobs;
using Unity.Collections;

public struct MovementJob : IJobParallelFor
{
    public NativeArray<Vector3> positions;
    public NativeArray<Vector3> velocities;
    public float deltaTime;

    public void Execute(int index)
    {
        positions[index] += velocities[index] * deltaTime;
    }
}

The Burst Compiler

The Burst Compiler is a highly optimized compiler technology that translates C# job code into optimized machine code.

  • It uses LLVM to optimize instructions for target platforms.
  • Simply add the [BurstCompile] attribute above your job struct to enable it, resulting in massive speed gains.

TL;DR

  • C# Job System compiles multithreaded operations safely.
  • Jobs work only with value types and native collections (like NativeArray).
  • The Burst Compiler uses LLVM to optimize job code for specific CPU architectures.
  • Add [BurstCompile] to job structs to enable compiler optimization.