Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Lots of small hierarchies - multiple BufferArrays or 'child' entities?

Discussion in 'Entity Component System' started by jorrit-de-vries, Sep 3, 2018.

  1. jorrit-de-vries

    jorrit-de-vries

    Joined:
    Aug 7, 2009
    Posts:
    71
    For a game I am working on I have developed a level solver, so we can check if levels designed are solvable, and in how many steps a solution can be achieved. I would love to port this solver to ECS, making use of parallelization and better/faster memory layout and Burst, as solving can take quite some time.

    A level is visually structured as if it was a rubik's cube, so in my OOP design I have a cube, which has cubelets, which have sides. The cubelets step in this hierarchy can be removed, so the data for a cube consists of a list cubelet-sides (position (3 floats), rotation (4 floats), some data (at the moment 2x2 ints)).

    The number of sides per cube is not known beforehand, minimum is 2, max is let's say 36 to 48, depending on the level designs we come up with. The number of cubes in a world can be 500k+, so the total number of sides will be in the millions.

    I have the choice to use either an entity for each side, and retrieve side relevant data through multiple ComponentDataFromEntity (for positions, rotations, and one or two for side relevant data) calls, and have a Cube component holding a list with entities pointing to this side data. Or I can use BufferArrays per cube entity (an array for positions, one for rotations, and one or two for side relevant data).

    When using the ComponentDataFromEntity route I understand the described setup will result in a lot of cache misses, for the BufferArray route I am not sure what the result will be. Is the usage of BufferArray here a valid choice, or is there an argument (or more) against such setup to make before I start implementing?
     
  2. M_R

    M_R

    Joined:
    Apr 15, 2015
    Posts:
    559
    (tldr - do both and profile)

    when you process each side, do you need data from other sides of the same cube? if not, you can have an entity per side (with a Cube component holding their cube ID, not the other way around).

    if instead the "cube" is the main "computational cluster", try the bufferArray route.

    the best data layout depends on how you need to access them.
    try to write a job that doesn't require any CDFE if possible, or at least minimize them.
     
  3. jorrit-de-vries

    jorrit-de-vries

    Joined:
    Aug 7, 2009
    Posts:
    71
    Yeah, sure do both and profile, that's best. But maybe one or the other is bad in the first place, of which I am not aware. Not everything is clear to me in how things get compiled, etc. and knowing some things beforehand can save quite some time.

    I need all sides, as I do pathfinding on the sides and/or transform the sides based on the cubes plane (think Rubik's cube) that is being rotated.
     
  4. M_R

    M_R

    Joined:
    Apr 15, 2015
    Posts:
    559
    then you'd start with "cube" entities with bufferArrays for the sides.

    buffers are allocated in the chunk for their default size, then if you use more they go on the heap. you'll want to set a suitable default so the majority of them are inside the chunks.

    you may also consider to have different buffer types, one for small cubes and one for large cubes, if your cubes distribution is not optimal (e.g. you have 300k 2x2 cubes and 200k 48x48 cubes)
     
  5. jorrit-de-vries

    jorrit-de-vries

    Joined:
    Aug 7, 2009
    Posts:
    71
    Different buffer types as in multiple implementations of IBufferElementData, each with a specific InternalBufferCapacity value set to approx. the amount of sides used? Would make the implementation of the systems a little larger (more component groups to be defined), but I keep the idea in mind. The Reinterpret cast functionality on the buffers can be pretty helpful in such case.
     
  6. M_R

    M_R

    Joined:
    Apr 15, 2015
    Posts:
    559
    yes. unless there is an API to override the attribute when creating the ComponentType for the archetype, which may or may not be (you can search the sources for that)