Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Disposing of NativeArray<ArchetypeChunk> with IJobParallelFor

Discussion in 'Entity Component System' started by MartijnGG, Aug 31, 2018.

  1. MartijnGG

    MartijnGG

    Joined:
    May 3, 2018
    Posts:
    74
    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:
    5,203
    [DeallocateOnJobCompletion]
     
    deus0 likes this.
  3. MartijnGG

    MartijnGG

    Joined:
    May 3, 2018
    Posts:
    74
    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,394
    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:
    2,683
    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 :)