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,24 +43,22 @@ internal class UnsafeSlotMapDebugView<T>
|
||||
public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
|
||||
where T : unmanaged
|
||||
{
|
||||
public struct Enumerator : IEnumerator<T>
|
||||
public ref struct Enumerator
|
||||
{
|
||||
private readonly UnsafeSlotMap<T>* _collection;
|
||||
private ref UnsafeSlotMap<T> _collection;
|
||||
private int _currentIndex;
|
||||
|
||||
public readonly ref T Current => ref _collection->_data[_currentIndex];
|
||||
readonly T IEnumerator<T>.Current => Current;
|
||||
readonly object? IEnumerator.Current => Current;
|
||||
public readonly ref T Current => ref _collection._data[_currentIndex];
|
||||
|
||||
public Enumerator(UnsafeSlotMap<T>* collection)
|
||||
public Enumerator(ref UnsafeSlotMap<T> collection)
|
||||
{
|
||||
_collection = collection;
|
||||
_collection = ref collection;
|
||||
_currentIndex = -1;
|
||||
}
|
||||
|
||||
public bool MoveNext()
|
||||
{
|
||||
_currentIndex = _collection->_validBits.NextSetBit(_currentIndex + 1);
|
||||
_currentIndex = _collection._validBits.NextSetBit(_currentIndex + 1);
|
||||
return _currentIndex != -1;
|
||||
}
|
||||
|
||||
@@ -67,10 +66,6 @@ public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
|
||||
{
|
||||
_currentIndex = -1;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
private UnsafeArray<T> _data;
|
||||
@@ -86,21 +81,6 @@ public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
|
||||
|
||||
public readonly bool IsCreated => _data.IsCreated && _generations.IsCreated && _freeSlots.IsCreated && _validBits.IsCreated;
|
||||
|
||||
public Enumerator GetEnumerator()
|
||||
{
|
||||
return new((UnsafeSlotMap<T>*)UnsafeUtility.AddressOf(ref this));
|
||||
}
|
||||
|
||||
IEnumerator<T> IEnumerable<T>.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Invalid constructor. Use <see cref="UnsafeSlotMap(int, Allocator, AllocationOption)"/> or <see cref="UnsafeSlotMap(int, AllocationHandle, AllocationOption)"/> instead."/>
|
||||
/// </summary>
|
||||
@@ -151,6 +131,13 @@ public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
|
||||
{
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[UnscopedRef]
|
||||
public Enumerator GetEnumerator()
|
||||
{
|
||||
return new Enumerator(ref this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the specified item to the collection and returns the index of the slot where it was stored.
|
||||
/// </summary>
|
||||
@@ -328,7 +315,7 @@ public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
|
||||
|
||||
public readonly void* GetUnsafePtr()
|
||||
{
|
||||
return (T*)_data.GetUnsafePtr() + 1;
|
||||
return (T*)_data.GetUnsafePtr();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
|
||||
Reference in New Issue
Block a user