[DOTS] Tuning InternalBufferCapacity for Large Data Sets
Solution
Unity 2022.3.x - Unity 6.3.x
Published 28 days ago
Attempts to store massive quantities of InternalBufferCapacity data within Entity chunks often result in ArchetypeChunk overflow and performance bottlenecks due to the fixed 16KB size constraint of Unity ECS memory blocks.
Entity chunks within Unity ECS are constrained to a 16KB memory limit. When the InternalBufferCapacity attribute is defined with a value greater than zero, elements of the DynamicBuffer are embedded directly within the ArchetypeChunk to improve cache locality.
If the total size of these elements exceeds the chunk capacity, it causes immediate overflow and forces the framework to allocate memory externally anyway, while wasting internal chunk space that could be used for other entities. Setting the capacity to zero ensures that the Entity footprint remains small and that all buffer data is handled by the heap.
- Define your buffer element struct implementing the
IBufferElementDatainterface. - Decorate the struct with the InternalBufferCapacity attribute, passing 0 as the argument.
- Access your buffer via
EntityManageror within aSystemBaseto verify heap-based allocation is active.
Additional Tips:
- Setting InternalBufferCapacity to 0 is optimal when the number of elements is unknown or exceeds several hundred units.
- This configuration prevents the “Internal Buffer” from consuming the 16KB chunk budget, allowing more entities per chunk.
- Use
BufferAccessorwithin anIJobEntityto process these external buffers efficiently across multiple entities.
TL;DR
To efficiently manage massive DynamicBuffer collections in Unity ECS, setting the InternalBufferCapacity to 0 forces external heap allocation, preventing ArchetypeChunk fragmentation.
Related Posts Haven't quite found a solution to your problem? We think these posts might help you.
Content inspired by a Unity discussion post.