我可以在不复制 C# 8 中的元素的情况下遍历结构数组吗?
Can I foreach over an array of structs without copying the elements in C# 8?
使用新的 readonly instance member features in C# 8,我尝试减少代码中不必要的结构实例复制。
我确实对结构数组进行了一些 foreach
迭代,根据 this answer,这意味着在对数组进行迭代时会复制每个元素。
我想我现在可以简单地修改我的代码来防止复制,就像这样:
// 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}.");
}
}
遗憾的是不能用
编译
CS1510 A ref or out value must be an assignable variable
但是,使用这样的索引器编译:
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}.");
}
}
我在 foreach
替代方案中的语法是否错误,或者这在 C# 8 中根本不可能(可能是因为枚举的内部实现方式)?
还是 C# 8 现在应用了一些智能,不再自己复制 Color
个实例?
foreach
基于目标类型的定义而不是一些内部黑盒工作。我们可以利用它来创建引用枚举支持:
//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);
}
然后我们可以通过引用枚举一个带有foreach
循环的数组:
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 回答的启发,我意识到我可以使用 AsSpan()
扩展方法(在 System
命名空间中可用)简单地检索数组的 Span
,并使用引用枚举的跨度能力:
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}.");
}
请记住,这仅适用于数组,不适用于 List<T>
个实例,我还没有找到引用枚举结构列表的简单解决方案。
我担心性能问题,所以 I measured the time it takes 通过以下方式迭代 10 和 1000 个 Color
实例:
for
复制
for
与 ref readonly
for
复制和缓存数组 length
for
与 ref readonly
并缓存数组长度
foreach
复制
foreach
与 ref readonly
和 AsSpan()
ref foreach
和 foreach
似乎表现最好(即使在更长的 10000 个实例中 运行):
| 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 |
使用新的 readonly instance member features in C# 8,我尝试减少代码中不必要的结构实例复制。
我确实对结构数组进行了一些 foreach
迭代,根据 this answer,这意味着在对数组进行迭代时会复制每个元素。
我想我现在可以简单地修改我的代码来防止复制,就像这样:
// 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}.");
}
}
遗憾的是不能用
编译CS1510 A ref or out value must be an assignable variable
但是,使用这样的索引器编译:
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}.");
}
}
我在 foreach
替代方案中的语法是否错误,或者这在 C# 8 中根本不可能(可能是因为枚举的内部实现方式)?
还是 C# 8 现在应用了一些智能,不再自己复制 Color
个实例?
foreach
基于目标类型的定义而不是一些内部黑盒工作。我们可以利用它来创建引用枚举支持:
//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);
}
然后我们可以通过引用枚举一个带有foreach
循环的数组:
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 回答的启发,我意识到我可以使用 AsSpan()
扩展方法(在 System
命名空间中可用)简单地检索数组的 Span
,并使用引用枚举的跨度能力:
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}.");
}
请记住,这仅适用于数组,不适用于 List<T>
个实例,我还没有找到引用枚举结构列表的简单解决方案。
我担心性能问题,所以 I measured the time it takes 通过以下方式迭代 10 和 1000 个 Color
实例:
for
复制for
与ref readonly
for
复制和缓存数组 lengthfor
与ref readonly
并缓存数组长度foreach
复制foreach
与ref readonly
和AsSpan()
ref foreach
和 foreach
似乎表现最好(即使在更长的 10000 个实例中 运行):
| 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 |