Search Unity

Question Speed up NativeList allocation/initiation

Discussion in 'Burst' started by DevDunk, May 28, 2023.

  1. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,060
    I am working on a system where I have to make arrays of 60k items, one for vertex position, normal, and UV.
    This takes up +- 2ms, which is too much for my current setup. Is there a way to improve this performance?
    Or is there maybe a way to get data from a mesh into a combined array (float3x2 for example for position and normal combined)?
     
  2. Saniell

    Saniell

    Joined:
    Oct 24, 2015
    Posts:
    194
    GetVertexData should do what you want, but its going to depend on how mesh is structured.
    Also, do you allocate lists every frame? Is there a way to not do that maybe? Do you set initial capacity to reduce amount of reallocations of the list?
     
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,988
    You only need one array normally because you can make a struct containing position, normal, UV and whatever you need. See link by Saniell and move up one level to Mesh.MeshData and explore that a bit.

    You could also alloc that array with persistent allocator and keep it around if you can afford to block this much memory (probably 1-2 megabytes).
     
  4. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,060
    Thanks! This is helping a lot with the performance.
    Looking more into the MeshData API as well to improve performance even more (but it's a headache since the documentation is a bit limited sometimes)