Refactor unsafe collections and benchmarks
Changed the `CollectionBenchmark` class to use unsafe code for improved memory operations and added benchmarks for stack-allocated arrays. Changed the `ParallelNoiseBenchmark` class to remove the internal `NoiseJob` struct, promoting better organization. Changed the `AllocationManager` class to remove the lock mechanism for thread safety and simplified the `Reset` method. Changed the `Arena` and `DynamicArena` structs to include `Initialize` methods for better initialization control. Changed the `UnsafeArray<T>`, `UnsafeHashSet<T>`, and `UnsafeList<T>` structs to improve element access and management. Updated the `UnsafeCollectionExtensions` class to enhance usability with new methods for copying and converting collections. Updated the `MemoryLeakException` class to provide more detailed stack trace information for better debugging. Removed the usage of `UnsafeHashMap` in `Program.cs` and directly ran the `CollectionBenchmark`. Added a new `NoiseJob` struct in `NoiseJob.cs` for generating gradient noise using `UnsafeArray<float>`. Fixed minor typos and improved method signatures throughout the codebase for clarity.
This commit is contained in:
@@ -73,7 +73,7 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
|
||||
public readonly ref T this[int index]
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => ref UnsafeUtilities.ReadArrayElementRef<T>(_buffer, index);
|
||||
get => ref _buffer[index];
|
||||
}
|
||||
|
||||
public readonly bool IsCreated
|
||||
@@ -128,6 +128,7 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
|
||||
_allocator = Allocator.External;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Resize(int newSize)
|
||||
{
|
||||
if (newSize == _count)
|
||||
@@ -139,20 +140,28 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
|
||||
_count = newSize;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly void Clear()
|
||||
{
|
||||
MemClear(_buffer, (nuint)(_count * sizeof(T)));
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly void* GetUnsafePtr()
|
||||
{
|
||||
return _buffer;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Dispose()
|
||||
{
|
||||
if (!IsCreated)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
AllocationManager.Free(_buffer, _allocator);
|
||||
|
||||
_buffer = null;
|
||||
|
||||
@@ -49,7 +49,7 @@ public unsafe struct UnsafeHashSet<T> : IUnsafeCollection<T>, IEnumerable<T>
|
||||
public IEnumerator<T> GetEnumerator() => new Enumerator((HashMapHelper<T>*)UnsafeUtilities.AddressOf(ref _hashMap));
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
|
||||
public UnsafeHashSet(int capacity, Allocator allocator, AllocationOption allocationOption)
|
||||
public UnsafeHashSet(int capacity, Allocator allocator, AllocationOption allocationOption = AllocationOption.None)
|
||||
{
|
||||
_hashMap = new HashMapHelper<T>(capacity, 0, HashMapHelper<T>.MINIMAL_CAPACITY, allocator, allocationOption);
|
||||
}
|
||||
|
||||
@@ -118,10 +118,12 @@ public unsafe struct UnsafeList<T> : IUnsafeCollection<T>
|
||||
public readonly int Capacity => _array.Count;
|
||||
public readonly bool IsCreated => _array.IsCreated;
|
||||
|
||||
public readonly ref T this[int index]
|
||||
public readonly T this[int index]
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => ref _array[index];
|
||||
get => _array[index];
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
set => _array[index] = value;
|
||||
}
|
||||
|
||||
public IEnumerator<T> GetEnumerator() => new Enumerator((UnsafeList<T>*)UnsafeUtilities.AddressOf(ref this));
|
||||
|
||||
@@ -70,10 +70,12 @@ public unsafe struct UnsafeQueue<T> : IUnsafeCollection<T>
|
||||
public readonly int Capacity => _array.Count;
|
||||
public readonly bool IsCreated => _array.IsCreated;
|
||||
|
||||
public readonly ref T this[int index]
|
||||
public readonly T this[int index]
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => ref _array[index];
|
||||
get => _array[index];
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
set => _array[index] = value;
|
||||
}
|
||||
|
||||
public IEnumerator<T> GetEnumerator() => new Enumerator((UnsafeQueue<T>*)UnsafeUtilities.AddressOf(ref this));
|
||||
|
||||
Reference in New Issue
Block a user