[Burst] Questionable optimization diagnostic

Given the following block of code, I get an optimization warning in the Burst inspector about a switch statement: “Remark: :0:0: loop not vectorized: loop contains a switch statement”. If I remove this chunk (or simplify it to have no branching) the warning vanishes. I’m guessing this is being optimized into a switch statement, which is then blocking vectorization? Any way I can deal with this on my end?

public static float GetTargetDensity(Vector3 point, Vector3 center, Vector3 normal, DensityShape shape, VoxelEditType editType, float radius, float stretchScale = 0.0f)
    {
        var density = 0f;
        if (shape == DensityShape.Sphere)
        {
            return SDFHelper.sphere(point - center, radius);
        }

        if (shape == DensityShape.Ellipsoid)
        {
            return SDFHelper.ellipsoid(point - center, normal);
        }

        if (shape == DensityShape.Cube)
        {
            return SDFHelper.box(point - center, radius);
        }

        if (shape == DensityShape.Plane)
        {
            float3 p = (point - center);
            float4 n = math.normalize(new float4(normal, 1));
            return SDFHelper.plane(p, n);
        }

        density = 0f;

        return density;
    }

rearrange your data. group by shape type. then instead of (pseudocode)

foreach ([shape, ...] in data) {
  if shape == sphere {...}
  if shape == ellipsoid {...}
  ...etc
}

you will write

foreach (... in spheres) {
    sphere
}
foreach (... in ellipsoids) {
    ellipsoid
}
...etc...
1 Like

try a … switch statement !
a complete (exhaustive) one, with the default:

I’ve read somewhere that trivial switch on enum gets optimized by burst ( as a lookup table if I remember correctly)