List to T[] without copying

后端 未结 8 781
名媛妹妹
名媛妹妹 2020-12-05 12:51

I\'m have a large list of value types that needs to be given to OpenGL. It would be great if this could happen as quickly as possible. What I\'m doing now looks like this:

8条回答
  •  鱼传尺愫
    2020-12-05 13:45

    You may want to consider if your approach to this is wrong. If you find yourself using reflection to do this - you've already lost.

    I can think of a few ways to approach this though which one is ideal depends a lot on whether this is a multi-threaded piece of code or not.

    Let's assume it's not ...

    Think about the characteristics of the array. Each time this method is called an N-length array is created. Your goal is to improve performance (which implies you want to minimize allocations and data copies).

    Can you hint at compile or runtime what the ideal starting size for the array is? I mean - if 95% of the time the N-length is 100k or less ... start with a 100k item array. Keep using it until you hit a case where the array is too small.

    When you hit this case you can decide what you do based on your understanding of the program. Should the array grow 10%? Should it grow to the literal needed length? Can you use what you have and continue the process for the rest of the data?

    Over time the ideal size will be found. You can even have your program monitor the final size each time it runs and use that as a hint for allocation the next time it starts (perhaps this array length depends on environmental factors such as resolution, etc).

    In other words - what I'm suggesting is that you not use the List-to-Array method and that you pre-allocate an array, keep it around forever, and grow it as needed.

    If your program has threading issues you will obviously need to address those.

提交回复
热议问题