Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question No performance difference between Run, Schedule and ScheduleParallel

Discussion in 'Entity Component System' started by Steff00212, Aug 15, 2023.

  1. Steff00212

    Steff00212

    Joined:
    May 17, 2013
    Posts:
    14
    Heya, so I've created a random movement system for a project:

    Code (CSharp):
    1. using Unity.Collections;
    2. using Unity.Core;
    3. using Unity.Entities;
    4. using Unity.Mathematics;
    5. using Unity.Transforms;
    6.  
    7. public partial class AgentMoveSystem : SystemBase
    8. {
    9.     protected override void OnUpdate()
    10.     {
    11.  
    12.         float deltaTime = World.Time.DeltaTime;
    13.         Random rnd = new Random();
    14.         rnd.InitState(49500);
    15.  
    16.  
    17.         AgentSpawnSettings settings = EntityManager.GetComponentData<AgentSpawnSettings>(SystemAPI.GetSingletonEntity<AgentSpawnSettings>());
    18.  
    19.         float3 minPos = settings.MinPos;
    20.         float3 maxPos = settings.MaxPos;
    21.  
    22.         Entities
    23.             .ForEach((ref MovementAgent agent, ref LocalTransform localTransform) =>
    24.             {
    25.  
    26.                 localTransform.Position =
    27.                 new float3(localTransform.Position.x + rnd.NextFloat(), localTransform.Position.y + rnd.NextFloat(), localTransform.Position.z);
    28.  
    29.                 if (localTransform.Position.y < minPos.y) localTransform.Position =
    30.                     new float3(localTransform.Position.x, maxPos.y, localTransform.Position.z);
    31.  
    32.                 if (localTransform.Position.y > maxPos.y) localTransform.Position =
    33.                     new float3(localTransform.Position.x, minPos.y, localTransform.Position.z);
    34.  
    35.                 if (localTransform.Position.x < minPos.x) localTransform.Position =
    36.                     new float3(maxPos.x, localTransform.Position.y, localTransform.Position.z);
    37.  
    38.                 if (localTransform.Position.x > maxPos.x) localTransform.Position =
    39.                     new float3(minPos.x, localTransform.Position.y, localTransform.Position.z);
    40.  
    41.  
    42.             }).ScheduleParallel(); //no difference between Run(), Schedule() and ScheduleParallel()
    43.     }
    44. }
    The issue is that Run(), Schedule() and ScheduleParallel() all have the same performance. There are 200000 MovementAgent entities with a total of 7 different AgentFlag IComponentData (AgentFlagOne, AgentFlagTwo, AgentFlagThree and so on)

    Given that my CPU has 8 physical cores, I'd expect it to be around 80% utilized. But it's not, and there's no performance difference between the three different methods.

    What am I missing? Appreciate any help.
     
  2. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    943
    Make sure that Burst is enabled (Jobs > Burst > Enable Compilation).
     
  3. Steff00212

    Steff00212

    Joined:
    May 17, 2013
    Posts:
    14
    Burst is already enabled.
     
  4. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,292
    Its usually easier to tell what's going on by looking at the Profiler -> Timeline -> Jobs.
    Enable flow events in details menu (three dots to the right), start from the system and look bellow into jobs and where they are scheduled.

    Maybe they're completed instantly. Or something else is going on in the background.
    You should definitely see differences in performance.
    Multitude of which depends on the algorithm you're trying to parallelize and how effective data layout is.
     
  5. Steff00212

    Steff00212

    Joined:
    May 17, 2013
    Posts:
    14
    I had a look at the Profiler data. And it turns out that the worker threads completed before the PlayerLoop was done. So I multithreaded, but without any impact on performance.
     
    apkdev and xVergilx like this.
  6. Laicasaane

    Laicasaane

    Joined:
    Apr 15, 2015
    Posts:
    288
    Have you tried ISystem and IJobEntity?