Use IJobParallelFor for large iterations
masterFor high-volume, small operations (like iterating over a large dataset), implement IJobParallelFor instead of IJob. This is more efficient than scheduling thousands of individual jobs.
Implementation Requirements:
Execute(int i): The logic to run for each indexi.Finish(): Logic to run once all indices are processed.BatchSize(int): Determines how many indices are processed per thread per dispatch.- High BatchSize: Minimizes scheduler overhead for very small tasks.
- Low BatchSize: Maximizes work-stealing efficiency for complex/mixed tasks.
ThreadCount(int): Number of spawned jobs. Set to0to use the scheduler's active thread count.
Usage:
Pass the job and the total number of iterations to scheduler.Schedule(job, count).
public class ManyCalculations : IJobParallelFor
{
public void Execute(int i) { /* ... */ }
public void Finish() { /* ... */ }
public int BatchSize => 32;
public int ThreadCount => 0;
}
// ...
var job = new ManyCalculations();
var handle = scheduler.Schedule(job, 512); // Executes 512 times