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:
@@ -2,6 +2,7 @@ using Misaki.HighPerformance.LowLevel.Buffer;
|
||||
using Misaki.HighPerformance.LowLevel.Collections.Contracts;
|
||||
using Misaki.HighPerformance.LowLevel.Utilities;
|
||||
using System.Collections;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Misaki.HighPerformance.LowLevel.Collections;
|
||||
@@ -13,19 +14,17 @@ namespace Misaki.HighPerformance.LowLevel.Collections;
|
||||
public unsafe struct UnsafeQueue<T> : IUnsafeCollection<T>
|
||||
where T : unmanaged
|
||||
{
|
||||
public struct Enumerator : IEnumerator<T>
|
||||
public ref struct Enumerator
|
||||
{
|
||||
private readonly UnsafeQueue<T>* _collection;
|
||||
private readonly ref UnsafeQueue<T> _collection;
|
||||
private int _currentIndex;
|
||||
|
||||
// We assume _currentIndex will always be in range when accessed.
|
||||
public readonly ref T Current => ref _collection->_array[(_collection->_offset + _currentIndex) % _collection->Capacity];
|
||||
readonly T IEnumerator<T>.Current => Current;
|
||||
readonly object IEnumerator.Current => Current;
|
||||
public readonly ref T Current => ref _collection._array[(_collection._offset + _currentIndex) % _collection.Capacity];
|
||||
|
||||
public Enumerator(UnsafeQueue<T>* collection)
|
||||
public Enumerator(ref UnsafeQueue<T> collection)
|
||||
{
|
||||
_collection = collection;
|
||||
_collection = ref collection;
|
||||
_currentIndex = -1;
|
||||
}
|
||||
|
||||
@@ -33,17 +32,13 @@ public unsafe struct UnsafeQueue<T> : IUnsafeCollection<T>
|
||||
public bool MoveNext()
|
||||
{
|
||||
_currentIndex++;
|
||||
return _currentIndex < _collection->_count;
|
||||
return _currentIndex < _collection._count;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
_currentIndex = -1;
|
||||
}
|
||||
|
||||
public readonly void Dispose()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
private UnsafeArray<T> _array;
|
||||
@@ -62,21 +57,6 @@ public unsafe struct UnsafeQueue<T> : IUnsafeCollection<T>
|
||||
set => _array[index] = value;
|
||||
}
|
||||
|
||||
public Enumerator GetEnumerator()
|
||||
{
|
||||
return new((UnsafeQueue<T>*)UnsafeUtility.AddressOf(ref this));
|
||||
}
|
||||
|
||||
IEnumerator<T> IEnumerable<T>.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Invalid constructor. Use <see cref="UnsafeQueue(int, Allocator, AllocationOption)"/> or <see cref="UnsafeQueue(int, AllocationHandle, AllocationOption)"/> instead."/>
|
||||
/// </summary>
|
||||
@@ -97,6 +77,13 @@ public unsafe struct UnsafeQueue<T> : IUnsafeCollection<T>
|
||||
{
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[UnscopedRef]
|
||||
public Enumerator GetEnumerator()
|
||||
{
|
||||
return new Enumerator(ref this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a reference to the item at the front of the queue without removing it.
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user