Burst Compiler causes worse performance URP

Hello, I am using Unity 2020.3.26f1 with the Universal Rendering Pipeline with the following packages:

Jobs 0.11.0-preview.6, Universal RP 10.8.1, Burst 1.5.4, Mathematics 1.2.1

I am using the code from this tutorial: Unity Job System and Burst Compiler: Getting Started | Kodeco

Which essentially just samples Perlin noise and calculates the position of vertices on a mesh with the following Job:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Jobs;
using Unity.Collections;
using Unity.Burst;
using Unity.Jobs;
using System.Threading.Tasks;
using Unity.Mathematics;

public class WaveGenerator : MonoBehaviour
{
    [Header("Wave Parameters")]
    public float waveScale;
    public float waveOffsetSpeed;
    public float waveHeight;

    [Header("References and Prefabs")]
    public MeshFilter waterMeshFilter;
    private Mesh waterMesh;

    //Private Mesh Job Properties
    NativeArray<Vector3> waterVertices;
    NativeArray<Vector3> waterNormals;

    //Job Handles
    UpdateMeshJob meshModificationJob;
    JobHandle meshModificationJobHandle;

    private void Start()
    {
        InitialiseData();  
    }

    //This is where the appropriate mesh verticies are loaded in
    private void InitialiseData()
    {
        waterMesh = waterMeshFilter.mesh;

        //This allows Unity to make background modifications so that it can update the mesh quicker
        waterMesh.MarkDynamic();

        //The verticies will be reused throughout the life of the program so the Allocator has to be set to Persistent
        waterVertices = new NativeArray<Vector3>(waterMesh.vertices, Allocator.Persistent);
        waterNormals = new NativeArray<Vector3>(waterMesh.normals, Allocator.Persistent);
    }

    private void Update()
    {
        //Creating a job and assigning the variables within the Job
        meshModificationJob = new UpdateMeshJob()
        {
            vertices = waterVertices,
            normals = waterNormals,
            offsetSpeed = waveOffsetSpeed,
            time = Time.time,
            scale = waveScale,
            height = waveHeight
        };

        //Setup of the job handle
        meshModificationJobHandle = meshModificationJob.Schedule(waterVertices.Length, 64);
    }

    private void LateUpdate()
    {
        //Ensuring the completion of the job
        meshModificationJobHandle.Complete();

        //Set the vertices directly
        waterMesh.SetVertices(meshModificationJob.vertices);
       
        //Most expensive
        waterMesh.RecalculateNormals();
    }

    private void OnDestroy()
    {
        // make sure to Dispose any NativeArrays when you're done
        waterVertices.Dispose();
        waterNormals.Dispose();
    }

    [BurstCompile]
    private struct UpdateMeshJob : IJobParallelFor
    {
        public NativeArray<Vector3> vertices;
        public NativeArray<Vector3> normals;

        [ReadOnly]
        public float offsetSpeed;

        [ReadOnly]
        public float time;

        [ReadOnly]
        public float scale;

        [ReadOnly]
        public float height;

        public void Execute(int i)
        {
            //Vertex values are always between -1 and 1 (facing partially upwards)
            if (normals[i].z > 0f)
            {
                var vertex = vertices[i];

                float noiseValue = Noise(vertex.x * scale + offsetSpeed * time, vertex.y * scale + offsetSpeed * time);

                vertices[i] = new Vector3(vertex.x , vertex.y, noiseValue * height + 0.3f);
            }
        }

        private float Noise(float x, float y)
        {
            float2 pos = math.float2(x, y);
            return noise.snoise(pos);
        }
    } 
}

If I use the [BurstCompile] flag, I seem to get even worse FPS performance than if I take it off.

This is pretty hard to believe without visual proof. Can you show us profiler captures?

Safety checks on maybe?

try running with less than 64 batches, maybe 1 or 2

@francois85 , turning safety checks off didn’t increase performance, neither did using 1-2 batches @print_helloworld . @DreamingImLatios I’ve attached the profilers for both the No burst compiler run and the burst compiler run, and they look very similar. Is the high memory usage concerning?


From your profiler captures it seems your project is running better with Burst. Your peak frame times stay under 66 ms with Burst and the blue area for scripts without Burst seems to disappear with Burst.

Are you running in burst even for the first frame your job runs? Or is it compiling asynchronously? Try turning on synchronous compilation. If that helps you can use the compile synchronously attribute on your job if that’s preferable.