Caveats for Native containers inside of components

I understand that I have to use cleanup systems if I’m going to use native containers inside of components, but what are the performance concerns here? This violates my (admittedly rudimentary) understanding of how components work. I thought part of the power of ECS came from the fact that components were a fixed size on the stack… doesn’t variable list containers like this change that?

Adding a native container field will just add the pointer to the underlying allocated unsafe container data. You’ll pay costs for indirection through that unsafe container header and another level of indirection to get the actual data. That necessarily has some effect on performance over using a fixed size container field (don’t need indirection for header) but when you’re sure you have the potential to exceed the capacity of the largest available fixed size container, you’ll either need to create a larger fixed size container type and severely decrease the number of entities which you can store in a chunk if the containers are used as components for general entities, or use a native container and effectively only need to keep a pointer in the component. Depending on what you’re doing (dynamic lookups or just iteration through a buffer) the performance hit of using a native collection should just range from negligible to slight, but the profiler would be the best way to know for sure (by comparing different kinds of storage).

1 Like

Excellent explanation, thank you.

Here is a great, short, read on NativeContainers in Components - Native container component support | Entities | 1.0.16

I am not quite sure how it does the memory under-the-surface. Components are typically copies of the same data. Maybe different values, but the same structure of the data types… NativeList and NativeHashMap are more indeterminate over time, so I see why you are wondering about the memory expansion…

I don’t have a great answer for this however. Maybe someone else can specify how NativeCollections on Components deal with structural changes upon changing the collection.