8

With the new readonly instance member features in C# 8, I try to minimize unnecessary copying of struct instances in my code.

I do have some foreach iterations over arrays of structs, and according to this answer, it means that every element is copied when iterating over the array.

I thought I could simply modify my code now to prevent the copying, like so:

// Example struct, real structs may be even bigger than 32 bytes.
struct Color
{
    public int R;
    public int G;
    public int B;
    public int A;
}

class Program
{
    static void Main()
    {
        Color[] colors = new Color[128];
        foreach (ref readonly Color color in ref colors) // note 'ref readonly' placed here
            Debug.WriteLine($"Color is {color.R} {color.G} {color.B} {color.A}.");
    }
}

This sadly does not compile with

CS1510  A ref or out value must be an assignable variable

However, using an indexer like this compiles:

static void Main()
{
    Color[] colors = new Color[128];
    for (int i = 0; i < colors.Length; i++)
    {
        ref readonly Color color = ref colors[i];
        Debug.WriteLine($"Color is {color.R} {color.G} {color.B} {color.A}.");
    }
}

Is my syntax in the foreach alternative wrong, or is this simply not possible in C# 8 (possibly because of how the enumeration is implemented internally)? Or is C# 8 applying some intelligence nowadays and does no longer copy the Color instances by itself?

Petter Hesselberg
  • 5,062
  • 2
  • 24
  • 42
Ray
  • 7,940
  • 7
  • 58
  • 90
  • 2
    This doesn't invalidate your question, but copying the instance (which is only 4 bytes) is actually far more efficient than using it in-place (where the compiler has to worry about aliasing). – Ben Voigt Sep 23 '19 at 20:24
  • @BenVoigt That is a valid comment, I should add that this is just dumb example code, and my structs may be bigger than 4 bytes. - edited to be 32 bytes large now. – Ray Sep 23 '19 at 20:25
  • Did the linked proposal modify the behavior of `foreach` in any way? I thought it's only about `readonly` members of `struct`s – UnholySheep Sep 23 '19 at 20:26
  • 2
    Also based on my (limited) knowledge the way to achieve what you want is to use `ReadOnlySpan colors = new Color[128];` (which allows you to use `ref readonly` in a `foreach`) – UnholySheep Sep 23 '19 at 20:27
  • @UnholySheep I think `foreach` was sadly not touched by the proposal, but I'm not having a complete overview of the new features in my head. The `Span` cannot be used as a class member (which the arrays often are), but I can try using `Memory.Span[x]` which would work on a class level and with indexers... not an optimal solution though to change that everywhere IMHO. – Ray Sep 23 '19 at 20:29
  • As I read it, the only change with Readonly instance members is: Previous you either marked the whoel struct readonly (including all fields), or nothing was readonly. Now you get to specify readonly on a member level. – Christopher Sep 23 '19 at 20:42
  • 1
    One thing to keep in mind here, is that foreach does *not* work with collections. It **only** works with enumerators. Wich will be implicitly created from any existing collection. So you will get all the enumerator overhead at the start and on each itteration/call of .next(). Normally I only mention it when peope run into the "non-mutable" property of Enumerators. But for your case this performance difference might mater and affect the larger problem. – Christopher Sep 23 '19 at 20:44

2 Answers2

6

foreach works based on the target type's definitions rather than some internal blackboxes. We could make use of this to create by-ref enumeration support:

//using System;

public readonly struct ArrayEnumerableByRef<T>
{
    private readonly T[] _target;

    public ArrayEnumerableByRef(T[] target) => _target = target;

    public Enumerator GetEnumerator() => new Enumerator(_target);

    public struct Enumerator
    {
        private readonly T[] _target;

        private int _index;

        public Enumerator(T[] target)
        {
            _target = target;
            _index = -1;
        }

        public readonly ref T Current
        {
            get
            {
                if (_target is null || _index < 0 || _index > _target.Length)
                {
                    throw new InvalidOperationException();
                }
                return ref _target[_index];
            }
        }

        public bool MoveNext() => ++_index < _target.Length;

        public void Reset() => _index = -1;
    }
}

