Search Unity

string.memcopy tanks RaycastCommand performance

Discussion in 'Entity Component System' started by TJaenichen, Mar 25, 2019.

  1. TJaenichen

    TJaenichen

    Joined:
    Oct 12, 2014
    Posts:
    29
    I only found ancient threads pertaining to memcpy, but not the RaycastCommand.

    I need to shoot a whole lot of rays. After adder rays at some point the performance tanks. When checking with the profiler I see that most of the performance is being lost by trying to get my results out of the NativeArray with the hits.

    When doing so for some reason it does a call to string.memcpy for each element. Regardless if I try to use ToArray or copy the elements via indexer or only copy the property that I need.

    Any idea what I could do here? Basically I need the point of collision. (Or maybe only a "has hit something/didn't hit anything")

    Interestingly, it behaves roughly the same when running as a build. So there is no boost from that.

    Code (CSharp):
    1.  
    2. var fromPos = Origin.transform.position;
    3.        
    4.         var results = new NativeArray<RaycastHit>(_directions.Length, Allocator.Temp);
    5.         var commands = new NativeArray<RaycastCommand>(_directions.Length, Allocator.Temp);
    6.  
    7.         if (_commands == null || _forceCommand)
    8.         {
    9.             for (var index = 0; index < _directions.Length; index++)
    10.             {
    11.                 var vector3 = _directions[index];
    12.                 commands[index] = new RaycastCommand(fromPos, vector3, 10000, -5, 1);
    13.             }
    14.         }
    15.  
    16.         handle = RaycastCommand.ScheduleBatch(commands, results, 1, default(JobHandle));
    17.         handle.Complete();
    18.  
    19.             _results = results.ToArray();
    20.  
    21.         // Dispose the buffers
    22.         results.Dispose();
    23.         commands.Dispose();