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

Question How to Use UnityEngine.Pool with NativeList

Discussion in 'Entity Component System' started by jGate99, Jul 1, 2021.

  1. jGate99

    jGate99

    Joined:
    Oct 22, 2013
    Posts:
    1,844
  2. julian-moschuering

    julian-moschuering

    Joined:
    Apr 15, 2014
    Posts:
    529
    You don't need to pool NativeLists. There is no garbage involved in NativeList instantiation (when not having safety checks active).
     
    jGate99 likes this.
  3. TheSmokingGnu

    TheSmokingGnu

    Joined:
    May 1, 2017
    Posts:
    22
    Afaik the generic parameter of all the new Pools is constrained to
    class
    ,
    all native collections are structs, so no, its impossible.

    Also, @julian-moschuering NativeLists do allocate of course since they own the memory, which means that it is always allocated on NativeList creation and freed on its disposal. Even if it's internal unity allocator, it's not free, so pooling may be as effective as with objects. The NativeList "handle" (object itself) is a struct, created and copied basically for free.
     
    jGate99 likes this.
  4. jGate99

    jGate99

    Joined:
    Oct 22, 2013
    Posts:
    1,844
    Thanks for your answer
    Currently, I'm using a custom created pool but with Unity's own Pools in 2021, I thought of using them if there is possibility
     
  5. julian-moschuering

    julian-moschuering

    Joined:
    Apr 15, 2014
    Posts:
    529
    In editor a persistent allocation is around 5x faster than a managed allocation, Temp around 30x faster and pooles are normally used in places where Temp/TempJob are usable. Additionally managed allocations have a much higher overhead due to them being handled by the GC.
    With safety checks enabled each native container has at least one managed allocation making them slow but that doesn't effect standalone performance.

    So as long as you don't use persistent for temporary allocations I don't think pooling is required.
     
    TheSmokingGnu likes this.