Search Unity

Is Object Pool a valid optimization pattern in ECS?

Discussion in 'Data Oriented Technology Stack' started by mnarimani, Aug 15, 2019.

  1. mnarimani

    mnarimani

    Joined:
    Mar 27, 2017
    Posts:
    186
    Object Pool is very common in games that are built with MonoBehaviours. Because instantiating game objects is costly.
    Do we need to create object pool in ECS? How costly is creating a new entity?
     
  2. supron

    supron

    Joined:
    Aug 24, 2013
    Posts:
    44
    I made some tests while I was writing an event system for my UI. I do not have profiler snapshots but I found out that creating entities and flagging with components (required to "mark" entities in a pool), are very similar in performance. Both operations require sync point and chunk operations (allocation/relocation). Creating entities in batch (EntityManager.CreateEntity(EntityArchetype, NativeArray<Entity>)) is even faster than pooling.
     
    mnarimani and Opeth001 like this.
  3. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    234
    ECS is awesomely fast at instantiating new Entities, you can use batch Operations for better performance.
    Generally Object Pooling in ECS is used when it's about hybred Entities which are Destroyed/Instantiated mulitple Times per Frame Like Bullets (Particale System).
    the Only Bad side about Destroy/Instantiate new entities is Sync Points but still Awesomely Fast.

    my Opinion !! (maybe wrong)
    in case you have Millions of pure entities that are Destroyed per frame, and other Millions of entities with same architype but their Components need to be reinitialized. you can modify their Components value By Chunk operations and not per Entity. eg by using CopyFromComponentDataArray<T>(NativeArray<T>, out JobHandle)
    logically modifying Components value on a lot of Entities is faster than Destrying them, creating new ones and modifying their Components value.
     
    mnarimani likes this.
  4. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    381
    Pooling is only useful for reference types which are allocated on the GC-managed heap. This includes class types and System.Collections.Generic stuff. Generally you should avoid pooling unless you need these things. Unity.Rendering.Hybrid uses a pool for lights so that you can have millions of virtual lights in your scene but only a thousand or so active in a frame.
     
  5. rizu

    rizu

    Joined:
    Oct 8, 2013
    Posts:
    1,083
     
    mnarimani likes this.
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    5,242
    I use pooling for certain types of entities. But my main reason is, they got references in/to other entities and the generation require execution of some specific algorithms. Hence better for me generate fewer per frame. So in my case, pooling makes sense and I don't remove these entities over time. But when you got entities with only few components, just instantiate them.
     
    Opeth001 likes this.