public static class ArrayExtensions
{
    public static ArrayEnumerableByRef<T> ToEnumerableByRef<T>(this T[] array) => new ArrayEnumerableByRef<T>(array);
}

Then we could enumerate an array with foreach loop by reference:

static void Main()
{
    var colors = new Color[128];

    foreach (ref readonly var color in colors.ToEnumerableByRef())
    {
        Debug.WriteLine($"Color is {color.R} {color.G} {color.B} {color.A}.");
    }
}
Alsein
  • 4,268
  • 1
  • 15
  • 35
  • Nice solution, it gave me the idea of wrapping the array into a `Span` which implements this by-ref enumeration too, calling `foreach (ref readonly Color color in colors.AsSpan())` - would it do the same, basically? It seems to compile. – Ray Sep 25 '19 at 10:46
  • It compiles, but it is still a bit different because `Span` is defined with `ref` which is a stack-only structure, as `Span.Enumerable` is a stack-only structure too because it holds the reference to the `Span`. There are [limits to stack-only structure](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/ref#ref-struct-types). – Alsein Sep 27 '19 at 01:40
  • But for the purpose of iterating over struct arrays without copying it would suffice if I see that correctly? – Ray Sep 27 '19 at 09:39
5

Inspried by Alsein's answer, I realized that I can simply retrieve a Span of an array with the AsSpan() extension method (available in the System namespace), and use the span capability of ref-enumerating it:

static void Main()
{
    Color[] colors = new Color[128];
    foreach (ref readonly Color color in colors.AsSpan())
        Debug.WriteLine($"Color is {color.R} {color.G} {color.B} {color.A}.");
}

Keep in mind that this only works for arrays, not List<T> instances, I did not find a simple solution for ref-enumerating lists of structs yet.

I was worried about performance, so I measured the time it takes iterating over 10 and 1000 Color instances in the following ways:

  • for with copying
  • for with ref readonly
  • for with copying and caching the array length
  • for with ref readonly and caching the array length
  • foreach with copying
  • foreach with ref readonly and AsSpan()

ref foreach and foreach seems to perform the best (even at a longer 10000 instance run):

|            Method | ColorCount |        Mean |      Error |     StdDev | Rank |
|------------------ |----------- |------------:|-----------:|-----------:|-----:|
|               For |         10 |    76.76 ns |  0.3310 ns |  0.3096 ns |    4 |
|            ForRef |         10 |    77.31 ns |  0.4397 ns |  0.3898 ns |    4 |
|    ForCacheLength |         10 |    69.39 ns |  0.1923 ns |  0.1605 ns |    3 |
| ForCacheLengthRef |         10 |    69.46 ns |  0.4859 ns |  0.4545 ns |    3 |
|           ForEach |         10 |    68.28 ns |  0.7367 ns |  0.6152 ns |    2 |
|        ForEachRef |         10 |    64.76 ns |  0.6355 ns |  0.5944 ns |    1 |
|               For |       1000 | 6,912.80 ns | 49.9517 ns | 44.2808 ns |    7 |
|            ForRef |       1000 | 6,882.85 ns | 44.9467 ns | 39.8441 ns |    7 |
|    ForCacheLength |       1000 | 6,874.55 ns | 59.6360 ns | 55.7835 ns |    7 |
| ForCacheLengthRef |       1000 | 6,871.79 ns | 42.3081 ns | 39.5750 ns |    7 |
|           ForEach |       1000 | 6,701.68 ns | 31.3103 ns | 27.7558 ns |    6 |
|        ForEachRef |       1000 | 6,341.90 ns | 80.8536 ns | 75.6305 ns |    5 |
Ray
  • 7,940
  • 7
  • 58
  • 90
  • 1
    For your last sentence, look into [BenchmarkDotNet](https://github.com/dotnet/BenchmarkDotNet), it's available as a nuget package. You should also measure simple foreach/for, without using ref, so that you can see that using ref really helps. – Lasse V. Karlsen Oct 03 '19 at 16:20
  • 1
    @LasseVågsætherKarlsen An amazing library. I just updated my answer to include the very interesting results. – Ray Oct 03 '19 at 17:19