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:
@@ -5,28 +5,45 @@ using Misaki.HighPerformance.Unsafe.Collections;
|
||||
namespace Misaki.HighPerformance.Test;
|
||||
|
||||
[MemoryDiagnoser]
|
||||
public class CollectionBenchmark
|
||||
public unsafe class CollectionBenchmark
|
||||
{
|
||||
[Params(10, 100, 1000)]
|
||||
public int count = 100;
|
||||
public int count;
|
||||
|
||||
[GlobalSetup]
|
||||
public void Setup()
|
||||
{
|
||||
AllocationManager.Initialize(512_000);
|
||||
AllocationManager.Initialize();
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public void Array()
|
||||
{
|
||||
var array = new int[count];
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
array[i] = i;
|
||||
}
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
[Benchmark(Baseline = true)]
|
||||
public void UnsafeArray()
|
||||
{
|
||||
var array = new UnsafeArray<int>(count, Allocator.Temp);
|
||||
AllocationManager.Reset();
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
array[i] = i;
|
||||
}
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public void StackArray()
|
||||
{
|
||||
var array = stackalloc int[count];
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
array[i] = i;
|
||||
}
|
||||
}
|
||||
|
||||
[GlobalCleanup]
|
||||
|
||||
50
Misaki.HighPerformance.Test/Jobs/NoiseJob.cs
Normal file
50
Misaki.HighPerformance.Test/Jobs/NoiseJob.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using Misaki.HighPerformance.Jobs;
|
||||
using Misaki.HighPerformance.Unsafe.Collections;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Misaki.HighPerformance.Test.Jobs;
|
||||
internal struct NoiseJob : IJobParallelFor
|
||||
{
|
||||
public UnsafeArray<float> buffers;
|
||||
public int width;
|
||||
public int height;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static float Frac(float x)
|
||||
{
|
||||
return x - MathF.Truncate(x);
|
||||
}
|
||||
|
||||
private static Vector2 GradientNoiseDirect(Vector2 uv)
|
||||
{
|
||||
uv.X %= 289;
|
||||
uv.Y %= 289;
|
||||
var x = (34 * uv.X + 1) * uv.X % 289 + uv.Y;
|
||||
x = (34 * x + 1) * x % 289;
|
||||
x = Frac(x / 41) * 2 - 1;
|
||||
return Vector2.Normalize(new Vector2(x - MathF.Floor(x + 0.5f), MathF.Abs(x) - 0.5f));
|
||||
}
|
||||
|
||||
public static float GradientNoise(Vector2 uv)
|
||||
{
|
||||
var ip = new Vector2(MathF.Floor(uv.X), MathF.Floor(uv.Y));
|
||||
var fp = new Vector2(Frac(uv.X), Frac(uv.Y));
|
||||
|
||||
var d00 = Vector2.Dot(GradientNoiseDirect(ip), fp);
|
||||
var d01 = Vector2.Dot(GradientNoiseDirect(ip + new Vector2(0, 1)), fp - new Vector2(0, 1));
|
||||
var d10 = Vector2.Dot(GradientNoiseDirect(ip + new Vector2(1, 0)), fp - new Vector2(1, 0));
|
||||
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 float.Lerp(float.Lerp(d00, d10, fp.Y), float.Lerp(d01, d11, fp.Y), fp.X);
|
||||
}
|
||||
|
||||
public void Execute(int index)
|
||||
{
|
||||
var x = index % width;
|
||||
var y = index / height;
|
||||
var uv = new Vector2(x, y);
|
||||
buffers[index] = GradientNoise(uv);
|
||||
}
|
||||
}
|
||||
@@ -1,59 +1,14 @@
|
||||
using BenchmarkDotNet.Attributes;
|
||||
using Misaki.HighPerformance.Jobs;
|
||||
using Misaki.HighPerformance.Test.Jobs;
|
||||
using Misaki.HighPerformance.Unsafe.Collections;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Misaki.HighPerformance.Test;
|
||||
|
||||
[MemoryDiagnoser]
|
||||
public class ParallelNoiseBenchmark
|
||||
{
|
||||
private struct NoiseJob : IJobParallelFor
|
||||
{
|
||||
public UnsafeArray<float> buffers;
|
||||
public int width;
|
||||
public int height;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static float Frac(float x)
|
||||
{
|
||||
return x - MathF.Truncate(x);
|
||||
}
|
||||
|
||||
private static Vector2 GradientNoiseDirect(Vector2 uv)
|
||||
{
|
||||
uv.X %= 289;
|
||||
uv.Y %= 289;
|
||||
var x = (34 * uv.X + 1) * uv.X % 289 + uv.Y;
|
||||
x = (34 * x + 1) * x % 289;
|
||||
x = Frac(x / 41) * 2 - 1;
|
||||
return Vector2.Normalize(new Vector2(x - MathF.Floor(x + 0.5f), MathF.Abs(x) - 0.5f));
|
||||
}
|
||||
|
||||
public static float GradientNoise(Vector2 uv)
|
||||
{
|
||||
var ip = new Vector2(MathF.Floor(uv.X), MathF.Floor(uv.Y));
|
||||
var fp = new Vector2(Frac(uv.X), Frac(uv.Y));
|
||||
|
||||
var d00 = Vector2.Dot(GradientNoiseDirect(ip), fp);
|
||||
var d01 = Vector2.Dot(GradientNoiseDirect(ip + new Vector2(0, 1)), fp - new Vector2(0, 1));
|
||||
var d10 = Vector2.Dot(GradientNoiseDirect(ip + new Vector2(1, 0)), fp - new Vector2(1, 0));
|
||||
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 float.Lerp(float.Lerp(d00, d10, fp.Y), float.Lerp(d01, d11, fp.Y), fp.X);
|
||||
}
|
||||
|
||||
public void Execute(int index)
|
||||
{
|
||||
var x = index % width;
|
||||
var y = index / height;
|
||||
var uv = new Vector2(x, y);
|
||||
buffers[index] = GradientNoise(uv);
|
||||
}
|
||||
}
|
||||
|
||||
private const int _WIDTH = 512;
|
||||
private const int _HEIGHT = 512;
|
||||
private const int _LENGTH = _WIDTH * _HEIGHT;
|
||||
|
||||
@@ -1,10 +1,4 @@
|
||||
using Misaki.HighPerformance.Unsafe.Collections;
|
||||
using Misaki.HighPerformance.Unsafe.Helpers;
|
||||
using System.Numerics;
|
||||
using BenchmarkDotNet.Running;
|
||||
using Misaki.HighPerformance.Test;
|
||||
|
||||
unsafe
|
||||
{
|
||||
Console.WriteLine(sizeof(UnsafeHashMap<int, float>));
|
||||
Console.WriteLine(MemoryUtilities.AlignOf<UnsafeHashMap<int, float>>());
|
||||
Console.WriteLine(1 << Math.Min(3, BitOperations.TrailingZeroCount(sizeof(UnsafeHashMap<int, float>))));
|
||||
}
|
||||
BenchmarkRunner.Run<CollectionBenchmark>();
|
||||
|
||||
Reference in New Issue
Block a user