Managing Multiple Collections

Let’s say in my example I have multiple entities that want to collect any DistanceHit. They use a NativeList to collect and process them in a job. In general, we should allocate the NativeList outside the job because allocationg in a job is slow. But each threads need its own NativeList or there will be a race condition. In that situation, how can you manage multiple collection between all thread ?

struct SimpleJob : IJobForEach<Translation>
{
    [ReadOnly] CollisionWorld CollisionWorld;

    [DeallocateOnJobCompletion] NativeList<DistanceHit> collector;

    public void Execute(ref Translation c0)
    {
        PointDistanceInput input = new PointDistanceInput();

        CollisionWorld.CalculateDistance(input, ref collector);

        //Process them ...
    }
}

Like in this case, IJobForEach can run on 2 threads but have the same collector. Should I allocate a NativeList on the stack or do we have a better solution ?

Compared to the cost of the physics query the allocation isn’t that significant really. The other alternative is create a custom collector.