Job system priorities, async waits, parallel map/queue

Major refactor:
- Add job priority tiers and async wait APIs to IJobScheduler
- Implement priority-based job queues and scheduling logic
- Introduce UnsafeParallelHashMap and refactor UnsafeParallelQueue
- Refactor UnsafeSlotMap to chunked storage for scalability
- Update SlotMap/ConcurrentSlotMap for consistency and perf
- Add new benchmarks and unit tests for parallel collections
- Misc: add MemoryUtility.AlignUp, version bumps, test improvements, bug fixes
This commit is contained in:
2026-04-18 11:26:08 +09:00
parent d5616daa05
commit 13802ca6c8
22 changed files with 1459 additions and 267 deletions

View File

@@ -166,7 +166,7 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int CeilPow2(int x)
internal static int CeilPow2(int x)
{
x -= 1;
x |= x >> 1;

View File

@@ -1,7 +1,6 @@
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;

View File

@@ -0,0 +1,504 @@
using Misaki.HighPerformance.LowLevel.Buffer;
using Misaki.HighPerformance.LowLevel.Utilities;
using System.Diagnostics;
using System.Numerics;
using System.Runtime.CompilerServices;
namespace Misaki.HighPerformance.LowLevel.Collections;
public unsafe struct UnsafeParallelHashMapData<TKey, TValue>
where TKey : unmanaged, IEquatable<TKey>
where TValue : unmanaged
{
public byte* buffer;
public TKey* keys;
public TValue* values;
public int* next;
public int* buckets;
public int count;
public int capacity;
public int bucketCapacityMask;
public int allocatedIndex;
public int firstFreeIndex;
public int alignment;
public int log2MinGrowth;
#if MHP_ENABLE_SAFETY_CHECKS
public MemoryHandle memoryHandle;
#endif
public AllocationHandle allocationHandle;
}
public unsafe struct UnsafeParallelHashMap<TKey, TValue> : IDisposable
where TKey : unmanaged, IEquatable<TKey>
where TValue : unmanaged
{
internal UnsafeParallelHashMapData<TKey, TValue>* _data;
public const int MINIMAL_CAPACITY = 64;
public readonly int Count => _data != null ? _data->count : 0;
public readonly int Capacity => _data != null ? _data->capacity : 0;
public readonly bool IsEmpty => !IsCreated || _data->count == 0;
public readonly bool IsCreated
{
get
{
#if MHP_ENABLE_SAFETY_CHECKS
if (_data != null)
{
if (_data->buffer != null)
{
return _data->memoryHandle.IsValid;
}
}
return false;
#else
return _data != null && _data->buffer != null;
#endif
}
}
public UnsafeParallelHashMap(int capacity, uint minGrowth, AllocationHandle handle, AllocationOption allocationOption)
{
ArgumentOutOfRangeException.ThrowIfNegative(capacity);
_data = (UnsafeParallelHashMapData<TKey, TValue>*)handle.Alloc(handle.State, (uint)sizeof(UnsafeParallelHashMapData<TKey, TValue>), (nuint)AlignOf<UnsafeParallelHashMapData<TKey, TValue>>(), AllocationOption.Clear);
if (_data == null)
throw new OutOfMemoryException("Failed to allocate UnsafeParallelHashMapData.");
_data->capacity = capacity;
_data->bucketCapacityMask = capacity * 2 - 1;
var alignOfKey = (int)AlignOf<TKey>();
var alignOfTValue = (int)AlignOf<TValue>();
var alignOfInt = (int)AlignOf<int>();
var maxDataAlign = Math.Max(Math.Max(alignOfTValue, alignOfKey), alignOfInt);
_data->alignment = maxDataAlign;
_data->log2MinGrowth = BitOperations.Log2(minGrowth);
_data->allocationHandle = handle;
var totalSize = CalculateDataSize(capacity, capacity * 2, out var keyOffset, out var valueOffset, out var nextOffset, out var bucketOffset);
allocationOption &= ~AllocationOption.Clear;
AllocateBuffer(_data, totalSize, keyOffset, valueOffset, nextOffset, bucketOffset, allocationOption);
#if MHP_ENABLE_SAFETY_CHECKS
_data->memoryHandle = MemoryHandle.Create(_data->buffer, (nuint)totalSize);
#endif
Clear();
}
public void Dispose()
{
if (!IsCreated)
return;
#if MHP_ENABLE_SAFETY_CHECKS
_data->memoryHandle.Dispose();
#endif
if (_data->buffer != null && _data->allocationHandle.Free != null)
{
_data->allocationHandle.Free(_data->allocationHandle.State, _data->buffer);
_data->buffer = null;
}
if (_data != null && _data->allocationHandle.Free != null)
{
_data->allocationHandle.Free(_data->allocationHandle.State, _data);
_data = null;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Conditional("MHP_ENABLE_SAFETY_CHECKS")]
private readonly void ThrowIfNotCreated()
{
if (!IsCreated)
{
throw new InvalidOperationException("The UnsafeParallelHashMap is not created.");
}
}
private static int CalculateDataSize(int capacity, int bucketCapacity, out int outKeyOffset, out int outValueOffset, out int outNextOffset, out int outBucketOffset)
{
var sizeOfTKey = sizeof(TKey);
var sizeOfTValue = sizeof(TValue);
var sizeOfInt = sizeof(int);
var keysSize = sizeOfTKey * capacity;
var valuesSize = sizeOfTValue * capacity;
var nextSize = sizeOfInt * capacity;
var bucketSize = sizeOfInt * bucketCapacity;
var totalSize = keysSize + valuesSize + nextSize + bucketSize;
outKeyOffset = 0;
outValueOffset = outKeyOffset + keysSize;
outNextOffset = outValueOffset + valuesSize;
outBucketOffset = outNextOffset + nextSize;
return totalSize;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static uint AlignOf<T>() where T : unmanaged
{
return (uint)Unsafe.SizeOf<T>(); // Temporary substitute for alignment util
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static int CeilPow2(int x)
{
x -= 1;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x + 1;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private readonly int CalcCapacityCeilPow2(int capacity)
{
capacity = Math.Max(Math.Max(1, _data->count), capacity);
var newCapacity = Math.Max(capacity, 1 << _data->log2MinGrowth);
var result = CeilPow2(newCapacity);
return result;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void AllocateBuffer(UnsafeParallelHashMapData<TKey, TValue>* data, int totalSize, int keyOffset, int valueOffset, int nextOffset, int bucketOffset, AllocationOption allocationOption)
{
if (data->allocationHandle.Alloc == null)
{
throw new InvalidOperationException("Target allocation handle does not support allocation.");
}
var buf = (byte*)data->allocationHandle.Alloc(data->allocationHandle.State, (uint)totalSize, (nuint)data->alignment, allocationOption);
data->buffer = buf;
data->keys = (TKey*)(buf + keyOffset);
data->values = (TValue*)(buf + valueOffset);
data->next = (int*)(buf + nextOffset);
data->buckets = (int*)(buf + bucketOffset);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private readonly int GetBucket(int hash)
{
return hash & _data->bucketCapacityMask;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private readonly int GetBucket(scoped in TKey key)
{
return GetBucket(key.GetHashCode());
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private readonly void CheckIndexOutOfBounds(int idx)
{
if ((uint)idx >= (uint)_data->capacity)
{
throw new InvalidOperationException($"Index {idx} is out of bounds for the hash map with capacity {_data->capacity}.");
}
}
public void Clear()
{
ThrowIfNotCreated();
_data->count = 0;
_data->allocatedIndex = 0;
_data->firstFreeIndex = -1;
if (_data->buffer == null)
return;
var bucketCapacity = _data->bucketCapacityMask + 1;
MemoryUtility.MemSet(_data->buckets, (byte)0xFF, (nuint)(bucketCapacity * sizeof(int)));
MemoryUtility.MemSet(_data->next, (byte)0xFF, (nuint)(_data->capacity * sizeof(int)));
}
public int Add(scoped in TKey key, scoped in TValue value)
{
ThrowIfNotCreated();
if (Find(in key) != -1)
return -1; // Or throw depending on semantics you want
return AllocateEntry(key, value);
}
public bool TryGetValue(scoped in TKey key, out TValue item)
{
var idx = Find(key);
if (idx != -1)
{
item = _data->values[idx];
return true;
}
item = default;
return false;
}
public int Find(scoped in TKey key)
{
ThrowIfNotCreated();
if (_data->allocatedIndex <= 0)
{
return -1;
}
var bucket = GetBucket(key);
var entryIdx = _data->buckets[bucket];
if ((uint)entryIdx < (uint)_data->capacity)
{
var nextPtrs = _data->next;
while (!UnsafeUtility.ReadArrayElement<TKey>(_data->keys, entryIdx).Equals(key))
{
entryIdx = nextPtrs[entryIdx];
if ((uint)entryIdx >= (uint)_data->capacity)
{
return -1;
}
}
return entryIdx;
}
return -1;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private int AllocateEntry(scoped in TKey key, scoped in TValue value)
{
int idx;
if (_data->allocatedIndex >= _data->capacity && _data->firstFreeIndex < 0)
{
var newCap = CalcCapacityCeilPow2(_data->capacity + (1 << _data->log2MinGrowth));
Resize(newCap);
}
idx = _data->firstFreeIndex;
if (idx >= 0)
{
_data->firstFreeIndex = _data->next[idx];
}
else
{
idx = _data->allocatedIndex++;
}
CheckIndexOutOfBounds(idx);
UnsafeUtility.WriteArrayElement(_data->keys, idx, key);
UnsafeUtility.WriteArrayElement(_data->values, idx, value);
var bucket = GetBucket(key);
_data->next[idx] = _data->buckets[bucket];
_data->buckets[bucket] = idx;
_data->count++;
return idx;
}
public bool Remove(scoped in TKey key)
{
ThrowIfNotCreated();
if (_data->capacity == 0)
{
return false;
}
var removed = false;
var bucket = GetBucket(key);
var prevEntry = -1;
var entryIdx = _data->buckets[bucket];
while (entryIdx >= 0 && entryIdx < _data->capacity)
{
if (UnsafeUtility.ReadArrayElement<TKey>(_data->keys, entryIdx).Equals(key))
{
removed = true;
if (prevEntry < 0)
{
_data->buckets[bucket] = _data->next[entryIdx];
}
else
{
_data->next[prevEntry] = _data->next[entryIdx];
}
var nextIdx = _data->next[entryIdx];
_data->next[entryIdx] = _data->firstFreeIndex;
_data->firstFreeIndex = entryIdx;
entryIdx = nextIdx;
break;
}
else
{
prevEntry = entryIdx;
entryIdx = _data->next[entryIdx];
}
}
if (removed)
_data->count--;
return removed;
}
private void ResizeExact(int newCapacity, int newBucketCapacity)
{
var totalSize = CalculateDataSize(newCapacity, newBucketCapacity, out var keyOffset, out var valueOffset, out var nextOffset, out var bucketOffset);
var oldBuffer = _data->buffer;
var oldKeys = _data->keys;
var oldValues = _data->values;
var oldNext = _data->next;
var oldBuckets = _data->buckets;
var oldBucketCapacity = _data->bucketCapacityMask + 1;
AllocateBuffer(_data, totalSize, keyOffset, valueOffset, nextOffset, bucketOffset, AllocationOption.None);
_data->capacity = newCapacity;
_data->bucketCapacityMask = newBucketCapacity - 1;
Clear();
for (int i = 0, num = oldBucketCapacity; i < num; ++i)
{
for (var idx = oldBuckets[i]; idx != -1; idx = oldNext[idx])
{
Add(oldKeys[idx], oldValues[idx]);
}
}
if (_data->allocationHandle.Free != null && oldBuffer != null)
{
_data->allocationHandle.Free(_data->allocationHandle.State, oldBuffer);
}
#if MHP_ENABLE_SAFETY_CHECKS
_data->memoryHandle.Update(_data->buffer, (nuint)totalSize);
#endif
}
public void Resize(int newCapacity)
{
ThrowIfNotCreated();
newCapacity = Math.Max(newCapacity, _data->count);
var newBucketCapacity = CeilPow2(newCapacity * 2);
if (_data->capacity == newCapacity && (_data->bucketCapacityMask + 1) == newBucketCapacity)
{
return;
}
ResizeExact(newCapacity, newBucketCapacity);
}
public ParallelWriter AsParallelWriter()
{
ThrowIfNotCreated();
return new ParallelWriter(_data);
}
public unsafe struct ParallelWriter
{
internal UnsafeParallelHashMapData<TKey, TValue>* _data;
internal ParallelWriter(UnsafeParallelHashMapData<TKey, TValue>* data)
{
_data = data;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryAdd(TKey key, TValue item)
{
if (_data == null || _data->buffer == null)
{
throw new InvalidOperationException("The UnsafeParallelHashMap is not created.");
}
var hash = key.GetHashCode();
var bucket = hash & _data->bucketCapacityMask;
ref var bucketValue = ref _data->buckets[bucket];
var entryIdx = bucketValue;
var nextPtrs = _data->next;
// Optional Fast path Check if item exists. (Does not lock)
if ((uint)entryIdx < (uint)_data->capacity)
{
while (!UnsafeUtility.ReadArrayElement<TKey>(_data->keys, entryIdx).Equals(key))
{
entryIdx = nextPtrs[entryIdx];
if ((uint)entryIdx >= (uint)_data->capacity)
{
break;
}
}
if ((uint)entryIdx < (uint)_data->capacity)
{
// Item already exists
return false;
}
}
// Allocate a new slot from the contiguous array atomically
var idx = Interlocked.Increment(ref _data->allocatedIndex) - 1;
if (idx >= _data->capacity)
{
// UnsafeParallelHashMap does not resize concurrently. Must pre-allocate enough memory.
Interlocked.Decrement(ref _data->allocatedIndex);
throw new InvalidOperationException($"Hash map capacity ({_data->capacity}) exceeded during parallel writing.");
}
// Write our data
UnsafeUtility.WriteArrayElement(_data->keys, idx, key);
UnsafeUtility.WriteArrayElement(_data->values, idx, item);
ref var b = ref _data->buckets[bucket];
// Atomically link into bucket linked list
while (true)
{
var bucketHead = Volatile.Read(ref b);
UnsafeUtility.WriteArrayElement(_data->next, idx, bucketHead);
if (Interlocked.CompareExchange(ref b, idx, bucketHead) == bucketHead)
{
Interlocked.Increment(ref _data->count);
return true;
}
}
}
}
}

View File

@@ -8,7 +8,7 @@ namespace Misaki.HighPerformance.LowLevel.Collections;
/// A dynamically resizing, parallel, lock-free queue using unmanaged chunks.
/// Uses a very brief spin lock only during chunk allocation, alongside a lock-free segment cache.
/// </summary>
public unsafe struct UnsafeChunkedQueue<T> : IDisposable
public unsafe struct UnsafeParallelQueue<T> : IDisposable
where T : unmanaged
{
[StructLayout(LayoutKind.Sequential)]
@@ -39,28 +39,34 @@ public unsafe struct UnsafeChunkedQueue<T> : IDisposable
public readonly unsafe struct ParallelProducer
{
private readonly UnsafeChunkedQueue<T>* _queue;
private readonly UnsafeParallelQueue<T>* _queue;
internal ParallelProducer(UnsafeChunkedQueue<T>* queue)
internal ParallelProducer(UnsafeParallelQueue<T>* queue)
{
_queue = queue;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Enqueue(T item) => _queue->Enqueue(item);
public void Enqueue(T item)
{
_queue->Enqueue(item);
}
}
public readonly unsafe struct ParallelConsumer
{
private readonly UnsafeChunkedQueue<T>* _queue;
private readonly UnsafeParallelQueue<T>* _queue;
internal ParallelConsumer(UnsafeChunkedQueue<T>* queue)
internal ParallelConsumer(UnsafeParallelQueue<T>* queue)
{
_queue = queue;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryDequeue(out T item) => _queue->TryDequeue(out item);
public bool TryDequeue(out T item)
{
return _queue->TryDequeue(out item);
}
}
// Pointer representations (nint utilized for straightforward Interlocked compatibility)
@@ -79,7 +85,15 @@ public unsafe struct UnsafeChunkedQueue<T> : IDisposable
public readonly bool IsCreated => _head != 0;
public UnsafeChunkedQueue(int capacityPerChunk, AllocationHandle handle, AllocationOption allocationOption = AllocationOption.None)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static DisposablePtr<UnsafeParallelQueue<T>> Allocate(int capacityPerChunk, AllocationHandle handle, AllocationOption allocationOption = AllocationOption.None)
{
var pQueue = (UnsafeParallelQueue<T>*)handle.Alloc(handle.State, SizeOf<DisposablePtr<UnsafeParallelQueue<T>>>(), AlignOf<DisposablePtr<UnsafeParallelQueue<T>>>(), AllocationOption.None);
*pQueue = new UnsafeParallelQueue<T>(capacityPerChunk, handle, allocationOption);
return new DisposablePtr<UnsafeParallelQueue<T>>(pQueue);
}
public UnsafeParallelQueue(int capacityPerChunk, AllocationHandle handle, AllocationOption allocationOption = AllocationOption.None)
{
_chunkCapacity = Math.Max(32, capacityPerChunk);
_allocHandle = handle;
@@ -98,7 +112,7 @@ public unsafe struct UnsafeChunkedQueue<T> : IDisposable
}
[Obsolete("Use AllocationHandle instead.")]
public UnsafeChunkedQueue(int capacityPerChunk, Allocator allocator, AllocationOption allocationOption = AllocationOption.None)
public UnsafeParallelQueue(int capacityPerChunk, Allocator allocator, AllocationOption allocationOption = AllocationOption.None)
: this(capacityPerChunk, AllocationManager.GetAllocationHandle(allocator), allocationOption)
{
}
@@ -312,7 +326,7 @@ public unsafe struct UnsafeChunkedQueue<T> : IDisposable
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ParallelProducer AsParallelProducer()
{
return new ParallelProducer((UnsafeChunkedQueue<T>*)Unsafe.AsPointer(ref this));
return new ParallelProducer((UnsafeParallelQueue<T>*)Unsafe.AsPointer(ref this));
}
/// <summary>
@@ -323,7 +337,7 @@ public unsafe struct UnsafeChunkedQueue<T> : IDisposable
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ParallelConsumer AsParallelConsumer()
{
return new ParallelConsumer((UnsafeChunkedQueue<T>*)Unsafe.AsPointer(ref this));
return new ParallelConsumer((UnsafeParallelQueue<T>*)Unsafe.AsPointer(ref this));
}
public void Dispose()

View File

@@ -1,7 +1,5 @@
using Misaki.HighPerformance.LowLevel.Buffer;
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;
@@ -43,19 +41,38 @@ internal class UnsafeSlotMapDebugView<T>
public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
where T : unmanaged
{
private struct SlotEntry
{
public T value;
public int generation;
}
private const int _CHUNK_SHIFT = 8;
private const int _CHUNK_SIZE = 1 << _CHUNK_SHIFT;
private const int _CHUNK_MASK = _CHUNK_SIZE - 1;
public ref struct Enumerator
{
private ref UnsafeSlotMap<T> _collection;
private int _currentIndex;
public readonly ref T Current => ref _collection._data[_currentIndex];
public Enumerator(ref UnsafeSlotMap<T> collection)
{
_collection = ref collection;
_currentIndex = -1;
}
public readonly ref T Current
{
get
{
var chunks = _collection._chunks;
var chunkIdx = _currentIndex >> _CHUNK_SHIFT;
var localIdx = _currentIndex & _CHUNK_MASK;
return ref chunks[chunkIdx][localIdx].value;
}
}
public bool MoveNext()
{
_currentIndex = _collection._validBits.NextSetBit(_currentIndex + 1);
@@ -68,18 +85,20 @@ public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
}
}
private UnsafeArray<T> _data;
private UnsafeArray<int> _generations;
private UnsafeArray<UnsafeArray<SlotEntry>> _chunks;
private UnsafeQueue<int> _freeSlots;
private UnsafeBitSet _validBits;
private AllocationHandle _handle;
private AllocationOption _allocationOption;
private int _count;
private int _capacity;
private int _nextSlotIndex;
public readonly int Count => _count;
public readonly int Capacity => _capacity;
public readonly bool IsCreated => _data.IsCreated && _generations.IsCreated && _freeSlots.IsCreated && _validBits.IsCreated;
public readonly bool IsCreated => _chunks.IsCreated && _freeSlots.IsCreated && _validBits.IsCreated;
/// <summary>
/// Initializes a new instance of UnsafeSlotMap with a default size of 1 and a persistent allocation handle.
@@ -104,19 +123,34 @@ public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
throw new ArgumentOutOfRangeException(nameof(capacity), "Capacity must be greater than zero.");
}
_data = new UnsafeArray<T>(capacity, handle, allocationOption);
_generations = new UnsafeArray<int>(capacity, handle, allocationOption);
_handle = handle;
_allocationOption = allocationOption;
var initialChunks = (capacity + _CHUNK_MASK) / _CHUNK_SIZE;
if (initialChunks == 0)
initialChunks = 1;
_capacity = initialChunks * _CHUNK_SIZE;
_chunks = new UnsafeArray<UnsafeArray<SlotEntry>>(initialChunks, handle, allocationOption);
for (var i = 0; i < initialChunks; i++)
{
_chunks[i] = new UnsafeArray<SlotEntry>(_CHUNK_SIZE, handle, allocationOption);
if (!allocationOption.HasFlag(AllocationOption.Clear))
{
_chunks[i].AsSpan().Clear();
}
}
_freeSlots = new UnsafeQueue<int>(capacity, handle, allocationOption);
_validBits = new UnsafeBitSet(capacity, handle, allocationOption);
_validBits = new UnsafeBitSet(_capacity, handle, allocationOption);
if (!allocationOption.HasFlag(AllocationOption.Clear))
{
_generations.AsSpan().Clear();
_validBits.ClearAll();
}
_count = 0;
_capacity = capacity;
_nextSlotIndex = 0;
}
/// <summary>
@@ -139,6 +173,33 @@ public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
return new Enumerator(ref this);
}
[MethodImpl(MethodImplOptions.NoInlining)]
private void EnsureChunkExists(int requiredChunkIndex)
{
if (requiredChunkIndex < _chunks.Length)
return;
var newChunkCount = _chunks.Length;
while (newChunkCount <= requiredChunkIndex)
{
newChunkCount *= 2;
}
_chunks.Resize(newChunkCount, _allocationOption);
for (var i = _capacity / _CHUNK_SIZE; i < newChunkCount; i++)
{
_chunks[i] = new UnsafeArray<SlotEntry>(_CHUNK_SIZE, _handle, _allocationOption);
if (!_allocationOption.HasFlag(AllocationOption.Clear))
{
_chunks[i].AsSpan().Clear();
}
}
_capacity = newChunkCount * _CHUNK_SIZE;
_validBits.Resize(_capacity, _allocationOption);
}
/// <summary>
/// Adds the specified item to the collection and returns the index of the slot where it was stored.
/// </summary>
@@ -147,28 +208,40 @@ public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
/// <returns>The index of the slot in which the item was stored.</returns>
public int Add(T item, out int generation)
{
if (_count >= _capacity)
if (_freeSlots.Count > 0)
{
Resize(Math.Max(1, _capacity * 2));
var slotIndex = _freeSlots.Dequeue();
var chunkIdx = slotIndex >> _CHUNK_SHIFT;
var localIdx = slotIndex & _CHUNK_MASK;
ref var slot = ref _chunks[chunkIdx][localIdx];
generation = slot.generation;
slot.value = item;
_validBits.SetBit(slotIndex);
_count++;
return slotIndex;
}
int index;
if (_freeSlots.Count == 0)
var newSlotIndex = _nextSlotIndex++;
var newChunkIdx = newSlotIndex >> _CHUNK_SHIFT;
var newLocalIdx = newSlotIndex & _CHUNK_MASK;
if (newChunkIdx >= _chunks.Length)
{
index = _count;
}
else
{
index = _freeSlots.Dequeue();
EnsureChunkExists(newChunkIdx);
}
_data[index] = item;
_validBits.SetBit(index);
ref var newSlot = ref _chunks[newChunkIdx][newLocalIdx];
newSlot.value = item;
newSlot.generation = 0;
_validBits.SetBit(newSlotIndex);
generation = 0;
_count++;
generation = _generations[index];
return index;
return newSlotIndex;
}
/// <summary>
@@ -181,23 +254,32 @@ public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
public bool Remove(int slotIndex, int generation, out T item)
{
item = default;
if (slotIndex < 0 || slotIndex >= _capacity)
if (slotIndex < 0)
{
return false;
}
ref var gen = ref _generations[slotIndex];
if (gen != generation)
var chunkIdx = slotIndex >> _CHUNK_SHIFT;
var localIdx = slotIndex & _CHUNK_MASK;
if (chunkIdx >= _chunks.Length)
{
return false;
}
item = _data[slotIndex];
ref var slot = ref _chunks[chunkIdx][localIdx];
gen++;
if (!_validBits.IsSet(slotIndex) || slot.generation != generation)
{
return false;
}
slot.generation++;
_validBits.ClearBit(slotIndex);
_freeSlots.Enqueue(slotIndex);
item = slot.value;
slot.value = default;
_freeSlots.Enqueue(slotIndex);
_count--;
return true;
@@ -223,17 +305,8 @@ public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
/// <returns>true if the slot at the specified index is valid and its Generation matches the specified value; otherwise, false.</returns>
public readonly bool Contains(int slotIndex, int generation)
{
if (slotIndex < 0 || slotIndex >= _capacity)
{
return false;
}
if (_validBits.IsSet(slotIndex) && _generations[slotIndex] == generation)
{
return true;
}
return false;
GetElementReferenceAt(slotIndex, generation, out var exist);
return exist;
}
/// <summary>
@@ -247,14 +320,17 @@ public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
/// <returns>true if the element at the specified slot index and Generation is found; otherwise, false.</returns>
public readonly bool TryGetElementAt(int slotIndex, int generation, out T value)
{
if (!Contains(slotIndex, generation))
ref var val = ref GetElementReferenceAt(slotIndex, generation, out var exist);
if (exist)
{
value = val;
return true;
}
else
{
value = default;
return false;
}
value = _data[slotIndex];
return true;
}
/// <summary>
@@ -267,12 +343,12 @@ public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
/// <exception cref="InvalidOperationException">Thrown when the specified slot is not occupied or the Generation does not match.</exception>
public readonly T GetElementAt(int slotIndex, int generation)
{
if (!Contains(slotIndex, generation))
if (!TryGetElementAt(slotIndex, generation, out var value))
{
throw new InvalidOperationException("The specified slot is not occupied or the generation does not match.");
}
return _data[slotIndex];
return value;
}
/// <summary>
@@ -283,50 +359,83 @@ public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
/// <param name="generation">The expected Generation value for the slot. Used to verify that the slot has not been recycled or replaced.</param>
/// <param name="exist">When this method returns, contains <see langword="true"/> if a valid element exists at the specified slot and Generation; otherwise, <see langword="false"/>.</param>
/// <returns>A reference to the element of type <typeparamref name="T"/> at the specified slot and Generation if it exists; otherwise, a null reference.</returns>
public ref T GetElementReferenceAt(int slotIndex, int generation, out bool exist)
public readonly ref T GetElementReferenceAt(int slotIndex, int generation, out bool exist)
{
if (!Contains(slotIndex, generation))
if (slotIndex < 0)
{
exist = false;
return ref Unsafe.NullRef<T>();
}
exist = true;
return ref _data[slotIndex];
var chunkIdx = slotIndex >> _CHUNK_SHIFT;
var localIdx = slotIndex & _CHUNK_MASK;
if (chunkIdx >= _chunks.Length)
{
exist = false;
return ref Unsafe.NullRef<T>();
}
ref var slot = ref _chunks[chunkIdx][localIdx];
if (_validBits.IsSet(slotIndex) && slot.generation == generation)
{
exist = true;
return ref slot.value;
}
exist = false;
return ref Unsafe.NullRef<T>();
}
public void Resize(int newSize, AllocationOption option = AllocationOption.None)
{
_data.Resize(newSize, option);
_generations.Resize(newSize, option | AllocationOption.Clear);
var requiredChunkIndex = (newSize + _CHUNK_MASK) / _CHUNK_SIZE - 1;
EnsureChunkExists(requiredChunkIndex);
_freeSlots.Resize(newSize, option);
_validBits.Resize(newSize, option);
_capacity = newSize;
}
public void Clear()
{
_generations.Clear();
for (var i = 0; i < _chunks.Length; i++)
{
if (_chunks[i].IsCreated)
{
var chunk = _chunks[i];
for (var slot = 0; slot < _CHUNK_SIZE; slot++)
{
chunk[slot].generation = 0;
chunk[slot].value = default;
}
}
}
_freeSlots.Clear();
_validBits.ClearAll();
_count = 0;
_nextSlotIndex = 0;
}
public readonly void* GetUnsafePtr()
{
return (T*)_data.GetUnsafePtr();
return null;
}
public void Dispose()
{
_data.Dispose();
_generations.Dispose();
for (var i = 0; i < _chunks.Length; i++)
{
if (_chunks[i].IsCreated)
{
_chunks[i].Dispose();
}
}
_chunks.Dispose();
_freeSlots.Dispose();
_validBits.Dispose();
_count = 0;
_capacity = 0;
_nextSlotIndex = 0;
}
}