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:
public void SetReturnValue() {
//Debug.Log("Setting Mesh Return Value");
meshData.verticesPerLine = returnVerticesPerLine[0];
meshData.float3Vertices = returnFloat3Vertices.ToArray();
meshData.int3Triangles = returnInt3Triangles.ToArray();
meshData.float2UVs = returnFloat2UVs.ToArray();
meshData.vertices = returnVertices.ToArray();
meshData.triangles = returnTriangles.ToArray();
meshData.uvs = returnUVs.ToArray();
meshData.flipMap = returnFlipMap.ToArray();
meshData.vertexHeightMap = returnVertexHeightMap.ToArray();
meshData.vertexIndexMap = returnVertexIndexMap.ToArray();
meshData.CreateMesh();
returnVerticesPerLine.Dispose();
returnFlipMap.Dispose();
returnVertexHeightMap.Dispose();
returnVertexIndexMap.Dispose();
returnFloat3Vertices.Dispose();
returnInt3Triangles.Dispose();
returnFloat2UVs.Dispose();
returnVertices.Dispose();
returnTriangles.Dispose();
returnUVs.Dispose();
nativeHeightCurve.Dispose();
nativeHeightMap.Dispose();
returnComplete.Dispose();
returnValue = meshData;
complete = true;
}
Thanks!
Are you sure NativeArray is too slow outside of job? Have you tried it in a build where the safety checks are off?
1 Like
Timboc
September 2, 2021, 6:41pm
3
slushieboy99:
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:
public void SetReturnValue() {
//Debug.Log("Setting Mesh Return Value");
meshData.verticesPerLine = returnVerticesPerLine[0];
meshData.float3Vertices = returnFloat3Vertices.ToArray();
meshData.int3Triangles = returnInt3Triangles.ToArray();
meshData.float2UVs = returnFloat2UVs.ToArray();
meshData.vertices = returnVertices.ToArray();
meshData.triangles = returnTriangles.ToArray();
meshData.uvs = returnUVs.ToArray();
meshData.flipMap = returnFlipMap.ToArray();
meshData.vertexHeightMap = returnVertexHeightMap.ToArray();
meshData.vertexIndexMap = returnVertexIndexMap.ToArray();
meshData.CreateMesh();
returnVerticesPerLine.Dispose();
returnFlipMap.Dispose();
returnVertexHeightMap.Dispose();
returnVertexIndexMap.Dispose();
returnFloat3Vertices.Dispose();
returnInt3Triangles.Dispose();
returnFloat2UVs.Dispose();
returnVertices.Dispose();
returnTriangles.Dispose();
returnUVs.Dispose();
nativeHeightCurve.Dispose();
nativeHeightMap.Dispose();
returnComplete.Dispose();
returnValue = meshData;
complete = true;
}
Thanks!
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.
3 Likes
Hey guys, thanks for the answers! Super helpful!
1 Like