feat(collections)!: switch to ref struct enumerators
Refactor all unsafe collection enumerators to use ref struct types, removing support for boxing and standard .NET enumeration interfaces. GetEnumerator methods now return stack-only, more efficient enumerators with [UnscopedRef] and inlining attributes. IEnumerable<T> and IEnumerable implementations are removed from affected types. Interfaces now require unmanaged types. Also includes minor doc and bug fixes. BREAKING CHANGE: Enumerators are no longer compatible with LINQ, and collections no longer implement IEnumerable/IEnumerator.
This commit is contained in:
@@ -3,6 +3,7 @@ using Misaki.HighPerformance.LowLevel.Collections.Contracts;
|
||||
using Misaki.HighPerformance.LowLevel.Utilities;
|
||||
using System.Collections;
|
||||
using System.Diagnostics;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Misaki.HighPerformance.LowLevel.Collections;
|
||||
@@ -42,18 +43,16 @@ internal class UnsafeArrayDebugView<T>
|
||||
public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
|
||||
where T : unmanaged
|
||||
{
|
||||
public struct Enumerator : IEnumerator<T>
|
||||
public ref struct Enumerator
|
||||
{
|
||||
private readonly UnsafeArray<T>* _collection;
|
||||
private ref UnsafeArray<T> _collection;
|
||||
private int _index;
|
||||
|
||||
public readonly ref T Current => ref _collection->_buffer[_index];
|
||||
readonly T IEnumerator<T>.Current => Current;
|
||||
readonly object IEnumerator.Current => Current;
|
||||
public readonly ref T Current => ref _collection._buffer[_index];
|
||||
|
||||
public Enumerator(UnsafeArray<T>* collection)
|
||||
public Enumerator(ref UnsafeArray<T> collection)
|
||||
{
|
||||
_collection = collection;
|
||||
_collection = ref collection;
|
||||
_index = -1;
|
||||
}
|
||||
|
||||
@@ -61,17 +60,13 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
|
||||
public bool MoveNext()
|
||||
{
|
||||
_index++;
|
||||
return _index < _collection->_count;
|
||||
return _index < _collection._count;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
_index = -1;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
private T* _buffer;
|
||||
@@ -130,21 +125,6 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
|
||||
}
|
||||
}
|
||||
|
||||
public Enumerator GetEnumerator()
|
||||
{
|
||||
return new((UnsafeArray<T>*)UnsafeUtility.AddressOf(ref this));
|
||||
}
|
||||
|
||||
IEnumerator<T> IEnumerable<T>.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Invalid constructor, use <see cref="UnsafeArray(int, Allocator, AllocationOption)"/> or <see cref="UnsafeArray(int, AllocationHandle, AllocationOption)"/> instead.
|
||||
/// </summary>
|
||||
@@ -245,6 +225,13 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
|
||||
return new ReadOnlyUnsafeCollection<T>(_buffer, _count);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[UnscopedRef]
|
||||
public Enumerator GetEnumerator()
|
||||
{
|
||||
return new Enumerator(ref this);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Resize(int newSize, AllocationOption option = AllocationOption.None)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user