Damian
Damian

Reputation: 137

C# is this a correct usage of ReadOnlySpan<T>?

Motivation: I am working on a piece of software that should squeeze as much performance as possible from the machine it works on.

Hypothesis: Any array in .NET 6 can be converted behind the scenes into a ReadOnlySpan<T> or variations of it.

Question: Is there any performance benefit if you replace all function passing parameters from an array into a ReadOnlySpan<T> ?

Concrete Example:

private float MethodWithArrayOfFloats(float[] arr)
{
    float sum = 0;
    for (var index = 0; index < arr.Length; index++)
    {
        sum += arr[index];
    }
    //What if I would have called here another method passing this array parameter ?
    return sum;
}

private float MethodWithArrayOfSpans(ReadOnlySpan<float> arr)
{
    float sum = 0;
    for (var index = 0; index < arr.Length; index++)
    {
        sum += arr[index];
    }
    //What if I would have called here another method passing this ReadOnlySpan parameter ?
    return sum;
}

Other observations:

Nonetheless, this is a mystery for me. Is this a good practice to apply this technique to all scenarios ? If not why does it seem to be more performant ? What are the hidden implications ?

Upvotes: 3

Views: 6393

Answers (1)

Clemens
Clemens

Reputation: 716

From my perspective it should be amost always more performant to use ReadOnlySpan<T>. As ReadOnlySpan<T> is a ref struct, there are several limitations. And thus the code gets less maintainability. So if what you do is on the hot path of a critical solution use ReadonlySpan, if not use it if it fits and does not complicate your code.

Upvotes: 4

Related Questions