Search Unity

string.memcopy tanks RaycastCommand performance

Discussion in 'Data Oriented Technology Stack' started by Farox, Mar 25, 2019.

  1. Farox


    Oct 12, 2014
    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):
    2. var fromPos = Origin.transform.position;
    4.         var results = new NativeArray<RaycastHit>(_directions.Length, Allocator.Temp);
    5.         var commands = new NativeArray<RaycastCommand>(_directions.Length, Allocator.Temp);
    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.         }
    16.         handle = RaycastCommand.ScheduleBatch(commands, results, 1, default(JobHandle));
    17.         handle.Complete();
    19.             _results = results.ToArray();
    21.         // Dispose the buffers
    22.         results.Dispose();
    23.         commands.Dispose();