Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  5. Nominations have been announced for this years Unity Awards. Celebrate the wonderful projects made by your peers this year and get voting! Vote here!
    Dismiss Notice
  6. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Branch misprediction in Systems

Discussion in 'Entity Component System and C# Job system' started by Vacummus, Dec 7, 2018.

  1. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    61
    Branch misprediction can cause performance issues. Can the following code sample lead to branch mispredictions and if so what would be a good way to improve upon this?

    This is a simple example of entities following random points. Here we have a system that checks if an entity is near it's destination. If it's not, we keep updating it's direction towards it, otherwise we create a new random point for it to move towards a new destination.

    Code (CSharp):
    1. public struct FollowRandomPoints: IComponentData
    2. {
    3.     public float radius;
    4.     public float2 anchorPoint;
    5.     public float2 nextPoint;
    6.     public float distance;
    7. }
    8.  
    9. public struct MovementInput: IComponentData
    10. {
    11.     public float2 Axis;
    12. }
    13.  
    14. public class FollowRandomPointsSystem: JobComponentSystem
    15. {
    16.     [BurstCompile]
    17.     struct Job: IJobProcessComponentData<Position, FollowRandomPoints, MovementInput>
    18.     {
    19.         public void Execute(
    20.             [ReadOnly] ref Position position,
    21.             ref FollowRandomPoints followRandomPoints,
    22.             ref MovementInput movementInput
    23.         ) {
    24.             var nextPoint = followRandomPoints.nextPoint;
    25.             var currentPosition = position.value;
    26.             var distance = followRandomPoints.distance;
    27.  
    28.             if (!Utilities.IsWithinDistance(nextPoint, currentPosition, distance))
    29.             {
    30.                 movementInput.Axis = Utilities.GetDirection(currentPosition, nextPoint);
    31.             }
    32.             else
    33.             {
    34.                 var anchorPoint = followRandomPoints.anchorPoint;
    35.                 var radius = followRandomPoints.radius;
    36.  
    37.                 followRandomPoints.nextPoint = Utilities.RandomPoint(anchorPoint, radius);
    38.             }
    39.         }
    40.     }
    41.  
    42.     protected override JobHandle OnUpdate(JobHandle inputDeps)
    43.     {
    44.         return new Job { }.Schedule(this);
    45.     }
    46. }
     
  2. EthanHunt

    EthanHunt

    Joined:
    Oct 9, 2012
    Posts:
    13
    When you have an if statement, you stand a chance to win a branch misprediction. But the common case here will be that most of the time the agent/entity is predicted to be continuing to move, since that what should happen 99% of the time. However if you have multiple agents moving with different timings, chance of misprediction increases.

    But with all that said, I would not worry about this little case, at least not until the profiler tells me otherwise. A case of premature optimisation.
     
    Vacummus likes this.
  3. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    61
    Great points, thank you for sharing. Would a profiler be able to tell you how many branch mispredictions your system has? Or what signs would you look for when profiling this kind of system to see if it's worth optimizing?

    Also, assuming that branch misprediction rate is high enough to optimize this, any thoughts on what would be good ways in ECS to help the compiler predict branching more easily? My goal is to learn more about to how deal with branch mispredictions in ECS, and especially on what a good "default" is to avoiding branch mispredictions.
     
  4. EthanHunt

    EthanHunt

    Joined:
    Oct 9, 2012
    Posts:
    13
    A hardware counter based profiler like Intel V-tune can tell you approximately how many branch prediction misses, but it would be in multiple of hundred thousands, not the precise number.

    I'm not sure if ECS can really help you here in this very specific scenario. You will need to reconsider the higher level structure of your code. For example, ECS already helped you a lot by running update on one similar type of entities before moving on to another type of entities. So the cost of 1 branch is spread out over many cycles, making it less of a problem. The bad way to do update is just go through all entities and if check what type each one is.

    The compiler cannot help you predict the branches, it is done on the CPU, at runtime. And since the CPU does not know much about the code it may need to run, it can only make a guess base on history of how the branch was used in the previous executions. Check this answer, it is very detailed on how the hardware predictor works: https://www.quora.com/CPUs-How-is-branch-prediction-implemented-in-microprocessors. But then again the branch preditor nowadays works fairly well, as long as you can maintain a somewhat consistent behaviour loop after loop. This is the reason why I predicted that the branch predictor (pun not intented) would guess ~99% of the time that your agent is going to continue to move. The key is to look at the most common case. If you have another scenario where there is a 50-50 chance the condition will be true or false, that's the worst.
     
    Last edited: Dec 7, 2018
  5. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    6,633
    Code (Vacummus, post: 3969868, member: 507369):
    1.             var nextPoint = followRandomPoints.nextPoint;
    2.             var currentPosition = position.value;
    3.             var distance = followRandomPoints.distance;
    4.  
    5.  
    6.             if (!Utilities.IsWithinDistance(nextPoint, currentPosition, distance))
    7.             {
    8.                 movementInput.Axis = Utilities.GetDirection(currentPosition, nextPoint);
    9.             }
    10.             else
    11.             {
    12.                 var anchorPoint = followRandomPoints.anchorPoint;
    13.                 var radius = followRandomPoints.radius;
    14.  
    15.  
    16.                 followRandomPoints.nextPoint = Utilities.RandomPoint(anchorPoint, radius);
    17.             }
    I would be more concerned about two things:
    1. The number of function calls (Utilities x 3) and .accesses (9) within the inner loop. Hopefully the Burst compiler will Inline the function calls and variables needed.
    2. Two seperate behaviours are built into one system, a move to destination system and a set random destination system. This creates the potential for branch prediction issues but more importantly limits the reusability of the systems.
     
  6. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    23,840
    My 2p...

    Well it's actually really hard to get performance loss like branch misprediction with ECS because you're packing data in a very sequential and tight way, so the CPU is already in the ideal position to be predicting better than the code we'd normally write to be cache friendly or prediction friendly (unless it's a tiny program).

    The reason I don't think this micro optimising works here is because the CPU has already loaded a bunch of data already and has already started it's prediction, and it becomes a system size problem.

    At best this would be a micro-optimisation this early in Unity ECS (I expect we can massage this later via hinting how large our systems are or such).

    I definitely would be deterministic with my random though. Perlin or similar.
     
  7. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    686
    And my 2p :) For non-bursted jobs it's very micro optimistion, but for Burst - not, because Burst compiles to native SIMD friendly code and branches breaks performance gains :)
    And Unity also recommends avoid branches ;)
    (https://github.com/Unity-Technologi...r/Documentation/content/burst_optimization.md)
    upload_2018-12-7_20-10-0.png

    Of course you still get performance with burst, but brunches don't give you maximum performance
     
    hippocoder likes this.
  8. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    23,840
    Very nice.... :)
    Same parallelism friendly wisdom as shaders.
     
  9. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    686
    Yes, because GPU use SIMD too :) But on GPU you have MAAAANY cores and threads, and on modern GPUs branches are not critical now, but on CPU you still have small count of cores\threads and here we can see some performance drops with branches :)
     
    hippocoder likes this.
  10. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    814
    Maybe try inline-if? Calculate distance simply without if, then movement and random point get the same value or new value based on inline if condition. But then you have to calculate the case for new value even if it is not needed, not sure if worth it or not.
     
  11. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    23,840
    Is dynamic/static branching even a thing at all with Burst? I mean I would expect something that will always resolve one way to get compiled out (for reusing systems under multiple contexts).
     
  12. EthanHunt

    EthanHunt

    Joined:
    Oct 9, 2012
    Posts:
    13
    This is going a but further down the micro optimisation lane, what can be done is split the system into 2, one for simple calculation of remaining distance, the other one is scheduled afterwards to check if the said distance fell below threshold. At least with that the first system can be burst optimized, and able to process 4 entities at a time. The branch misses due to the condition check is unavoidable, then again it is not a big deal if this check isn’t ran million times each frame.
     
    Last edited: Dec 8, 2018
    Vacummus likes this.
  13. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    61
    Why is it bad to make function calls from within the inner loop?

    Not familiar with Perlin. How would you be deterministic with the random? I am currently doing the following:

    Code (CSharp):
    1. var randomPoint = anchorPoint + UnityEngine.Random.insideUnitCircle * radius;
     
  14. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    23,840
    Vacummus likes this.
  15. tbriley

    tbriley

    Joined:
    Sep 10, 2013
    Posts:
    64
    I'd second @EthanHunt's suggestion and consider splitting this into two systems - one for just movement, and one that does the check and updates the target.
     
  16. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    6,633
    When optimising code you should inline functions in inner loops, this brings the code into one block for the cpu to work on remember code as well as data has to move from ram to cpu.

    Function calls also have an overhead as the CPU has to store the current state of the program on the heap and pass copied data to the function.

    Also function call libraries can be nested so one apparently simple function call can involve multiple sub-functions, variables, logic, branching and even external data.


    PS Regarding random you can find one line RND number functions that take advantage of floating point errors to generate random numbers although hopefully Burst will be optimised for and inline any mathematics. functions.
     
    Vacummus likes this.
  17. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    814
    You can use the Burst Inspector to see the assembly code. Burst also can inline functions, I don't think it worth to inline it yourself vs write more readable code and let Burst do it.

    For example properties are function, I once compare in the debugger and property and non-property version the LLVM Unoptimized tab gives different code but LLVM Optimized tab is exactly the same, so no fear of losing performance at all using simple properties. The final tab also lists whether it succeed or fail to do which optimization. If in doubt, you can press copy to clipboard button and put it in Git commit, then try to inline manually and copy again to see changes.

    Other interesting thing I see is that switch case with enums will often be optimized into jump table. If you have to do multiple ifs then to make it a switch case instead. It does not happen with number case.
     
    Last edited: Dec 8, 2018
  18. LurkingNinjaDev

    LurkingNinjaDev

    Joined:
    Jan 20, 2015
    Posts:
    1,940
    Only if you're using large data-structures, otherwise the context will be stored on the stack. It's not really better, because of the context switch. Sorry for the smartassin'. ;)
     
    Last edited: Dec 8, 2018
    Arowx likes this.
  19. EthanHunt

    EthanHunt

    Joined:
    Oct 9, 2012
    Posts:
    13
    The cost of function calls does not stop here. The CPU will also have to clear out the instruction cache, load new instructions which is very likely going to cause an L2 miss. That's a minimum 200 cycles wasted on an x86 CPU. After that it has to do a jump to the new instruction to continue execution. I wrote an interpreter before, even when I used assembly in an extremely tight loop, just jumping around (the jump distance was just a few instructions) without loading anything already can cause significant slow down. Burst compiler can inline, so it is important to check the generated assembly.
     
    LurkingNinjaDev likes this.
  20. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    814
    ps. I found the images I made for explanation months ago. If you use inline-if/Select you could be turning some j commands into cmove (conditional move). For switch case with integers it is a bunch of jumps but with enum it seems to do smarter job.

    Screenshot 2018-12-09 02.21.53.png Screenshot 2018-12-09 02.22.16.png
     
  21. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    6,633
    It would be nice if Unity provided ECS Burst hints or tips to help us optimise these inner loops and get the most out of ECS.
     
    FROS7 and Vacummus like this.
  22. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    61
    Would like to share an update on this. After experimenting with 2 different approaches (recommended here), I am going to be moving forward with my original solution as it has proven to be the most performant. The two other approaches I experimented with are:
    • Avoid branching by using math.select (per Unity's documentation that @eizenhorn shared above)
    • Splitting my system into two systems.
    I micro benchmarked the 3 approaches using Unity's profiler with 1 million entities. Here are the results for how long it took to process these entities per frame (this is an aggregated total from 8 parallel instances that were used on my machine):
    • Original Approach: 27ms
    • math.select approach: 40ms
    • Two system approach: 42ms
    I think the reason the original approach is faster is because 99% of the time the system does not need to process a random value. Where as for the math.select approach it always has to process a random value.
     
    5argon likes this.
  23. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    814
    Nice find, select/inline if should be used for selecting between simple value assignment I think.

    Did you use random which came with the Mathematics lib?
     
  24. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    6,633
    What are the timings on the two system approach the timings for each system?

    It seems odd as you would expect only a tiny fraction of the workload to be moving to the second new random waypoint system so I would expect the move towards to be faster than 27ms and the new waypoint to be way faster than 15ms?

    Could you share your code as there could be issues in how you tested or how you managed the data between the systems?
     
  25. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    686
    Vacummus likes this.
  26. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    61
    Yup, figured out how to use it thanks to help of the community.
     
  27. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    61
    Absolutely. Here is the updated version of the original approach. Also, if you uncomment the commented out code, and comment out the if block, then that would be the math.select approach:
    Code (CSharp):
    1. public struct FollowRandomPoints: IComponentData
    2. {
    3.     public float radius;
    4.     public float2 anchorPoint;
    5.     public float2 nextPoint;
    6.     public float distance;
    7. }
    8.  
    9. public struct MovementInput: IComponentData
    10. {
    11.     public float2 Axis;
    12. }
    13.  
    14. public class FollowRandomPointsSystem: JobComponentSystem
    15. {
    16.     [BurstCompile]
    17.     struct Job: IJobProcessComponentData<Position, FollowRandomPoints, MovementInput>
    18.     {
    19.         public Unity.Mathematics.Random random;
    20.  
    21.         public void Execute(
    22.             [Unity.Collections.ReadOnly] ref Position position,
    23.             ref FollowRandomPoints followRandomPoints,
    24.             [Unity.Collections.WriteOnly] ref MovementInput movementInput
    25.         ) {
    26.             var distanceFromPoint = math.distance(position.value, followRandomPoints.nextPoint);
    27.             var isNear = distanceFromPoint < followRandomPoints.distance;
    28.             // var currentPoint = followRandomPoints.nextPoint;
    29.             // var randomPoint = random.NextFloat2(-followRandomPoints.radius, followRandomPoints.radius);
    30.             // var nextPoint = followRandomPoints.anchorPoint + randomPoint;
    31.          
    32.             // followRandomPoints.nextPoint = math.select(currentPoint, nextPoint, isNear);
    33.  
    34.             if (isNear)
    35.             {
    36.                 var randomPoint = random.NextFloat2(-followRandomPoints.radius, followRandomPoints.radius);
    37.                 followRandomPoints.nextPoint = followRandomPoints.anchorPoint + randomPoint;
    38.             }
    39.  
    40.             movementInput.Axis = math.normalize(followRandomPoints.nextPoint - position.value);
    41.         }
    42.     }
    43.  
    44.     protected override JobHandle OnUpdate(JobHandle inputDeps)
    45.     {
    46.         var random = new Unity.Mathematics.Random((uint)UnityEngine.Random.Range(1, 10000));
    47.      
    48.         return new Job { random = random }.Schedule(this, inputDeps);
    49.     }
    50. }
    And for the two system approach, I introduced some new data "MoveTowardsPoint" and a system that handles functionality for that data, allowing the FollowRandomPointsSystem to only be concerned with generating a random point:
    Code (CSharp):
    1. public struct MoveTowardsPoint2: IComponentData
    2. {
    3.     public float2 point;
    4.     public float distanceFromPoint;
    5. }
    6.  
    7. public struct FollowRandomPoints2: IComponentData
    8. {
    9.     public float radius;
    10.     public float2 anchorPoint;
    11.     public float distance;
    12. }
    13.  
    14. public struct MovementInput2: IComponentData
    15. {
    16.     public float2 Axis;
    17. }
    18.  
    19. public class FollowRandomPointsSystem2: JobComponentSystem
    20. {
    21.     [BurstCompile]
    22.     struct Job: IJobProcessComponentData<MoveTowardsPoint2, FollowRandomPoints2>
    23.     {
    24.         public Unity.Mathematics.Random random;
    25.  
    26.         public void Execute(
    27.             ref MoveTowardsPoint2 moveTowardsPoint,
    28.             [Unity.Collections.ReadOnly] ref FollowRandomPoints2 followRandomPoints
    29.         ) {
    30.             var isNear = moveTowardsPoint.distanceFromPoint < followRandomPoints.distance;
    31.             var currentPoint = moveTowardsPoint.point;
    32.             var randomPoint = random.NextFloat2(-followRandomPoints.radius, followRandomPoints.radius);
    33.             var nextPoint = followRandomPoints.anchorPoint + randomPoint;
    34.          
    35.             moveTowardsPoint.point = math.select(currentPoint, nextPoint, isNear);
    36.         }
    37.     }
    38.  
    39.     protected override JobHandle OnUpdate(JobHandle inputDeps)
    40.     {
    41.         var random = new Unity.Mathematics.Random((uint)UnityEngine.Random.Range(1, 10000));
    42.  
    43.         return new Job { random = random }.Schedule(this, inputDeps);
    44.     }
    45. }
    46.  
    47. public class MoveTowardPointSystem2: JobComponentSystem
    48. {
    49.     [BurstCompile]
    50.     struct Job: IJobProcessComponentData<Position, MoveTowardsPoint2, MovementInput2>
    51.     {
    52.         public void Execute(
    53.             [Unity.Collections.ReadOnly] ref Position position,
    54.             ref MoveTowardsPoint2 moveTowardsPoint,
    55.             [Unity.Collections.WriteOnly] ref MovementInput2 movementInput
    56.         ) {
    57.             moveTowardsPoint.distanceFromPoint = math.distance(position.value, moveTowardsPoint.point);
    58.             movementInput.Axis = math.normalize(moveTowardsPoint.point - position.value);
    59.         }
    60.     }
    61.  
    62.     protected override JobHandle OnUpdate(JobHandle inputDeps)
    63.     {
    64.         return new Job { }.Schedule(this, inputDeps);
    65.     }
    66. }
    So from the two system approach code above, the timings of the two systems is approx the following:

    MoveTowardsPointSystem2: 25ms
    FollowRandomPointsSystem2: 18ms
     
    Last edited: Dec 15, 2018 at 7:10 PM
  28. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    61
    I should also point out that I tried using an if block instead of math.select for FollowRandomPointsSystem2 (two system approach), and didn't see any performance improvements this time around, it still ran at 18ms. ¯\_(ツ)_/¯
     
  29. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    6,633
    Code (CSharp):
    1. moveTowardsPoint.distanceFromPoint = math.distance(position.value, moveTowardsPoint.point);
    2.             movementInput.Axis = math.normalize(moveTowardsPoint.point - position.value);
    Why calculate the distance then the normal between these two points as once you have the distance you can divide the vector by this value to get the normal?

    It also looks like you are re-calculating this normalized movement vector every MoveTowards cycle, surely you could calculate this once when you generate the new random location?

    Code (CSharp):
    1. var random = new Unity.Mathematics.Random((uint)UnityEngine.Random.Range(1, 10000));
    It looks like your are re-seeding the random number function every update, I don't think you need to do this?

    I'm wondering what your radius, near values are as if they are low and the movement is via normalized vector then your simulation will just be churning as there will only be a few moves before a new way-point is triggered.

    Also shouldn't the movement system use some form of delta time to smooth the movement so the speed does not vary with frame rate?
     
    Last edited: Dec 16, 2018 at 3:36 AM
  30. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    61
    I didn't know about that. Good catch! That makes the system from the first approach about 2-3 ms faster.

    Yes you could if nothing else in the game is affecting it's position. But for my game, there is 2d physics that affects the position of these entities, and thus they will always need to keep correcting their course towards a point.

    Is it bad to re-seed every update? I am doing it because it's the only way I have found thus far to truly simulate random values within jobs. If I use the same seed every time, then every frame the same random values will be produced, which is not very random at all. This becomes super buggy too when all you have is one entity following random points because the same random value will always be generated for that one entity.

    Can you re-iterate this? I am not quite following you here.

    I haven't shared any code for my movement system, but yes it does use delta.time. I have a MovementSpeed component, and the movement system uses that along with the MovementInput component to determine how much force and in what direction it needs to apply to the 2d physics rigidbody.
     
  31. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    6,633
    Your system timings for the two systems approach appear way too similar, e.g. if on average your objects take 10-100 frames to move to their waypoints then you would expect about a 55:1 ratio between the movement and random waypoint systems. Therefore I suspect your waypoints are too close together, your movement speeds are too high or your proximity test is too wide. Or a combination of any of these are making your system thrash or churn with much higher random waypoint generation than expected.

    You could count the number of waypoints generated a frame to look into it, what percentage of waypoints are generated vs number of moving objects.

    Regarding the random number generation one idea is to pre-generate a large array of random values and just randomly index a job into them, this takes a little bit of setup time but massively reduces workload in game.
     
  32. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    61
    The waypoints are pretty far apart. It takes a couple of seconds for each entity to reach it's destination before a new way point is required. I am testing with 1 million entities. And with the data setup I have in place, on average about 3500 entities need a new random point to be generated per frame. That's a 0.35% hit ratio.
     
  33. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    6,633
    So moving nearly a million points a frame takes 25ms and setting a random waypoint for 3,500 entities takes 18ms, you definitely need a pregenerated random pool and a deeper looking profile of this side of things.

    There will be inter system overhead, where moving your data from one system to another and back again may be causing a slowdown.

    Code (CSharp):
    1. var isNear = moveTowardsPoint.distanceFromPoint < followRandomPoints.distance;
    2.             var currentPoint = moveTowardsPoint.point;
    3.             var randomPoint = random.NextFloat2(-followRandomPoints.radius, followRandomPoints.radius);
    4.             var nextPoint = followRandomPoints.anchorPoint + randomPoint;
    5.        
    6.             moveTowardsPoint.point = math.select(currentPoint, nextPoint, isNear);
    The only thing here that looks as though it might be a performance hog is the random.NextFloat2() function, try generating a large array of random values you can pass into this job system with a random index offset for each job and then just increment the index.

    Other than that you could maybe look at how you transfer the data between systems, is there a faster way to have two job systems work on the same data with only a flag to indicate which data goes to which system, without having to move the data between chunks/systems?
     
    Last edited: Dec 17, 2018 at 8:04 PM
  34. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    61
    To clarify, the two systems from the 3rd approach (FollowRandomPointsSystem and MoveTowardsPointSystem) both process exactly 1 million entities per frame. So the FollowRandomPointsSystem generates 1 million random points per frame, but only 3500 are actually used. This is because of math.select. I tried using an if statement instead (so it only generates 3500 random points or whatever is needed) but I did not see any performance improvements.

    I doubt that querying data for each system is causing the slow downs here. If you comment out all the code inside the Execute function of any system and you have that system just iterate over 1 million entities, that is still going to take a while (all depends on how much data you are loading). For the case of the FollowRandomPointsSystem, it takes about 7ms on my machine just to iterate over 1 million items.

    So considering that the MoveTowardsPointSystem takes 25ms to run, and at best it takes the FollowRandomPointsSystem 7ms run (if we have it execute no code), this is still 5 ms slower then the first approach...
     
    Last edited: Dec 18, 2018 at 10:14 AM