Files
Misaki.HighPerformance/Misaki.HighPerformance.Test/ParallelNoiseBenchmark.cs
Misaki 691a336111 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.
2025-04-11 15:53:11 +09:00

57 lines
1.5 KiB
C#

using BenchmarkDotNet.Attributes;
using Misaki.HighPerformance.Jobs;
using Misaki.HighPerformance.Test.Jobs;
using Misaki.HighPerformance.Unsafe.Collections;
using System.Numerics;
namespace Misaki.HighPerformance.Test;
[MemoryDiagnoser]
public class ParallelNoiseBenchmark
{
private const int _WIDTH = 512;
private const int _HEIGHT = 512;
private const int _LENGTH = _WIDTH * _HEIGHT;
[Benchmark]
public static void JobSystem()
{
using var buffers = new UnsafeArray<float>(_LENGTH, Allocator.Persistent, AllocationOption.None);
var job = new NoiseJob()
{
buffers = buffers,
width = _WIDTH,
height = _HEIGHT
};
using var handle = job.Schedule(_LENGTH, 64);
handle.WaitComplete();
}
[Benchmark]
public static void ParallelFor()
{
using var buffers = new UnsafeArray<float>(_LENGTH, Allocator.Persistent, AllocationOption.None);
Parallel.For(0, _LENGTH, i =>
{
var x = i % _WIDTH;
var y = i / _HEIGHT;
var uv = new Vector2(x, y);
buffers[i] = NoiseJob.GradientNoise(uv);
});
}
[Benchmark]
public static void For()
{
using var buffers = new UnsafeArray<float>(_LENGTH, Allocator.Persistent, AllocationOption.None);
for (var i = 0; i < _LENGTH; i++)
{
var x = i % _WIDTH;
var y = i / _HEIGHT;
var uv = new Vector2(x, y);
buffers[i] = NoiseJob.GradientNoise(uv);
}
}
}