The TransformAccessArray in Unity is a specialized structure used for efficiently managing and accessing multiple Transform components, particularly in the context of jobs. It is designed to work with the Job System to allow parallel processing of Transform operations while minimizing overhead.
I tested this in my pet project a long time ago, and the difference was very noticeable, almost eliminating the cost of moving a large number of transforms every frame. While the API is pretty clean, working with jobs still adds cognitive load, so my advice is to always profile your code. Only if you see that transforms are the bottleneck should you switch from simple transform manipulations to using TransformAccessArray.
Tips from the Article:
- Use Burst.
- When a job only needs to read transforms (i.e., not write), use
IJobParallelForTransform.ScheduleReadOnly.- Break deep transform hierarchies into multiple transform roots whenever possible. This tip was also mentioned at Unite 2018 and in my post summarizing all performance tips from Unite.
- Review the
desiredJobCount and innerloopBatchCount parameters. Aim for small enough values to keep all workers busy, but large enough to minimize the overhead of workers grabbing or stealing tasks too frequently.- Prefer local space transform data like
localPosition and GetLocalPositionAndRotation over world space data like position and localToWorldMatrix when possible.- Reuse TransformAccessArray instances between frames; avoid recreating them unnecessarily.
- To remove a transform from the array, use
TransformAccessArray.RemoveAtSwapBack instead of recreating the array. This is a general best practice for removing elements in hot paths.- Avoid using transforms in a TransformAccessArray to store intermediary data between jobs. Transform access in jobs is slower compared to other alternatives.
https://medium.com/toca-boca-tech-blog/unitys-transformaccessarray-internals-and-best-practices-2923546e0b41
#optimization #performance #transform