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 ?
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.
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.
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.
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.