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

Nested Tree like structure Native Containers.

Discussion in 'Burst' started by morphex, Feb 18, 2022.

  1. morphex

    morphex

    Joined:
    Dec 18, 2012
    Posts:
    108
    I am playing around with burst compiler and I found myself in need of a dynamic NESTED data structure.
    I cant seem to find any references on what happens if you pass a NativeContainer inside a struct for example:

    Code (CSharp):
    1. struct Mydata { public NativeArray<int> data;}
    2. struct MyContainer { NativeArray<MyData> data }
    or event this
    Code (CSharp):
    1. NativeArray<NativeArray<int>> d;

    Is this the right aproach? My gut feeling is telling me that NativeArray will just be copied over ?
    If not, how would one implement something such as a Octree, or some any other tree like structure?

    A bit more context, I am trying to have a Permanent structure that is mutable and will never get disposed.
    The idea is to be able to pass this through to jobs for some processing.

    Considerations

    * The MAX depth of the tree is set as CONST
    * There can be infinite*** number of children per node
    * Possibility of parallelFor on Nodes
    * Tree is essencially boundless, so I dont think I can encode it in a flat datastructure and index the values like that.
    *The tree is potentially huge, so Array.Copy is not a solution since I would need to loop through all the values and traverse the full tree back and forward between jobs.


    Any sugestion on how to achieve something like this ?
     
  2. jasonboukheir

    jasonboukheir

    Joined:
    May 3, 2017
    Posts:
    80
    I'll give you a couple hints for nesting native containers.

    1. Use unsafe containers (UnsafeArray, UnsafeList) to store the data.
    2. Wrap the root container/node with a struct you define. Let's call it NativeTreeRoot.
    3. Refer to the source code of NativeArray for how memory safety and memory allocation checks are made.
    4. Implement the same memory safety checks in your NativeTreeRoot and make sure it manages the memory of all of your nested containers.

    Native containers are not allowed to contain native containers because of the memory safety bits. All you really need is the memory safety checks in the top-most node managing all of the memory in your struct.
     
  3. Carpet_Head

    Carpet_Head

    Joined:
    Nov 27, 2014
    Posts:
    254
    Does UnsafeArray exist? I don't seem to have it
     
  4. jasonboukheir

    jasonboukheir

    Joined:
    May 3, 2017
    Posts:
    80
    Sorry my bad, there is no UnsafeArray.

    NativeArray just uses a pointer and memory allocation.
     
  5. Baggers_

    Baggers_

    Joined:
    Sep 10, 2017
    Posts:
    97
    One step I'll add to @jasonboukheir's great answer is: Test the everloving hell out of it.

    As a minimum: Find or write a simple managed (non-native) equivalent of the collection that you are making, this is the specification for your native collection. Write tests that generate random quantities of random changes and apply them to your collection and the managed one. At every step compare both collections to each other and fail if there are any deviations.

    Writing native collections is an absolute minefield, one mistake and you will end up with subtle issues and editor crashes that can takes ages to track down. There is no kind of data-structure too simple for me to make a mistake while coding, heed my stupidity!