Enhance memory management and performance benchmarks

Added a new configuration setting in `.editorconfig` to sort system directives last and increased the maximum line length to 400 characters.
Added a new static class `MathUtilities` in `MathUtilities.cs` with a method `CeilPow2` for computing powers of two.
Added a new benchmark class `CollectionBenchmark` in `CollectionBenchmark.cs` to measure performance of standard versus unsafe arrays.
Added a new benchmark class `HashCodeBenchmark` in `HashCodeBenchmark.cs` to evaluate hash code generation performance.
Added new utility methods in `UnsafeUtilities.cs` for memory allocation and deallocation, including `Malloc`, `AlignedAlloc`, `Realloc`, and `Free`.
Added a new `AllocationType` enum in `AllocationType.cs` to specify memory allocation types.

Changed the project file `Misaki.HighPerformance.Mathematics.csproj` to target .NET 9.0 and enable implicit usings and nullable reference types.
Changed the `ParallelNoiseBenchmark` class in `ParallelNoiseBenchmark.cs` to improve memory allocation strategies and performance.
Changed memory management in `Arena.cs` and `DynamicArena.cs` to use custom `Malloc` and `Free` functions.
Changed the `IUnsafeCollection` interface in `IUnsafeCollection.cs` to include new methods for resizing collections and obtaining unsafe pointers.
Changed the `UnsafeArray.cs` to improve management of unsafe arrays, including constructor and method updates.
Changed the `UnsafeHashMap` and `UnsafeHashSet` classes to enhance performance and memory management.
Changed the `UnsafeCollectionExtensions` class to provide additional methods for copying elements and converting collections.
Changed the `ObjectPool` class in `ObjectPool.cs` to simplify cleanup and remove auto-cleanup functionality.
Changed job scheduling and worker classes in `JobExtensions.cs` and `JobWorker.cs` to improve job scheduling in a thread pool.

Removed commented-out code in `Program.cs` related to previous testing methods.
Removed auto-cleanup functionality from the `ObjectPool` class.
This commit is contained in:
2025-04-03 09:13:07 +09:00
parent 060b4c9477
commit 48f2dce778
27 changed files with 1557 additions and 228 deletions

View File

@@ -0,0 +1,41 @@
using BenchmarkDotNet.Attributes;
using Misaki.HighPerformance.Unsafe.Collections;
using Misaki.HighPerformance.Unsafe.Collections.Services;
namespace Misaki.HighPerformance.Test;
[MemoryDiagnoser]
public class CollectionBenchmark
{
[Params(10, 100, 1000)]
public int count = 100;
[Benchmark]
public void Array()
{
for (var i = 0; i < count; i++)
{
var array = new int[count];
}
}
[Benchmark]
public void UnsafeArray()
{
for (var i = 0; i < count; i++)
{
var array = new UnsafeArray<int>(count, Allocator.Temp);
for (var j = 0; j < count; j++)
{
array[j] = j;
}
foreach (var item in array)
{
Console.WriteLine(item);
}
AllocationManager.Reset();
}
}
}

View File

@@ -0,0 +1,95 @@
using BenchmarkDotNet.Attributes;
using System.Numerics;
namespace Misaki.HighPerformance.Test;
public class HashCodeBenchmark
{
private struct Component
{
public int Value;
public int Value2;
public float Value3;
public Guid Value4;
public Matrix4x4 Value5;
public Vector4 Value6;
}
[Params(100, 1000, 10000)]
public int count;
private Component _component = new Component()
{
Value = 0,
Value2 = 1,
Value3 = 2,
Value4 = Guid.NewGuid(),
Value5 = Matrix4x4.Identity,
Value6 = Vector4.One
};
private Dictionary<Type, int> _hashCache = new();
//private UnsafeHashMap<Guid, int> _hashMap = new(16);
private bool _disposed;
//~HashCodeBenchmark()
//{
// Dispose();
//}
[Benchmark]
public void Hash()
{
for (var i = 0; i < count; i++)
{
_ = _component.GetHashCode();
}
}
[Benchmark]
public void HashWithCache()
{
for (var i = 0; i < count; i++)
{
var type = typeof(Component);
if (!_hashCache.TryGetValue(type, out var hash))
{
hash = type.GetHashCode();
_hashCache[type] = hash;
}
_ = hash;
}
}
//[Benchmark]
//public void HashWithUnsafeHashMap()
//{
// for (var i = 0; i < count; i++)
// {
// var type = _component.GetType();
// var guid = type.GUID;
// if (!_hashMap.TryGetValue(guid, out var hash))
// {
// hash = type.GetHashCode();
// _hashMap.Add(guid, hash);
// _hashMap.Test(ref _hashMap._hashMap);
// }
// _ = hash;
// }
//}
//public void Dispose()
//{
// if (_disposed)
// {
// return;
// }
// _hashMap.Dispose();
// GC.SuppressFinalize(this);
//}
}

