Is it not possible to create uninitialized managed arrays in Unity C# for performance? Error says GC.AllocateUnitializedArray is not found
It may depend on the Unity version that you’re using, but it’s possible that it’s not supported at all. You do know that the “performance benefit” is tiny and only accounts for the creation of the array and only has any effect on really huge array when we are in the mega bytes range. For anything smaller it makes absolutely no sense. The creation of objects or arrays is always slow and the initialization is only a fraction of that time. If performance is an issue you should avoid creating new arrays and instead think of reusing the array(s) you already have.
There are very, very few reasons to create uninitialized objects or arrays. It’s a major security risk which could even reveal other information from other applications. So your really need a good justification and a really thorough analysis of the code that works with that array.
Can you share any more details in what you need it for?
Specifically I wanted it for the ComputeBuffer’s “GetData()” function. It just seems like since that function fills the array immediately, there is no need to initialize it with zeroes.
It’s not available in the .Net Standard 2.1 or .Net Framework 4.8 that Unity 6 uses.
Probably won’t get it until the CoreCLR update.
But as I said, for performance you should reuse the array anyways. Creating arrays is probably 10 times slower than initializing the array with zero. That’s why you usually create a large enough array and reuse it.
Is there perhaps an equivalent returning a native Collection? If so, that would be preferable.