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:
struct Mydata { public NativeArray<int> data;}
struct MyContainer { NativeArray<MyData> data }
or event this
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 ?
I’ll give you a couple hints for nesting native containers.
Use unsafe containers (UnsafeArray, UnsafeList) to store the data.
Wrap the root container/node with a struct you define. Let’s call it NativeTreeRoot.
Refer to the source code of NativeArray for how memory safety and memory allocation checks are made.
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.
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!