You are only allowed to schedule a job if all native container fields are populated at the start of the job, however allocating on the main thread is slower than Allocator.Temp
For this reason, Temp allocations must be made inside the job, however that means you have to pass them as an explicit argument to every single function that needs to access them. When you have a lot of data structures going around, this can get pretty tedious and hard to read. It’s quite nice to have them as member variables of the job itself
The quick solution is to wrap the real job in a parent job, and then allocate that memory in the parent job
[BurstCompile]
public struct ComputeVerticesJobWrapper : IJob
{
[ReadOnly]
private readonly NativeArray<float3> inputPoints;
private readonly NativeHashMapList<int, foo> faces;
private readonly float epsilon;
public ComputeVerticesJobWrapper(NativeArray<float3> inputVertices, NativeHashMapList<int, foo> faces, float epsilonMultiplier = 1f)
{
inputPoints = inputVertices;
this.faces = faces;
epsilon = 0.0001f * epsilonMultiplier;
}
public void Execute()
{
int numVertices = inputPoints.Length;
NativeList<bool> litFaces = new NativeList<bool>(3 * numVertices / 2, Allocator.Temp);
NativeList<foobar> horizon = new NativeList<foobar>(numVertices, Allocator.Temp);
NativeList<foo> openSet = new NativeList<foo>(numVertices, Allocator.Temp);
ComputeVerticesJob computeVerticesJob = new ComputeVerticesJob(inputPoints, faces, openSet, litFaces, horizon, epsilon);
computeVerticesJob.Execute();
}
}
I wonder if it would be possible to have some way to allow us to initialise these at the start of job execution rather than before the job. For example
[AllocatedAtJobBegin]
private NativeList horizon;