- Home /
Question by
Squitz · Jul 04, 2020 at 01:24 PM ·
c#performanceperformance optimizationsystem
Job System, workers are idle even though Asked to complete
Hi there! So I am starting with the job system. And I made this code (below) But when I look at the profiler the worker treads are always idle. even though I asked to complete it. When I looked at other examples everything should work. any ideas? My game performance is kinda trash so I need this.
This is my code run on the main tread:
public void UseAllAutos()
{
//Getting all the values in the respective arrays
AllamountsAuto = new NativeArray<int>(AllAutos.Count * 4, Allocator.Persistent);
AllSamountsAuto = new NativeArray<int>(AllAutos.Count * 4, Allocator.Persistent);
AllGrabbedAuto = new NativeArray<bool>(AllAutos.Count * 4, Allocator.Persistent);
for (int i = 0; i < AllAutos.Count * 4; i += 4)
{
for (int j = 0; j < 4; j++)
{
if (j < AllAutos[i / 4].MyFarms.Count) AllamountsAuto[i + j] = AllAutos[i / 4].MyFarms[j].Amount;
if (j < AllAutos[i / 4].MyFarms.Count) AllSamountsAuto[i + j] = AllAutos[i / 4].MyFarms[j].Amount;
AllGrabbedAuto[i + j] = false;
}
}
//making the instance with the correct values
var autoInc = new UseAutos()
{
AllamountsHere = AllamountsAuto,
AllSamounts = AllSamountsAuto,
AllGrabbed = AllGrabbedAuto
};
//asking to complete the job
JobHandle Autojob = autoInc.Schedule(AllAutos.Count*4, AllAutos.Count/4);
Autojob.Complete();
//putting the values in the correct spots
for (int i = 0; i < AllAutos.Count * 4; i += 4)
{
for (int j = 0; j < 4; j++)
{
if (AllGrabbedAuto[i + j])
{
if (j <= AllAutos[i / 4].MyFarms.Count)
{
if (j < AllAutos[i / 4].MyFarms.Count) AllAutos[i / 4].MyFarms[j].Amount = AllSamountsAuto[i + j];
if (j < AllAutos[i / 4].MyFarms.Count) AddMaterial(AllAutos[i / 4].MyFarms[j].Mine.WhatIMake, AllamountsAuto[i + j]);
}
}
}
}
//no memory leaks here!!
AllamountsAuto.Dispose();
AllSamountsAuto.Dispose();
AllGrabbedAuto.Dispose();
}
This is my code trying to run on the worker's treads (can run up to 1k times when stressing my game):
public struct UseAutos : IJobParallelFor
{
[ReadOnly] public NativeArray<int> AllamountsHere;
[WriteOnly] public NativeArray<int> AllSamounts;
[WriteOnly] public NativeArray<bool> AllGrabbed;
[ReadOnly]public int MaxToGrab;
public void Execute(int index)
{
if (AllamountsHere[index] > MaxToGrab)
{
AllSamounts[index] = 0;
AllGrabbed[index] = true;
}
}
}
Comment