Search Unity

Disposing of NativeArray<ArchetypeChunk> with IJobParallelFor

Discussion in 'Data Oriented Technology Stack' started by MartijnGG, Aug 31, 2018.

  1. MartijnGG

    MartijnGG

    Joined:
    May 3, 2018
    Posts:
    57
    I've been trying out the ArchetypeChunks and EntityArchetypeQuery to try and optimise some work by splitting the work per chunk of entities, instead of per X entities.

    Currently the psuedo code looks as following:

    Code (CSharp):
    1. var array = EntityManager.CreateArchetypeChunkArray(scoreQuery, Allocator.TempJob);
    2. var scoreType = GetArchetypeChunkComponentType<Score>();
    3.  
    4. var jobHandle = new ScoringJob
    5. {
    6.     Chunks = array,
    7.     ScoreType = scoreType,
    8. }.Schedule(array.Length, 1, inputDeps);
    9.  
    10. return jobHandle;
    This caused some issues with the allocated NativeArray, as it needs to be disposed of when this job has completed.
    How would I best Dispose of this, without calling jobHandle.Complete() followed by array.Dispose() of the chunks?
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    4,671
    [DeallocateOnJobCompletion]
     
  3. MartijnGG

    MartijnGG

    Joined:
    May 3, 2018
    Posts:
    57
    Using this attribute seems to cause it to deallocate once one of the parallel scheduled jobs is completed, instead of after the entire batch is done.

    Update: I'm not sure why, but when I try to reproduce the error now again to get the log for this topic.. it is no longer happening.
    I'm very sorry about that!
     
    Last edited: Aug 31, 2018
  4. LennartJohansen

    LennartJohansen

    Joined:
    Dec 1, 2014
    Posts:
    2,289
    Will this be possible to do on NativeLists also? I tried and got a message that it was not supported.

    Lennart
     
  5. iam2bam

    iam2bam

    Joined:
    Mar 9, 2016
    Posts:
    36
    Had a hard time finding this... Maybe because the word Dispose is written everywhere else instead of deallocation.
     
  6. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    1,422
    No, NativeList, NativeQueue, Native(Multi)HashMap hasn't [NativeContainerSupportsDeallocateOnJobCompletion] attribute, and thus not support [DeallocateOnJobCompletion] by design reasons, I think they not will be support this in future, but maybe I'm wrong and @Joachim_Ante says that :)