Search Unity

Caching of GetComponentGroup queries

Discussion in 'Entity Component System' started by themeshpotato, Apr 11, 2018.

  1. themeshpotato

    themeshpotato

    Joined:
    Apr 11, 2018
    Posts:
    55
    I might just have misunderstood the talks on this topic, but I was wondering, how the engine packs the arrays that are then used by the systems when using the job system.

    Let's say you have your struct of component data called RotatorData like in one of the demos and want to call
    Code (CSharp):
    1. var group = EntityManager.GetComponentGroup(typeof(RotatorData), typeof(SometOtherComponent));
    When the arrays that this group will contain are packed by the engine to enable linear access patterns when iterated over by the developer will this packing of data happen on every call to GetComponentGroup() or are the latest queries cached to enable faster queries if you call the same query every frame for example?

    As mentioned I might just have misunderstood the concept, and would love to know more about how this system really works.
     
  2. GabrieleUnity

    GabrieleUnity

    Unity Technologies

    Joined:
    Sep 4, 2012
    Posts:
    116
    Every
    ComponentSystem
    stores a cache of the
    ComponentGroups
    created via
    ComponentSystem.GetComponentGroup
    . You can access them via
    ComponentSystem.ComponentGroups
    if you are interested.

    A new
    ComponentGroup
    is created for a given set of
    ComponentType
    s for a specific
    ComponentSystem
    , upon the first request, and then it is cached. Subsequent calls to
    ComponentSystem.GetComponentGroup
    return the cache. We suggest caching the result of
    ComponentSystem.GetComponentGroup
    in
    OnCreateManager
    anyway, to make sure to avoid any overhead.

    Creating a new
    ComponentGroup
    allocates GC memory, and it is quite expensive. Using injection or
    IJobProcessComponentData
    is the suggested way to access
    ComponentGroup
    s, so the ECS system takes care of all the work for you unless you need something special like setting filters manually.

    Regarding data layout, we don't repack all the data every time you call
    GetComponentGroup
    . You can get here a bit more information regarding how the system works: https://github.com/Unity-Technologi.../ecs_in_detail.md#chunk-implementation-detail