I have array of bytes
private byte[] _data;
[Benchmark()]
public void SpanTest()
{
var temp = _data.AsSpan();
var length = BitConverter.ToInt32(temp.Slice(0, 4));
var buffData = temp.Slice(4, length);
var s = buffData[0];
}
[Benchmark()]
public async Task MemoryTest()
{
var temp = _data.AsMemory();
var length = BitConverter.ToInt32(temp.Slice(0, 4).Span);
var buffData = temp.Slice(4, length);
var s = buffData.Span[0];
}
I can't understand why Span is faster than memory like in 10 times. From my perspective, Span is allocated on the stack but it points to the data on the heap (in my case). Memory is allocated on the heap and that's only one difference that I see here. My question is why Span is so fast? I have read about ref structs but didn't understand how it works.
The performance difference you are observing between Span and Memory is mainly due to the fact that Span is a stack-only data structure, while Memory can be used on the heap. This difference impacts the performance in various ways.
However, it's important to note that the performance difference between Span and Memory is not always as significant as your benchmark results indicate. The relative performance may vary depending on the specific use case and the nature of the data being processed.
In your particular example, the performance difference might be less about Span vs. Memory and more about the fact that you are using an asynchronous method (async Task) for the MemoryTest benchmark. Asynchronous methods introduce overhead due to state machines, and this overhead might be affecting the benchmark results. If you remove the async Task and use a regular synchronous method for the MemoryTest benchmark, the performance difference between SpanTest and MemoryTest might become less pronounced.