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 CollectionHelper CreativeNativeArray vs new

Discussion in 'Entity Component System' started by Ksanone, Sep 29, 2022.

  1. Ksanone

    Ksanone

    Joined:
    Feb 7, 2015
    Posts:
    39
    Is there any functional difference between

    Code (csharp):
    1.  
    2. NativeArray< EntityAndPositionData > workArrayVersion1 = CollectionHelper.CreateNativeArray< EntityAndPositionData , RewindableAllocator >(
    3.    _workEntityQuery.CalculateEntityCount() , ref World.UpdateAllocator ) ;
    4.  
    5. // alternative way to make a list ?benefits between the two ?
    6. NativeList< EntityAndPositionData > workArrayVersion2 = new NativeList< EntityAndPositionData >( _workEntityQuery.CalculateEntityCount() , Allocator.TempJob ) ;
    7.  
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,983
    There's quite a big difference. Different container types. Different allocators. Different lifetime guarantees. Different disposal responsibilities. You get the idea.
     
    Occuros likes this.
  3. Ksanone

    Ksanone

    Joined:
    Feb 7, 2015
    Posts:
    39
    Ah I meant
    Code (csharp):
    1.  
    2. NativeArray< EntityAndPositionData > workArrayVersion1 = CollectionHelper.CreateNativeArray< EntityAndPositionData , RewindableAllocator >(
    3.    _workEntityQuery.CalculateEntityCount() , ref World.UpdateAllocator ) ;
    4.  
    5. NativeArray< EntityAndPositionData > workArrayVersion2 = new NativeArray< EntityAndPositionData >( _workEntityQuery.CalculateEntityCount() , Allocator.Temp ) ;
    6.  
    but point taken about all the other differences, thanks.
     
  4. Luxxuor

    Luxxuor

    Joined:
    Jul 18, 2019
    Posts:
    89
    As far as I understood it the first variant allows you to provide a custom allocator (like World.UpdateAllocator) while the second variant only works with the usual allocator labels (at least for NativeArray).
     
    Ksanone likes this.
  5. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,983
    Pretty much. And you only really need CollectionHelper for NativeArray. Everything in the Collections package works with custom allocators via ToAllocator.
     
    Occuros and Luxxuor like this.