Refactor core APIs, fix bugs, and improve safety

- Make image result/info structs readonly; improve error handling and memory safety in image library
- Introduce IJobScheduler interface; move job scheduling docs to interface
- Remove "index 0 invalid" convention from slot/sparse maps; fix Count logic
- Add Owner<T> for disposable value types in low-level utilities
- Improve ObjectPool<T> thread safety and logic
- Change List<T>.RemoveAndSwapBack to return bool
- Remove unsafe methods from generated math types; add debug range checks
- Update benchmarks and enable collection checks in tests
- Improve documentation, comments, and error messages
- Bump assembly versions across all projects
This commit is contained in:
2025-12-21 16:08:10 +09:00
parent a1ad0bd2da
commit 1fee890329
38 changed files with 1967 additions and 350 deletions

View File

@@ -2,58 +2,18 @@
using BenchmarkDotNet.Attributes;
using Misaki.HighPerformance.Mathematics;
using Misaki.HighPerformance.Test.Jobs;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;
namespace Misaki.HighPerformance.Test.Benchmark;
public unsafe class MathematicsBenchmark
public class MathematicsBenchmark
{
public struct f2
{
public float x;
public float y;
public f2(float x, float y)
{
//this = Asf2(Vector128.Create(x, y, 0, 0));
this.x = x;
this.y = y;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector128<float> AsVector128Unsafe(f2 value)
{
return Vector128.Create(value.x, value.y, 0, 0);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static f2 Asf2(Vector128<float> value)
{
//f2 result;
//result.x = value.GetElement(0);
//result.y = value.GetElement(1);
//return result;
ref byte address = ref Unsafe.As<Vector128<float>, byte>(ref value);
return Unsafe.ReadUnaligned<f2>(ref address);
}
public static f2 operator +(f2 lhs, f2 rhs)
{
//return Asf2(AsVector128Unsafe(lhs) + AsVector128Unsafe(rhs));
return new f2(lhs.x + rhs.x, lhs.y + rhs.y);
}
}
#if VECTOR_BENCHMARK
private Vector2 _v2a = new Vector2(1, 2);
private Vector2 _v2b = new Vector2(3, 4);
private f2 _f2a = new f2(1, 2);
private f2 _f2b = new f2(3, 4);
private float2 _f2a = new float2(1, 2);
private float2 _f2b = new float2(3, 4);
[Benchmark]
public Vector2 VectorAdd()
@@ -69,9 +29,9 @@ public unsafe class MathematicsBenchmark
}
[Benchmark]
public f2 f2Add()
public float2 float2Add()
{
var v = new f2(0, 0);
var v = new float2(0, 0);
for (var i = 0; i < 10; i++)
{

View File

@@ -41,7 +41,7 @@ public class ParallelNoiseBenchmark
height = _HEIGHT
};
var handle = _jobScheduler.ScheduleParallel(ref job, _LENGTH, 64, -1);
var handle = _jobScheduler.ScheduleParallel(ref job, _LENGTH, 64, -1, JobHandle.Invalid);
_jobScheduler.WaitComplete(handle);
}

View File

@@ -9,7 +9,9 @@
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DefineConstants>$(DefineConstants);ENABLE_COLLECTION_CHECKS</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'" />

View File

@@ -1,28 +1,6 @@
//var threadCount = 8;
//var map = new ConcurrentSlotMap<int>();
using Misaki.HighPerformance.Image;
//var barrier = new Barrier(threadCount);
//Parallel.For(0, threadCount, threadIndex =>
//{
// barrier.SignalAndWait();
// for (var i = 0; i < 1000; i++)
// {
// var id = map.Add(i + threadIndex * 1000, out var gen);
// if (i % 100 == 0)
// {
// map.Remove(id, gen);
// }
// }
//});
//Console.WriteLine($"Count should be {threadCount * 990}, actual: {map.Count}");
//using Misaki.HighPerformance.LowLevel;
using System.Runtime.Intrinsics;
BenchmarkDotNet.Running.BenchmarkRunner.Run<Misaki.HighPerformance.Test.Benchmark.MathematicsBenchmark>();
//BenchmarkDotNet.Running.BenchmarkRunner.Run<Misaki.HighPerformance.Test.Benchmark.ParallelNoiseBenchmark>();
//using Misaki.HighPerformance.Collections;
//using Misaki.HighPerformance.LowLevel.Buffer;
@@ -59,3 +37,12 @@ BenchmarkDotNet.Running.BenchmarkRunner.Run<Misaki.HighPerformance.Test.Benchmar
// 8, 7, 6, 5,
// 4, 3, 2, 1);
//Console.WriteLine(Matrix4x4.Multiply(ma, mb));
const string _IMAGE_PATH = "C:/Users/Misaki/Downloads/Im/119683453_p2.jpg";
using var stream = File.OpenRead(_IMAGE_PATH);
var imageInfo = ImageInfo.FromStream(stream);
using var image = ImageResult.FromStream(stream);
Console.WriteLine($"{imageInfo.Width}x{imageInfo.Height} {imageInfo.ColorComponents}");