hlsf wrong slicing solving

This commit is contained in:
Youmu Liu
2025-11-26 13:49:31 +08:00
parent 5b09160049
commit 23914f05ac
7 changed files with 275 additions and 142 deletions
+37 -22
View File
@@ -25,10 +25,6 @@
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="HlsfStressTest" (performance benchmark)
// Project: StgSharp
// -----------------------------------------------------------------------
using StgSharp.HighPerformance.Memory;
using System;
using System.Collections.Generic;
@@ -46,7 +42,7 @@ namespace StgSharp
int.TryParse(mbStr, out int mb) &&
mb >= 64 ?
(ulong)mb * 1024UL * 1024UL :
3UL * 1024UL * 1024UL * 1024UL;
512UL * 1024UL * 1024UL; // lower default to 512MB
private static readonly uint[] SizeSweep = {
64, 128, 192, 256, 512, 768, 1024, 2048, 3072, 4096, 8192, 12288, 16384, 32768, 49152, 65536, 131072, 196608, 262144, 524288, 786432, 1_048_576, 2_097_152, 3_145_728, 4_194_304, 8_388_608, 12_582_912, 16_777_216, 25_165_824, 33_554_432
@@ -56,42 +52,61 @@ namespace StgSharp
{
Console.WriteLine("=== HLSF Allocator Single-Thread Performance Benchmark ===");
Console.WriteLine($"Arena size: {ArenaBytes / (1024 * 1024)} MiB (env override: HLSF_ARENA_MB)");
using HybridLayerSegregatedFitAllocator allocator = new HybridLayerSegregatedFitAllocator((nuint)ArenaBytes);
Warmup(allocator);
Console.WriteLine();
Console.WriteLine("[1] Allocation throughput (no touch)");
foreach (uint sz in SizeSweep)
using (HybridLayerSegregatedFitAllocator allocator = new HybridLayerSegregatedFitAllocator((nuint)ArenaBytes))
{
uint size = sz > MaxAllocSize ? MaxAllocSize : sz;
BenchAllocThroughput(allocator, size, touch:false);
GC.Collect();
GC.WaitForPendingFinalizers();
Warmup(allocator);
foreach (uint sz in SizeSweep)
{
uint size = sz > MaxAllocSize ? MaxAllocSize : sz;
BenchAllocThroughput(allocator, size, touch:false);
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
Console.WriteLine();
Console.WriteLine("[2] Allocation throughput (touch first byte)");
foreach (uint sz in SizeSweep)
using (HybridLayerSegregatedFitAllocator allocator = new HybridLayerSegregatedFitAllocator((nuint)ArenaBytes))
{
uint size = sz > MaxAllocSize ? MaxAllocSize : sz;
BenchAllocThroughput(allocator, size, touch:true);
GC.Collect();
GC.WaitForPendingFinalizers();
Warmup(allocator);
foreach (uint sz in SizeSweep)
{
uint size = sz > MaxAllocSize ? MaxAllocSize : sz;
BenchAllocThroughput(allocator, size, touch:true);
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
Console.WriteLine();
Console.WriteLine("[3] Free throughput after pre-allocation (256k blocks of 1KB)");
BenchFreeThroughput(allocator, 256 * 1024, 1024);
using (HybridLayerSegregatedFitAllocator allocator = new HybridLayerSegregatedFitAllocator((nuint)ArenaBytes))
{
Warmup(allocator);
Console.WriteLine("[3] Free throughput after pre-allocation (256k blocks of 1KB)");
BenchFreeThroughput(allocator, 256 * 1024, 1024);
}
Console.WriteLine();
Console.WriteLine("[4] Alloc+Immediate Free pairs (5,000,000 x 256B)");
BenchAllocFreePairs(allocator, 5_000_000, 256);
using (HybridLayerSegregatedFitAllocator allocator = new HybridLayerSegregatedFitAllocator((nuint)ArenaBytes))
{
Warmup(allocator);
BenchAllocFreePairs(allocator, 5_000_000, 256);
}
Console.WriteLine();
Console.WriteLine("[5] Fragmentation cycle (allocate ~1024MB random, free half, reallocate)");
BenchFragmentationCycle(allocator, 1024);
using (HybridLayerSegregatedFitAllocator allocator = new HybridLayerSegregatedFitAllocator((nuint)ArenaBytes))
{
Warmup(allocator);
BenchFragmentationCycle(allocator, 512); // target within 500MB arena
}
Console.WriteLine();
Console.WriteLine("=== Benchmark Complete ===");
}
-22
View File
@@ -25,30 +25,8 @@
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using StgSharp.Collections;
using StgSharp.Controlling.UsrActivity;
using StgSharp.Geometries;
using StgSharp.Graphics;
using StgSharp.HighPerformance;
using StgSharp.HighPerformance.Memory;
using StgSharp.Internal;
using StgSharp.Mathematics;
using StgSharp.Mathematics.Graphic;
using StgSharp.Mathematics.Numeric;
using StgSharp.Model.Step;
using StgSharp.MVVM;
using StgSharp.MVVM.ViewModel;
using StgSharp.Script;
using StgSharp.Script.Express;
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
using System.Threading;
using System.Xml.XPath;
namespace StgSharp
{
+82
View File
@@ -0,0 +1,82 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="SlabStressTest"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using StgSharp.HighPerformance.Memory;
using System;
using System.Diagnostics;
namespace StgSharp
{
internal static class SlabStressTest
{
// Exercise the Nonconcurrent Chunked SLAB used by HLSF (allocation/free only)
public static unsafe void Run()
{
Console.WriteLine("=== SLAB Allocator (Chunked) Single-Thread Stress Test ===");
// Use SLAB with element size of pointer (nuint) to simulate Entry handles
SlabAllocator<BunchOfBytes> slab = SlabAllocator<BunchOfBytes>.Create(64, SlabBufferLayout.Chunked, false);
int cycles = 10;
int allocsPerCycle = 200_000; // large enough to force internal growth
Stopwatch sw = new Stopwatch();
for (int c = 0; c < cycles; c++)
{
Console.WriteLine($"--- Cycle {c+1} ---");
nuint[] ptrs = new nuint[allocsPerCycle];
sw.Restart();
for (int i = 0; i < allocsPerCycle; i++) {
ptrs[i] = slab.Allocate();
}
sw.Stop();
Console.WriteLine($"Allocate: {allocsPerCycle} in {sw.Elapsed.TotalSeconds:0.00}s");
sw.Restart();
for (int i = 0; i < allocsPerCycle; i++) {
slab.Free(ptrs[i]);
}
sw.Stop();
Console.WriteLine($"Free: {allocsPerCycle} in {sw.Elapsed.TotalSeconds:0.00}s");
}
slab.Dispose();
Console.WriteLine("=== SLAB Stress Complete ===");
}
internal unsafe struct BunchOfBytes
{
public fixed byte Data[128];
}
}
}