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

Question NativeArray Allocator good practices

Discussion in 'C# Job System' started by petera1980, Apr 21, 2023.

  1. petera1980

    petera1980

    Joined:
    Jan 29, 2016
    Posts:
    55
    Hello,

    I have a question about how to properly assign Allocator type in NativeArray.

    I noticed that in Unity examples in Update is used Allocator.TempJob then Disposed every time after Complete.

    If I have job that executed in update, maybe it is better to create global NativeArray with Allocator.Persistent than create every time new native array with Allocator.TempJob ?

    Thank you
     
  2. vectorized-runner

    vectorized-runner

    Joined:
    Jan 22, 2018
    Posts:
    396
    I don't think it would matter all that much. In detail TempJob should be better as you release the memory as soon as the job is done (if all systems keep persistent memory this could go up), also TempJob should be really fast to allocate. It could be better for cpu cache usage if the next system uses the previously released memory as well.
     
  3. petera1980

    petera1980

    Joined:
    Jan 29, 2016
    Posts:
    55
    vectorized-runner Thank you for your replay.

    With Allocator.Persistent, I can avoid the overhead of allocating and freeing memory on every frame.

    Also the data in NativeArrays will be changed on every frame so I think cache will not help so much.

    In other hand on all unity docs's examples TempJob is used as Allocator memory type in Update.

    My concern is why I need to allocate memory even very fast when I can allocate it once in Start for example ?
     
  4. vectorized-runner

    vectorized-runner

    Joined:
    Jan 22, 2018
    Posts:
    396
    It's best practice to release memory as soon as you're done. If each of your systems allocated 100mb of memory you couldn't use Persistent memory but you could use TempJob. In practice it might not matter.
     
  5. petera1980

    petera1980

    Joined:
    Jan 29, 2016
    Posts:
    55
    In my case I have only four NativeArray<float4> size of 3 for iJob struct
     
  6. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    510
    In that case, you might want to consider using something like FixedList64Bytes<T> and avoid using a memory allocator at all.
     
  7. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,542
    How much overhead did you measure? ;)

    This question is moot for as long as you don‘t implement both versions, profile them, and see which is faster in your particular scenario assuming speed is your main concern here and let‘s not forget: if it‘s faster on your target platform (ie Windows with today’s AMD Ryzen CPU) this may not even hold true and may even be the opposite for another platform (ie Mac with older Intel CPU). Lastly, when you are nearly finished it may turn out that the project is not even CPU but GPU bound.

    Unless you have a general performance issue in your app and this particular code is contributing significantly to the performance issue, you are best advised to continue working on features and leave such optimization - if any - for a later time when all systems are in place and you can easily optimize 2 out of ten systems that contribute the most to runtime performance.
     
  8. petera1980

    petera1980

    Joined:
    Jan 29, 2016
    Posts:
    55
    Spy-Master
    FixedList64Bytes is good approach but in case of jobs system it has to copy whole array in job struct before execution. In the other hand NativeArrays pass only the pointer to job struct.

    Thank you for your answers