Remove hlsf, TLSF.alloc is ok

This commit is contained in:
Youmu Liu
2026-05-11 18:08:35 +08:00
parent 5706c89563
commit b59d414e20
21 changed files with 267 additions and 1514 deletions
+1 -1
View File
@@ -50,7 +50,7 @@ namespace StgSharp
{
// TestRender();
/**/
HybridLayerSegregatedFitAllocator hlsf;
TwoLayerSegregatedFitAllocator hlsf;
unchecked {
hlsf = new(size, 32, size);
}
@@ -1,6 +1,6 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="HlsfStressTest"
// file="TlsfStressTest"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
@@ -30,9 +30,11 @@ using System;
using System.Collections.Generic;
using System.Diagnostics;
using Tlsf = StgSharp.HighPerformance.Memory.TwoLayerSegregatedFitAllocator;
namespace StgSharp
{
internal static class HlsfStressTest
internal static class TlsfStressTest
{
private const uint MaxAllocSize = 32 * 1024 * 1024; // 32MB documented cap
@@ -56,7 +58,7 @@ namespace StgSharp
Console.WriteLine();
Console.WriteLine("[1] Allocation throughput (no touch)");
using (HybridLayerSegregatedFitAllocator allocator = new HybridLayerSegregatedFitAllocator((nuint)ArenaBytes))
using (TwoLayerSegregatedFitAllocator allocator = new TwoLayerSegregatedFitAllocator((nuint)ArenaBytes))
{
Warmup(allocator);
@@ -75,7 +77,7 @@ namespace StgSharp
Console.WriteLine();
Console.WriteLine("[2] Allocation throughput (touch first byte)");
using (HybridLayerSegregatedFitAllocator allocator = new HybridLayerSegregatedFitAllocator((nuint)ArenaBytes))
using (TwoLayerSegregatedFitAllocator allocator = new TwoLayerSegregatedFitAllocator((nuint)ArenaBytes))
{
Warmup(allocator);
@@ -89,7 +91,7 @@ namespace StgSharp
}
}
Console.WriteLine();
using (HybridLayerSegregatedFitAllocator allocator = new HybridLayerSegregatedFitAllocator((nuint)ArenaBytes))
using (TwoLayerSegregatedFitAllocator allocator = new TwoLayerSegregatedFitAllocator((nuint)ArenaBytes))
{
Warmup(allocator);
@@ -99,7 +101,7 @@ namespace StgSharp
}
Console.WriteLine();
Console.WriteLine("[4] Alloc+Immediate Free pairs (5,000,000 x 256B)");
using (HybridLayerSegregatedFitAllocator allocator = new HybridLayerSegregatedFitAllocator((nuint)ArenaBytes))
using (TwoLayerSegregatedFitAllocator allocator = new TwoLayerSegregatedFitAllocator((nuint)ArenaBytes))
{
Warmup(allocator);
@@ -108,7 +110,7 @@ namespace StgSharp
}
Console.WriteLine();
Console.WriteLine("[5] Fragmentation cycle (allocate ~1024MB random, free half, reallocate)");
using (HybridLayerSegregatedFitAllocator allocator = new HybridLayerSegregatedFitAllocator((nuint)ArenaBytes))
using (TwoLayerSegregatedFitAllocator allocator = new TwoLayerSegregatedFitAllocator((nuint)ArenaBytes))
{
Warmup(allocator);
@@ -120,7 +122,7 @@ namespace StgSharp
}
private static void BenchAllocFreePairs(
HybridLayerSegregatedFitAllocator allocator,
TwoLayerSegregatedFitAllocator allocator,
int pairs,
uint blockSize
)
@@ -128,7 +130,7 @@ namespace StgSharp
Stopwatch sw = Stopwatch.StartNew();
for (int i = 0; i < pairs; i++)
{
HlsfHandle h = allocator.Alloc(blockSize);
Tlsf.Handle h = allocator.Alloc(blockSize);
allocator.Free(h);
}
sw.Stop();
@@ -139,7 +141,7 @@ namespace StgSharp
}
private static void BenchAllocThroughput(
HybridLayerSegregatedFitAllocator allocator,
TwoLayerSegregatedFitAllocator allocator,
uint blockSize,
bool touch
)
@@ -155,14 +157,14 @@ namespace StgSharp
return;
}
List<HlsfHandle> list = new List<HlsfHandle>((int)ops);
List<Tlsf.Handle> list = new List<Tlsf.Handle>((int)ops);
Stopwatch sw = Stopwatch.StartNew();
for (long i = 0; i < ops; i++)
{
HlsfHandle h = allocator.Alloc(blockSize);
Tlsf.Handle h = allocator.Alloc(blockSize);
if (touch)
{
Span<byte> span = h.BufferHandle;
Span<byte> span = h.AsSpan(10);
if (!span.IsEmpty) {
span[0] = (byte)(i & 0xFF);
}
@@ -173,7 +175,7 @@ namespace StgSharp
}
sw.Stop();
double allocOpsPerSec = ops / sw.Elapsed.TotalSeconds;
foreach (HlsfHandle h in list) {
foreach (Tlsf.Handle h in list) {
allocator.Free(h);
}
@@ -182,19 +184,19 @@ namespace StgSharp
}
private static void BenchFragmentationCycle(
HybridLayerSegregatedFitAllocator allocator,
TwoLayerSegregatedFitAllocator allocator,
int targetMiB
)
{
int targetBytes = targetMiB * 1024 * 1024;
Random rng = new Random(123);
List<HlsfHandle> live = new List<HlsfHandle>(128 * 1024);
List<Tlsf.Handle> live = new List<Tlsf.Handle>(128 * 1024);
long allocated = 0;
Stopwatch swA = Stopwatch.StartNew();
while (allocated < targetBytes)
{
uint sz = PickSize(rng);
HlsfHandle h = allocator.Alloc(sz);
Tlsf.Handle h = allocator.Alloc(sz);
live.Add(h);
allocated += sz;
}
@@ -218,7 +220,7 @@ namespace StgSharp
while (allocated < targetBytes / 2)
{
uint sz = PickSize(rng);
HlsfHandle h = allocator.Alloc(sz);
Tlsf.Handle h = allocator.Alloc(sz);
live.Add(h);
allocated += sz;
reCount++;
@@ -227,7 +229,7 @@ namespace StgSharp
Console.WriteLine($" phaseC re-alloc {reCount} in {swC.Elapsed.TotalSeconds:0.00}s ~{allocated / (1024.0 * 1024.0):0}MB");
Stopwatch swD = Stopwatch.StartNew();
foreach (HlsfHandle h in live) {
foreach (Tlsf.Handle h in live) {
allocator.Free(h);
}
@@ -236,12 +238,12 @@ namespace StgSharp
}
private static void BenchFreeThroughput(
HybridLayerSegregatedFitAllocator allocator,
TwoLayerSegregatedFitAllocator allocator,
int count,
uint blockSize
)
{
List<HlsfHandle> list = new List<HlsfHandle>(count);
List<Tlsf.Handle> list = new List<Tlsf.Handle>(count);
for (int i = 0; i < count; i++) {
list.Add(allocator.Alloc(blockSize));
}
@@ -282,15 +284,15 @@ namespace StgSharp
}
private static void Warmup(
HybridLayerSegregatedFitAllocator allocator
TwoLayerSegregatedFitAllocator allocator
)
{
List<HlsfHandle> tmp = new List<HlsfHandle>(4096);
List<Tlsf.Handle> tmp = new List<Tlsf.Handle>(4096);
for (int i = 0; i < 4096; i++) {
tmp.Add(allocator.Alloc(256));
}
foreach (HlsfHandle h in tmp) {
foreach (Tlsf.Handle h in tmp) {
allocator.Free(h);
}
}
+9 -6
View File
@@ -33,6 +33,8 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tlsf = StgSharp.HighPerformance.Memory.TwoLayerSegregatedFitAllocator;
namespace StgSharp
{
internal partial class Program
@@ -47,7 +49,7 @@ namespace StgSharp
// HLSF test arena
Console.WriteLine(
"=== HLSF (Hybrid Layer Segregated Fit Allocator) Single-threaded Stress Test ===\n");
HybridLayerSegregatedFitAllocator hlsfAllocator = new(size);
TwoLayerSegregatedFitAllocator hlsfAllocator = new(size);
// === 简洁交替测试:100次alloc与100次free交替进行,直到触发异常或达到最大循环 ===
Console.WriteLine("\n=== 简洁交替测试:100次alloc + 100次free 交替 ===");
@@ -80,7 +82,7 @@ namespace StgSharp
Random rnd = new Random(42);
for (int round = 1; round <= maxRounds; round++)
{
List<HlsfHandle> batch = new(maxCount);
List<Tlsf.Handle> batch = new(maxCount);
for (int i = 0; i < maxCount; i++)
{
uint sz = sizes[rnd.Next(sizes.Length)];
@@ -90,7 +92,7 @@ namespace StgSharp
}
for (int i = 5; i < maxCount; i += 10)
{
HlsfHandle origin = batch[i];
Tlsf.Handle origin = batch[i];
int index = i + offset[rnd.Next(offset.Length)];
batch[i] = batch[index];
batch[index] = origin;
@@ -104,9 +106,10 @@ namespace StgSharp
if (round % 100 == 0)
{
Console.WriteLine($"[交替测试] 已完成 {round} 轮 {maxCount}alloc+{maxCount}free");
hlsfAllocator.DumpPositionChain(false, 10);
hlsfAllocator.Collect();
hlsfAllocator.DumpPositionChain(false, 10);
// hlsfAllocator.DumpPositionChain(false, 10);
// hlsfAllocator.Collect();
// hlsfAllocator.DumpPositionChain(false, 10);
}
}
Console.WriteLine("[交替测试] 正常结束,无异常触发");