Search Unity

Reallocating/Resizing NativeArray

Discussion in 'Data Oriented Technology Stack' started by Rs, Feb 6, 2019.

  1. Rs

    Rs

    Joined:
    Aug 14, 2012
    Posts:
    39
    I'm using, Unity 2018.3.3f1's Job System and RaycastCommand. I basically modified the example that can be found in the RaycastCommand documentation. In my script NativeArrays must change size when needed.

    Some context:

    Initialize
    Code (CSharp):
    1. var hitResults = new NativeArray<RaycastHit>(targets.Length * sources.Length, Allocator.TempJob);
    2.  
    3. var commands = new NativeArray<RaycastCommand>(targets.Length * sources.Length, Allocator.TempJob);
    Create Commands
    Code (CSharp):
    1. commands[j * targets.Length + i] = new RaycastCommand(
    2.                         sources[i].position,
    3.                         Vector3.Normalize(targets[j].position - (float3)sources[i].position),
    4.                         Mathf.Infinity
    5.                         );
    Do Schedule Batch
    Code (CSharp):
    1. // Schedule the batch of raycasts
    2.             jobHandle = RaycastCommand.ScheduleBatch(commands, hitResults, raycastingOptions.m_minCommandsPerJob, default(JobHandle));
    3.  
    4.             // Wait for the batch processing job to complete
    5.             jobHandle.Complete();
    And after processing results...

    Deallocate

    Code (CSharp):
    1.             hitResults.Dispose();
    2.             commands.Dispose();
    I know this full sequence should not be done in every Update (and I am changing that) but, for the sake of testing, if I do so, it works but Unity, in an inconsistent way, starts spitting these errors:

    Code (CSharp):
    1. : Index 2800 is out of range of '2800' Length.
    2. Unity.Collections.NativeArray`1[T].FailOutOfRangeError (System.Int32 index) (at C:/buildslave/unity/build/Runtime/Export/NativeArray/NativeArray.cs:207)
    3. Unity.Collections.NativeArray`1[T].CheckElementWriteAccess (System.Int32 index) (at C:/buildslave/unity/build/Runtime/Export/NativeArray/NativeArray.cs:126)
    4. Unity.Collections.NativeArray`1[T].set_Item (System.Int32 index, T value) (at C:/buildslave/unity/build/Runtime/Export/NativeArray/NativeArray.cs:145)
    5. GIA.Raycasting.RaycastingEngine.RaycastCompute () (at Assets/RaycastBatchingResearch/Overshadowing/Scripts/RaycastingEngine.cs:116)
    6. GIA.Raycasting.RaycastingEngine.Update () (at Assets/RaycastBatchingResearch/Overshadowing/Scripts/RaycastingEngine.cs:62)
    7.  
    Code (CSharp):
    1. Internal: JobTempAlloc has allocations that are more than 4 frames old - this is not allowed and likely a leak
    Code (CSharp):
    1. To Debug, enable the define: TLA_DEBUG_STACK_LEAK in ThreadsafeLinearAllocator.cpp. This will output the callstacks of the leaked allocations
    Where 2800 is the total new size of the commands NativeArray.
    This error seems less likely if I change size and reallocate sporadically.
    The weird thing is that if you take the example in the RaycastCommand docs and execute it every update it works and it resizes the arrays correctly. It seems to depend on how much stuff is done in the middle, I guess the jobs go out of sync with the Unity cycle?

    I do not understand exactly what's going wrong. Is there a better way to resize NativeArrays (other than reinitialize them with "new NativeArray")?
    How do I stop this from happening and let the arrays change whenever I please (even in every update perhaps)?

    Thanks in advance.
     
    Last edited: Feb 6, 2019
  2. Rs

    Rs

    Joined:
    Aug 14, 2012
    Posts:
    39
    Last edited: Feb 6, 2019
  3. RecursiveEclipse

    RecursiveEclipse

    Joined:
    Sep 6, 2018
    Posts:
    103
    Are you sure your indexing math is correct? I don't think TempJob allocation would be anything significant compared to the raycasts, but you might be able to use NativeList.AsArray if you find it preforms better.

    These last 2 are unrelated and are Unity bugs, AFAIK there isn't a complete fix for it yet, I'm on latest preview and 2019 beta and still get it from time to time.
     
  4. thelebaron

    thelebaron

    Joined:
    Jun 2, 2013
    Posts:
    326
    I think you are getting the alloc error because it doesnt properly process the indices of your arrays, at least this all feels very similar to what I went through doing something similar with my enemy raycast detection system. If you comment out the create commands section, does it still give tempalloc errors?
    As a sidenote I wish I had known about NativeList.AsArray earlier!