I just noticed that some of my jobs in game run slower in IJobParallelFor than in a single threaded IJob. I thought my code might be very parallelization unfriendly, maybe too small for parallelization benefits to kick in. But I made a little test with a giant array, where you’d think the benefits would be greatest with simplest possible code, and it is also true. I tried turning off leak checks, safety checks, it’s still the same ratio.
Here is how my profiler looks (tested in standalone with attach profiler):
As you can see, they both take about the same total time. It doesn’t make any sense.
Here is my test code:
Code
using Unity.Burst;
using Unity.Collections;
using Unity.Jobs;
using UnityEngine;
public class JobTest : MonoBehaviour
{
[BurstCompile, NoAlias]
public struct ArrayJob : IJob
{
public NativeArray<float> floats;
public void Execute()
{
int ln = floats.Length;
for (int i = 0; i < ln; i++)
{
floats[i] += 0.34f;
}
}
}
[BurstCompile, NoAlias]
public struct ArrayParallelJob : IJobParallelFor
{
public NativeArray<float> floats;
public void Execute(int i)
{
floats[i] += 0.34f;
}
}
[BurstCompile]
public struct ArraySliceJob : IJob
{
public NativeSlice<float> floats;
public void Execute()
{
for (int i = 0; i < floats.Length; i++)
{
floats[i] += 0.34f;
}
}
}
NativeArray<float> floats;
int LENGTH = 16777216;
void Start()
{
floats = new NativeArray<float>(LENGTH, Allocator.Persistent);
}
private void OnDestroy()
{
floats.Dispose();
}
void Update()
{
if (Input.GetKey(KeyCode.Alpha1))
new ArrayParallelJob() { floats = floats }.Schedule(floats.Length, 8).Complete();
if (Input.GetKey(KeyCode.Alpha2))
new ArrayJob() { floats = floats }.Schedule().Complete();
#if SLICE
JobHandle handle = new JobHandle();
for (int i = 0; i < 8; i++)
{
handle = JobHandle.CombineDependencies(handle,
new ArraySliceJob() { floats = floats.Slice(LENGTH / 8 * i, LENGTH / 8) }.Schedule());
}
handle.Complete();
#endif
}
}
As you can see in code sample I tried manually parallelizing with slices, which works fine with a single slice, but looks running these in parallel is not possible as even non-overlapping slices are not allowed to run in parallel (I thought this was the point of slices).
Is there something I’m doing wrong? I really need to process big arrays so this is crucial to me.
Running in 2019.3.0f6 with Jobs package version 0.2.5