View File

@@ -6,6 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishAot>True</PublishAot>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>

View File

@@ -21,12 +21,6 @@ public class ParallelNoiseBenchmark
return x - MathF.Truncate(x);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static float Lerp(float a, float b, float t)
{
return a + t * (b - a);
}
private static Vector2 GradientNoiseDirect(Vector2 uv)
{
uv.X %= 289;
@@ -48,7 +42,7 @@ public class ParallelNoiseBenchmark
var d11 = Vector2.Dot(GradientNoiseDirect(ip + new Vector2(1, 1)), fp - new Vector2(1, 1));
fp = fp * fp * fp * (fp * (fp * new Vector2(6.0f) - new Vector2(15.0f)) + new Vector2(10.0f));
return Lerp(Lerp(d00, d10, fp.Y), Lerp(d01, d11, fp.Y), fp.X);
return float.Lerp(float.Lerp(d00, d10, fp.Y), float.Lerp(d01, d11, fp.Y), fp.X);
}
public void Execute(int index)
@@ -65,9 +59,9 @@ public class ParallelNoiseBenchmark
private const int _LENGTH = _WIDTH * _HEIGHT;
[Benchmark]
public void JobSystem()
public static void JobSystem()
{
using var buffers = new UnsafeArray<float>(_LENGTH, AllocationType.UnInitialized);
using var buffers = new UnsafeArray<float>(_LENGTH, Allocator.Persistent, AllocationType.UnInitialized);
var job = new NoiseJob()
{
buffers = buffers,
@@ -80,9 +74,9 @@ public class ParallelNoiseBenchmark
}
[Benchmark]
public void ParallelFor()
public static void ParallelFor()
{
using var buffers = new UnsafeArray<float>(_LENGTH, AllocationType.UnInitialized);
using var buffers = new UnsafeArray<float>(_LENGTH, Allocator.Persistent, AllocationType.UnInitialized);
Parallel.For(0, _LENGTH, i =>
{
@@ -94,9 +88,9 @@ public class ParallelNoiseBenchmark
}
[Benchmark]
public void For()
public static void For()
{
using var buffers = new UnsafeArray<float>(_LENGTH, AllocationType.UnInitialized);
using var buffers = new UnsafeArray<float>(_LENGTH, Allocator.Persistent, AllocationType.UnInitialized);
for (var i = 0; i < _LENGTH; i++)
{
var x = i % _WIDTH;

View File

@@ -1,37 +1,7 @@
//using BenchmarkDotNet.Running;
//using Misaki.HighPerformance.Test;
using Misaki.HighPerformance.Test;
using Misaki.HighPerformance.Unsafe.Collections.Services;
//BenchmarkRunner.Run<ParallelNoiseBenchmark>();
using Misaki.HighPerformance.Unsafe.Collections;
using var test = new UnsafeArray<Test>(10, AllocationType.UnInitialized);
for (var i = 0; i < 10; i++)
{
var t = new Test();
t.buffers[0] = i;
test[i] = t;
}
test.ReAlloc(20);
for (var i = 0; i < 10; i++)
{
Console.WriteLine(test[i].buffers[0]);
}
struct Test : IDisposable
{
public UnsafeArray<float> buffers;
public Test()
{
buffers = new UnsafeArray<float>(1, AllocationType.UnInitialized);
}
public void Dispose()
{
buffers.Dispose();
}
}
AllocationManager.Initialize(512_000);
var test = new CollectionBenchmark();
test.UnsafeArray();
AllocationManager.Dispose();