Refactor unsafe collections and improve memory handling
Some checks failed
Publish NuGet Packages / publish (pull_request) Has been cancelled

Refactored enumerators across multiple unsafe collections to use
`ref` returns for `Current`, improving performance and reducing
memory usage. Enhanced memory management with `AllocationOption`
support and optimized resizing logic for collections like
`UnsafeBitSet`, `UnsafeSlotMap`, and `UnsafeSparseSet`.

Updated `publish-nuget.yaml` to support manual workflow dispatch
and trigger on `push`/`pull_request` events. Incremented project
version to `1.1.2` and ensured NuGet package generation on build.
This commit is contained in:
2025-11-11 21:20:33 +09:00
parent bc8b2c0aaa
commit bf4dd5670e
12 changed files with 128 additions and 240 deletions

View File

@@ -17,46 +17,29 @@ public unsafe struct UnsafeQueue<T> : IUnsafeCollection<T>
public struct Enumerator : IEnumerator<T>
{
private readonly UnsafeQueue<T>* _collection;
private int _index;
private T _value;
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 Enumerator(UnsafeQueue<T>* collection)
{
_collection = collection;
_index = -1;
_value = default;
_currentIndex = -1;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext()
{
_index++;
if (_index < _collection->_count)
{
_value = UnsafeUtility.ReadArrayElement<T>(_collection->_array.GetUnsafePtr(), _index);
return true;
}
_value = default;
return false;
_currentIndex++;
return _currentIndex < _collection->_count;
}
public void Reset()
{
_index = -1;
}
// Let NativeArray indexer check for out of range.
public readonly T Current
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _value;
}
readonly object IEnumerator.Current
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => Current;
_currentIndex = -1;
}
public readonly void Dispose()
@@ -128,7 +111,7 @@ public unsafe struct UnsafeQueue<T> : IUnsafeCollection<T>
{
if (_count >= Capacity)
{
Resize(Capacity + (int)(Capacity * 0.5f));
Resize((int)(Capacity * 1.5f));
}
UnsafeUtility.WriteArrayElement(_array.GetUnsafePtr(), (_offset + _count) % Capacity, value);