'Rapid stack allocations vs accessing a single heap allocation

I'm having a situation where I have an array T[] which must be copied in an instant and sent over to a function accepting a ReadOnlySpan<T>. I found two solutions on this problem. However I'm interested in the one which gives the better performance.

Considerations:

  • Array number of elements range between 1 to 3 (so it's extremely small);
  • T is a readonly struct of 16 bytes managed size
  1. I create another array T[] globally (which will be heap allocated) and then use the .CopyTo() extension method on the first array and then pass down the second array

  2. I create a Span<T> locally using stackalloc (which will be stack allocated) and then use .CopyTo<T>() extension just like the previous version.

The difference is that the second approach requires me to do this every time the function is called, whereas the first approach the array is already initialized before the function is called even the first time.

Which approach do you guys think it's the better one ?



Solution 1:[1]

Ok so I ran some benchmark dotnet tests. The results were almost 99% the same (taking about speed) with a small in favor of the stackalloc version, but only if I had used in combination with the [SkipLocalsInit] attribute. Also total allocations were higher for this version. So to summarize I came to the conclusion that this is just a micro-optimization and both variants are fine as long as you keep an eye on the stack size, otherwise first version is 10x better.

And not in the end, if you thought ReadOnlySpan<T> is always pass by value, think again when looking at my example

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Damian