Entities.ForEach().ForChunk().ForThread() ?

I think this might have been discussed in the past, but I’m interested in hearing updates about future plans for these things.

The most common reason for me not using Entities.ForEach() is when I want to do some once-per-thread logic, like initializing temporary NativeLists in ScheduleParallel jobs, that will live only for the duration of the job. Example of what I imagine:

    public class MySystem : SystemBase
    {
        protected override void OnUpdate()
        {
            NativeList<int> listForParallelJob;

            Entities
                .ForThread(() =>
                {
                    // Do something for each thread launched by the job
                    if(!listForParallelJob.IsCreated)
                    {
                        listForParallelJob = new NativeList<int>(Allocator.Temp);
                    }
                })
                .ForChunk(() =>
                {
                    // possibility of doing something per chunk
                })
                .ForEach((Entity entity, ref Translation translation) =>
                {
                    // Use listForParallelJob in some way
                })
                .ScheduleParallel();
        }
    }

Can we expect something like this in the future?

Also interested in hearing updates on the IJobEntity discussed in the past. I have cases where I need to operate on too many component types (more than what Entities.ForEach allows) and this new IJobEntity would save me from a ton of boilerplate. Ideally IJobEntity should also give us a “OnBeginThread()” and “OnBeginChunk()”

14 Likes

I would also like to see a feature like this.

This would save my poor wrists from so much typing

2 Likes

I think the link to the initial discussion should be here for the context with previous Unity answers :wink: Is it possible to allocate a new Temp NativeList per thread in Entities.ForEach?

2 Likes