Hi,
In Entities Graphics version 1.2.0-pre.12, RegisterMaterialsAndMeshesSystem.RegisterMaterialsAndMeshes() allocates garbage every frame (2.4 KB per frame in our project, even when the game is idle).
The allocations occur on the following four lines:
if (array.Materials == null || array.Meshes == null) // EntitiesGraphicsSystem.cs:441
// ...
if (renderArray.Materials == null || renderArray.Meshes == null) // EntitiesGraphicsSystem.cs:491
// ...
var materialCount = renderArray.Materials.Length; // EntitiesGraphicsSystem.cs:499
var meshCount = renderArray.Meshes.Length; // EntitiesGraphicsSystem.cs:500```
More specifically, it's the property getters for `RenderMeshArray.Meshes` and `RenderMeshArray.Materials` which allocate copies of internal arrays:
```cs
public Material[] Materials
{
get
{
if (m_Materials == null)
return null;
var materialsArray = new Material[m_Materials.Length]; // <-- the allocation
for (int i = 0; i < m_Materials.Length; i++)
{
materialsArray[i] = m_Materials[i].Value;
}
return materialsArray;
}
// ...
Note that none of the usages actually needs the content of the copied array - they merely check the array length and whether it's null, both of which could be checked on the internal arrays directly without copying them.
Proposed solution
Instead of accessing the public Materials and Meshes getters, use the internal MaterialsInternal and MeshesInternal getters (both of which are accessible here). So the lines would be modified as follows:
if (array.MaterialsInternal == null || array.MeshesInternal == null) // EntitiesGraphicsSystem.cs:441
// ...
if (renderArray.MaterialsInternal == null || renderArray.MeshesInternal == null) // EntitiesGraphicsSystem.cs:491
// ...
var materialCount = renderArray.MaterialsInternal.Length; // EntitiesGraphicsSystem.cs:499
var meshCount = renderArray.MeshesInternal.Length; // EntitiesGraphicsSystem.cs:500```