Search Unity

Question NativeArray.ToArray() results in huge amount GC Allocation

Discussion in 'Entity Component System' started by slushieboy99, Sep 2, 2021.

  1. slushieboy99

    slushieboy99

    Joined:
    Aug 29, 2014
    Posts:
    88
    Hello,
    I'm trying to pass many arrays in and out of a job, I know that indexing a NativeArray outside of a job is very slow, so I tried using NativeArray.ToArray() to make these arrays better accessible. The problem is, this creates nearly 6mb of Garbage Collection allocation for this particular job, which seems to be disposed of in the same frame and is very slow. I'm very new to dots, so any tips on how to better pass arrays in and out of jobs would super helpful! Below is my code:


    Code (CSharp):
    1. public void SetReturnValue() {
    2.         //Debug.Log("Setting Mesh Return Value");
    3.         meshData.verticesPerLine = returnVerticesPerLine[0];
    4.  
    5.         meshData.float3Vertices = returnFloat3Vertices.ToArray();
    6.         meshData.int3Triangles = returnInt3Triangles.ToArray();
    7.         meshData.float2UVs = returnFloat2UVs.ToArray();
    8.         meshData.vertices = returnVertices.ToArray();
    9.         meshData.triangles = returnTriangles.ToArray();
    10.         meshData.uvs = returnUVs.ToArray();
    11.         meshData.flipMap = returnFlipMap.ToArray();
    12.         meshData.vertexHeightMap = returnVertexHeightMap.ToArray();
    13.         meshData.vertexIndexMap = returnVertexIndexMap.ToArray();
    14.  
    15.         meshData.CreateMesh();
    16.  
    17.         returnVerticesPerLine.Dispose();
    18.         returnFlipMap.Dispose();
    19.         returnVertexHeightMap.Dispose();
    20.         returnVertexIndexMap.Dispose();
    21.         returnFloat3Vertices.Dispose();
    22.         returnInt3Triangles.Dispose();
    23.         returnFloat2UVs.Dispose();
    24.         returnVertices.Dispose();
    25.         returnTriangles.Dispose();
    26.         returnUVs.Dispose();
    27.         nativeHeightCurve.Dispose();
    28.         nativeHeightMap.Dispose();
    29.         returnComplete.Dispose();
    30.  
    31.         returnValue = meshData;
    32.         complete = true;
    33.     }
    Thanks!
     
  2. CookieStealer2

    CookieStealer2

    Joined:
    Jun 25, 2018
    Posts:
    119
    Are you sure NativeArray is too slow outside of job? Have you tried it in a build where the safety checks are off?
     
    slushieboy99 likes this.
  3. Timboc

    Timboc

    Joined:
    Jun 22, 2015
    Posts:
    238
    If you're on (I think) >= Unity 2020.1 you can use mesh methods that take NativeArrays then there's no need to convert them to managed. See e.g. https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Mesh.AllocateWritableMeshData.html
    https://docs.unity3d.com/ScriptReference/Mesh.SetVertexBufferData.html
    If you do need to convert them to managed for some reason (e.g. older Unity version or an unsupported feature I'm not aware of) and it's a frequent operation, it may be worth resizing an existing managed array and filling each entry, rather than always creating new arrays. Especially if sizes between invocations are similar.
     
  4. slushieboy99

    slushieboy99

    Joined:
    Aug 29, 2014
    Posts:
    88
    Hey guys, thanks for the answers! Super helpful!
     
    Timboc likes this.