Job system burst

From nlelouche/Unity-SkillForge · View interactive version

Job system burst is an AI agent skill from the GitHub repository nlelouche/Unity-SkillForge. Implements high-performance parallel CPU workloads using the Unity Job System and Burst Compiler. Achieves near-native C code performance with zero managed allocations.

How to use this skill

Skill file

Job System + Burst

Overview

The Unity Job System combined with the Burst Compiler enables deterministic, multi-threaded, allocation-free CPU computation. Burst compiles C# to highly optimized native machine code (SIMD vectorization, loop unrolling) while the Job System's safety system prevents race conditions at compile time. Use this for any CPU-bound computation: pathfinding, physics simulation, procedural generation, AI decisions.

When to Use

Architecture

┌────────────────────────────────────────────────────────────┐
│                  JOB SYSTEM PIPELINE                       │
├────────────────────────────────────────────────────────────┤
│                                                            │
│  MAIN THREAD                 WORKER THREADS               │
│  ┌─────────────┐             ┌────────────────────────┐   │
│  │ Allocate    │             │  Job.Execute(index)    │   │
│  │ NativeArray │────Schedule─▶  (Full Burst Native)  │   │
│  │ (Persistent │             │  NativeArray reads +  │   │
│  │  or TempJob)│             │  writes ONLY          │   │
│  └─────────────┘             └────────────────────────┘   │
│        │                              │                    │
│        │     JobHandle                │                    │
│        └──────────────────────────────┘                   │
│        ▼                                                   │
│  Complete() → Read results on main thread                  │
│                                                            │
│  ALLOCATOR LIFETIMES:                                      │
│  TempJob  → within same frame   (Schedule + Complete)      │
│  Temp     → within same method  (rarely used in jobs)      │
│  Persistent → across frames     (Dispose in OnDestroy)     │
└────────────────────────────────────────────────────────────┘

Job Types

| Type | Use Case | Parallelism |

|------|---------|------------|

| IJob | Single task (e.g., pathfinding for one unit) | None |

| IJobParallelFor | Same work over N items (e.g., update N agents) | Automatic batch split |

| IJobFor | Like ParallelFor but with explicit inner loop control | Manual |

| IJobChunk | ECS-only: process entity archetype chunks | Per-chunk |

TDD Contract

// Write these tests BEFORE implementing:
[Test]
public void MovementJob_WhenScheduled_MovesAllAgentsForward()
{
    // Arrange
    var positions = new NativeArray<float3>(10, Allocator.TempJob);
    var velocities = new NativeArray<float3>(10, Allocator.TempJob);
    for (int i = 0; i < 10; i++) velocities[i] = new float3(0, 0, 1);

    // Act
    var job = new MoveAgentsJob { Positions = positions, Velocities = velocities, DeltaTime = 0.016f };
    job.Schedule(10, 64).Complete();

    // Assert
    Assert.AreApproximatelyEqual(0.016f, positions[0].z, 0.001f);

    // Cleanup
    positions.Dispose();
    velocities.Dispose();
}

Procedure

  1. Context Check: Verify com.unity.burst and com.unity.collections are in manifest.json.
  2. Define Job Struct: Create [BurstCompile] struct MyJob : IJobParallelFor.
  3. Declare NativeArrays: All data passed via NativeArray<T> or NativeList<T>.
  4. Annotate Access: Use [ReadOnly] on input arrays for maximum parallelism.
  5. Schedule: Call job.Schedule(count, batchSize) → get JobHandle.
  6. Complete: Call handle.Complete() before reading results on main thread.
  7. Dispose: Always dispose NativeArray in OnDestroy() or after use.

Best Practices

Few-Shot Examples

Example 1: Parallel Agent Movement (IJobParallelFor)

User: "My 500 agents update their positions on the main thread and it's tanking FPS."

Agent:

using Unity.Burst;
using Unity.Collections;
using Unity.Jobs;
using Unity.Mathematics;
using UnityEngine;

[BurstCompile]
public struct MoveAgentsJob : IJobParallelFor
{
    [ReadOnly] public NativeArray<float3> Velocities;
    public NativeArray<float3> Positions;
    public float DeltaTime;

    public void Execute(int index)
    {
        Positions[index] += Velocities[index] * DeltaTime;
    }
}

public class AgentSystem : MonoBehaviour
{
    private NativeArray<float3> _positions;
    private NativeArray<float3> _velocities;
    private JobHandle _jobHandle;
    private const int AgentCount = 500;

    private void Awake()
    {
        _positions  = new NativeArray<float3>(AgentCount, Allocator.Persistent);
        _velocities = new NativeArray<float3>(AgentCount, Allocator.Persistent);
    }

    private void Update()
    {
        var job = new MoveAgentsJob
        {
            Positions  = _positions,
            Velocities = _velocities,
            DeltaTime  = Time.deltaTime
        };
        // Schedule: 500 items, 64 per batch
        _jobHandle = job.Schedule(AgentCount, 64);
    }

    private void LateUpdate()
    {
        _jobHandle.Complete(); // Read results after all jobs finish
        // Apply to transforms here...
    }

    private void OnDestroy()
    {
        _jobHandle.Complete(); // Safety: complete before dispose
        _positions.Dispose();
        _velocities.Dispose();
    }
}

Example 2: Single Background Task (IJob)

User: "Run a pathfinding calculation on a background thread."

Agent:

[BurstCompile]
public struct PathfindingJob : IJob
{
    [ReadOnly] public NativeArray<int> Grid;
    public NativeArray<int> ResultPath;
    public int2 Start;
    public int2 End;
    public int GridWidth;

    public void Execute()
    {
        // A* or Djikstra implementation here
        // Pure data — no Unity API calls allowed
    }
}

Performance Notes

| Scenario | Single-threaded | Job System + Burst |

|----------|:--------------:|:-----------------:|

| 500 agent positions | ~8ms | ~0.4ms |

| GC Allocation | Yes (closures) | 0 bytes |

| SIMD vectorization | ❌ | ✅ Auto |

Related Skills

Template Files

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.