This commit is contained in:
2026-03-30 12:47:29 +09:00
parent 04dd7222d9
commit 8231d6df60
45 changed files with 2497 additions and 707 deletions

View File

@@ -1,5 +1,4 @@
using Misaki.HighPerformance.LowLevel.Buffer;
using System.Diagnostics;
namespace Misaki.HighPerformance.Test.UnitTest.Buffer;
@@ -10,28 +9,28 @@ public unsafe class TestFreeList
public void SingleThreadedAllocFreeTest()
{
using var freeList = new FreeList(8, 1024);
// Allocate various sizes
void* p1 = freeList.Allocate(16, 8);
void* p2 = freeList.Allocate(32, 8);
void* p3 = freeList.Allocate(64, 8);
var p1 = freeList.Allocate(16, 8);
var p2 = freeList.Allocate(32, 8);
var p3 = freeList.Allocate(64, 8);
Assert.IsTrue(p1 != null);
Assert.IsTrue(p2 != null);
Assert.IsTrue(p3 != null);
// Free them
freeList.Free(p1);
freeList.Free(p2);
freeList.Free(p3);
// Allocate again - should reuse from buckets (or at least succeed)
void* p4 = freeList.Allocate(16, 8);
void* p5 = freeList.Allocate(32, 8);
var p4 = freeList.Allocate(16, 8);
var p5 = freeList.Allocate(32, 8);
Assert.IsTrue(p4 != null);
Assert.IsTrue(p5 != null);
freeList.Free(p4);
freeList.Free(p5);
}
@@ -42,23 +41,25 @@ public unsafe class TestFreeList
const int threadCount = 8;
const int iterations = 1000;
using var freeList = new FreeList(8, 64 * 1024, threadCount);
var threads = new Thread[threadCount];
for (int i = 0; i < threadCount; i++)
for (var i = 0; i < threadCount; i++)
{
threads[i] = new Thread(() =>
{
for (int j = 0; j < iterations; j++)
for (var j = 0; j < iterations; j++)
{
void* ptr = freeList.Allocate(16, 8);
var ptr = freeList.Allocate(16, 8);
Assert.IsTrue(ptr != null);
freeList.Free(ptr);
}
});
}
foreach (var t in threads) t.Start();
foreach (var t in threads) t.Join();
foreach (var t in threads)
t.Start();
foreach (var t in threads)
t.Join();
}
[TestMethod]
@@ -68,27 +69,27 @@ public unsafe class TestFreeList
const int consumerCount = 4;
const int iterations = 5000;
using var freeList = new FreeList(8, 64 * 1024, producerCount + consumerCount);
var queue = new System.Collections.Concurrent.ConcurrentQueue<IntPtr>();
var producers = new Thread[producerCount];
var consumers = new Thread[consumerCount];
bool producing = true;
for (int i = 0; i < producerCount; i++)
var producing = true;
for (var i = 0; i < producerCount; i++)
{
producers[i] = new Thread(() =>
{
for (int j = 0; j < iterations; j++)
for (var j = 0; j < iterations; j++)
{
void* ptr = freeList.Allocate(32, 8);
var ptr = freeList.Allocate(32, 8);
Assert.IsTrue(ptr != null);
queue.Enqueue((IntPtr)ptr);
}
});
}
for (int i = 0; i < consumerCount; i++)
for (var i = 0; i < consumerCount; i++)
{
consumers[i] = new Thread(() =>
{
@@ -106,12 +107,16 @@ public unsafe class TestFreeList
});
}
foreach (var t in producers) t.Start();
foreach (var t in consumers) t.Start();
foreach (var t in producers) t.Join();
foreach (var t in producers)
t.Start();
foreach (var t in consumers)
t.Start();
foreach (var t in producers)
t.Join();
Volatile.Write(ref producing, false);
foreach (var t in consumers) t.Join();
foreach (var t in consumers)
t.Join();
}
[TestMethod]
@@ -120,32 +125,34 @@ public unsafe class TestFreeList
// Set maxConcurrencyLevel to 1, but use more threads
const int threadCount = 5;
using var freeList = new FreeList(8, 1024, 1);
var threads = new Thread[threadCount];
for (int i = 0; i < threadCount; i++)
for (var i = 0; i < threadCount; i++)
{
threads[i] = new Thread(() =>
{
void* ptr = freeList.Allocate(16, 8);
var ptr = freeList.Allocate(16, 8);
Assert.IsTrue(ptr != null);
freeList.Free(ptr);
});
}
foreach (var t in threads) t.Start();
foreach (var t in threads) t.Join();
foreach (var t in threads)
t.Start();
foreach (var t in threads)
t.Join();
}
[TestMethod]
public void LargeAllocationTest()
{
using var freeList = new FreeList(8, 1024);
// Allocate larger than default chunk size
nuint largeSize = 2048;
void* ptr = freeList.Allocate(largeSize, 8);
var ptr = freeList.Allocate(largeSize, 8);
Assert.IsTrue(ptr != null);
freeList.Free(ptr);
}
}