Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get array from list without heap allocation

I have a list and I want to assign its array to a property.

public void BuildMesh(List<Vector3> list){
    mesh.vertices=list.ToArray();
}

Now the problems:

  • The project is game and is very tough on garbage collection so the default implementation of ToArray() is not an option as it creates a new array beside list's internal array.
  • The mesh object is from a closed source API and the vertices property is a Vector3[] so can't assign a pointer to it.

Do I have any option to prevent heap allocation?

EDIT: This is not a duplicate

Can't use IList<Vector3>. The mesh is from a closed source API and needs Vector3[] so I can't assign IList<Vector3> to it.

like image 669
morteza khosravi Avatar asked Dec 24 '15 06:12

morteza khosravi


Video Answer


1 Answers

I think you're using Unity. If I'm right, try mesh.SetVertices(list) which accept a List<Vector3>.

like image 123
Ran QUAN Avatar answered Sep 22 '22 00:09

Ran QUAN