Improve memory safety and alignment handling
Some checks failed
Publish NuGet Packages / publish (pull_request) Failing after 1m5s

- Updated `.gitignore` to include `.vscode/` and clarified comments.
- Introduced `SafeHandle` for managing memory alignment and safe access.
- Refactored `UnsafeArray<T>` to add bounds checking and alignment logic.
- Added `IUnsafeHashCollection<T>` for specialized hash-based collections.
- Refactored `UnsafeHashMap<TKey, TValue>` and `UnsafeHashSet<T>` to use `HashMapHelper<TKey>` with alignment support.
- Made `UnsafeSlotMap<T>` methods `readonly` for immutability.
- Enhanced `HashMapHelper<TKey>` with alignment-aware buffer management and validation.
- Updated benchmarks to use `UnsafeArray<Vector256<int>>` and added capacity checks.
- Incremented assembly version to `1.1.3` in `Misaki.HighPerformance.LowLevel.csproj`.
- Updated `Program.cs` to run `CollectionBenchmark` and demonstrate safe disposal handling.
This commit is contained in:
2025-11-18 01:25:40 +09:00
parent 57725369f9
commit c0a0861897
13 changed files with 309 additions and 140 deletions

View File

@@ -1,55 +1,49 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Attributes;
using Misaki.HighPerformance.LowLevel.Buffer;
using Misaki.HighPerformance.LowLevel.Collections;
using System.Runtime.Intrinsics;
namespace Misaki.HighPerformance.Test.Benchmark;
[MemoryDiagnoser]
public unsafe class CollectionBenchmark
public class CollectionBenchmark
{
private UnsafeArray<Vector256<int>> _array;
[Params(10, 100, 1000)]
public int count;
[GlobalSetup]
public void Setup()
{
}
[Benchmark]
public void Array()
{
var array = new int[count];
for (var i = 0; i < count; i++)
{
array[i] = i;
}
}
[Benchmark(Baseline = true)]
public void UnsafeArray()
{
var array = new UnsafeArray<int>(count, Allocator.Temp);
for (var i = 0; i < count; i++)
{
array[i] = i;
}
AllocationManager.ResetTempAllocator();
}
[Benchmark]
public void StackArray()
{
var array = stackalloc int[count];
for (var i = 0; i < count; i++)
{
array[i] = i;
}
_array = new UnsafeArray<Vector256<int>>(count, Allocator.Persistent);
}
[GlobalCleanup]
public void Cleanup()
{
AllocationManager.Dispose();
_array.Dispose();
}
[Benchmark]
public void WithCapacityChecks()
{
for (var i = 0; i < _array.Count; i++)
{
if (i < 0 || i >= _array.Count)
{
throw new IndexOutOfRangeException();
}
_array[i] = default;
}
}
[Benchmark]
public void WithoutCapacityChecks()
{
for (var i = 0; i < _array.Count; i++)
{
_array[i] = default;
}
}
}