switch root namespace name to NITLOAD

This commit is contained in:
Youmu Liu
2026-09-02 18:15:03 +08:00
parent 4218baad48
commit 7c7101ee48
502 changed files with 710 additions and 721 deletions
+192
View File
@@ -0,0 +1,192 @@
// -----------------------------------------------------------------------------
// file="HlsfAllocatorBench"
// Project: StgSharp
// Copyright (c) Nitload.
// SPDX-License-Identifier: MIT
// -----------------------------------------------------------------------------
using BenchmarkDotNet.Attributes;
using Nitload.Common.HighPerformance.Memory;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Tlsf = Nitload.Common.HighPerformance.Memory.TwoLayerSegregatedFitAllocator;
namespace Nitload.Benchmark
{
[MemoryDiagnoser]
public class TlsfAllocatorBench
{
private const uint Alignment = 64;
private const int AllocationCount = 2048;
private const int BatchCount = 256;
private const int TotalOperationsPerInvoke = BatchCount * AllocationCount;
private static readonly int[] AllocSizes = [
64,
100,
200,
400,
1024,
4096
];
private readonly int[] _freeOrderByBatch = new int[TotalOperationsPerInvoke];
private readonly uint[] _requestSizesByBatch = new uint[TotalOperationsPerInvoke];
private readonly List<nint> _libcLocalList = new();
private readonly List<Tlsf.Handle> _tlsfLocalList = new();
private Random _random = new(42);
private TwoLayerSegregatedFitAllocator _tlsfAllocator = null!;
[GlobalCleanup]
public void Cleanup()
{
_tlsfAllocator?.Dispose();
_tlsfLocalList.Clear();
_tlsfLocalList.TrimExcess();
_libcLocalList.Clear();
_libcLocalList.TrimExcess();
}
[IterationCleanup]
public void IterationCleanup()
{
_tlsfAllocator?.Dispose();
_tlsfAllocator = null!;
_tlsfLocalList.Clear();
_libcLocalList.Clear();
}
[IterationSetup]
public void IterationSetup()
{
_tlsfAllocator?.Dispose();
_random = new Random(42);
_tlsfAllocator = new TwoLayerSegregatedFitAllocator(GetArenaBytes(), align:(int)Alignment);
BuildIterationWorkload();
_tlsfLocalList.Clear();
_libcLocalList.Clear();
}
[Benchmark(Baseline = true, OperationsPerInvoke = TotalOperationsPerInvoke)]
public unsafe void Libc()
{
for (int batch = 0; batch < BatchCount; batch++) {
RunLibc(batch);
}
}
[Benchmark(OperationsPerInvoke = TotalOperationsPerInvoke)]
public void Tlsf()
{
for (int batch = 0; batch < BatchCount; batch++) {
RunTlsf(batch, _tlsfAllocator, 4);
}
}
private static uint AlignSize(
uint size
)
{
uint mask = Alignment - 1;
return (size + mask) & ~mask;
}
private void BuildIterationWorkload()
{
for (int batch = 0; batch < BatchCount; batch++)
{
int batchOffset = batch * AllocationCount;
for (int i = 0; i < AllocationCount; i++)
{
_requestSizesByBatch[batchOffset + i] = AlignSize((uint)AllocSizes[_random.Next(AllocSizes.Length)]);
_freeOrderByBatch[batchOffset + i] = i;
}
ShuffleFreeOrder(batchOffset, AllocationCount);
}
}
private static nuint GetArenaBytes()
{
const int defaultMb = 512;
const int minMb = 64;
int mb = defaultMb;
string? env = Environment.GetEnvironmentVariable("TLSF_BENCH_ARENA_MB");
if (string.IsNullOrWhiteSpace(env)) {
env = Environment.GetEnvironmentVariable("HLSF_BENCH_ARENA_MB");
}
if (!string.IsNullOrWhiteSpace(env) &&
int.TryParse(env, out int parsed) &&
parsed >= minMb) {
mb = parsed;
}
return (nuint)mb * 1024u * 1024u;
}
private unsafe void RunLibc(
int batch
)
{
int batchOffset = batch * AllocationCount;
_libcLocalList.Clear();
for (int i = 0; i < AllocationCount; i++)
{
uint size = _requestSizesByBatch[batchOffset + i];
void* allocate = NativeMemory.AlignedAlloc(size, Alignment);
if (allocate == null) {
throw new OutOfMemoryException("NativeMemory.AlignedAlloc returned null.");
}
_libcLocalList.Add((nint)allocate);
}
for (int i = 0; i < _libcLocalList.Count; i++) {
NativeMemory.AlignedFree((void*)_libcLocalList[_freeOrderByBatch[batchOffset + i]]);
}
}
private void RunTlsf(
int batch,
TwoLayerSegregatedFitAllocator allocator,
int scanWindow = 4
)
{
int batchOffset = batch * AllocationCount;
_tlsfLocalList.Clear();
for (int i = 0; i < AllocationCount; i++)
{
uint size = _requestSizesByBatch[batchOffset + i];
Tlsf.Handle allocate = allocator.Alloc(size);
_tlsfLocalList.Add(allocate);
}
for (int i = 0; i < _tlsfLocalList.Count; i++) {
allocator.Free(_tlsfLocalList[_freeOrderByBatch[batchOffset + i]], scanWindow);
}
}
private void ShuffleFreeOrder(
int offset,
int count
)
{
for (int i = count - 1; i > 0; i--)
{
int swapIndex = _random.Next(i + 1);
int left = offset + i;
int right = offset + swapIndex;
(_freeOrderByBatch[left], _freeOrderByBatch[right]) =
(_freeOrderByBatch[right], _freeOrderByBatch[left]);
}
}
}
}
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<SolutionDir Condition="'$(SolutionDir)'=='' Or '$(SolutionDir)'=='*Undefined*'">$(MSBuildProjectDirectory)\..\</SolutionDir>
<OutputType>Exe</OutputType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Nitload.Common\Nitload.Common.csproj" Properties="DisablePostBuildCopy=true" />
</ItemGroup>
<ItemGroup>
<NativeArtifacts Include="$(SolutionDir)build\**\Nitload.Native*.dll" />
<NativeArtifacts Include="$(SolutionDir)build\**\Nitload.Native*.pdb" />
</ItemGroup>
<Target Name="CopyNativeArtifacts" AfterTargets="Build" Condition="'$(DisableNativeCopy)'!='true'">
<Copy SourceFiles="@(NativeArtifacts)" DestinationFolder="$(TargetDir)" SkipUnchangedFiles="true" Condition="@(NativeArtifacts) != ''" />
</Target>
</Project>
+24
View File
@@ -0,0 +1,24 @@
// -----------------------------------------------------------------------------
// file="Program"
// Project: StgSharp
// Copyright (c) Nitload.
// SPDX-License-Identifier: MIT
// -----------------------------------------------------------------------------
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Loggers;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Toolchains.InProcess.Emit;
using Nitload.Benchmark;
IConfig config = ManualConfig.Create(DefaultConfig.Instance)
.AddLogger(ConsoleLogger.Default)
.AddJob(Job.Default
.WithToolchain(InProcessEmitToolchain.Instance)
.WithLaunchCount(1)
.WithWarmupCount(6)
.WithIterationCount(15)
.WithInvocationCount(3)
.WithUnrollFactor(1));
_ = BenchmarkRunner.Run<TlsfAllocatorBench>(config);
+485
View File
@@ -0,0 +1,485 @@
// -----------------------------------------------------------------------------
// file="SwissBenchmark"
// Project: StgSharp
// Copyright (c) Nitload.
// SPDX-License-Identifier: MIT
// -----------------------------------------------------------------------------
using BenchmarkDotNet.Attributes;
using Nitload.Common.Collections;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace Nitload.Benchmark
{
/// <summary>
/// Benchmark comparing SwissTable against <see cref="Dictionary{TKey, TValue}" /> under
/// realistic L4 software cache address-mapping workloads. <para>/// L4 scenario
/// characteristics: /// - Keys are HLSF/malloc pointer addresses (large, 1024-byte aligned)
/// /// - Access pattern is Zipfian-skewed (20% hot cache lines receive 80% of lookups) ///
/// - Steady state: high hit-rate lookups dominate, with rare evict+refill on miss /// -
/// Working set: 512–4096 active cache line mappings /// </para>
/// </summary>
[MemoryDiagnoser]
public class SwissTableBench
{
/// <summary>
/// L4 cache line size in bytes (per L4_design.md).
/// </summary>
private const int CacheLineSize = 1024;
/// <summary>
/// Simulated heap base address for pointer-like keys.
/// </summary>
private const long HeapBase = 0x7FFF_0000_0000L;
/// <summary>
/// Number of lookup operations per Lookup benchmark call.
/// </summary>
private const int LookupIterations = 8192;
/// <summary>
/// Fraction of steady-state ops that are cache misses.
/// </summary>
private const double MissRate = 0.05;
/// <summary>
/// Number of steady-state operations per call. Each op is either a lookup (hit) or an
/// evict+insert+lookup (miss).
/// </summary>
private const int SteadyStateOps = 4096;
/// <summary>
/// Indices into _residentKeys for eviction targets (cold entries, tail region).
/// </summary>
private int[] _evictOrder = null!;
/// <summary>
/// Additional keys not in the table, for triggering misses.
/// </summary>
private nuint[] _externalKeys = null!;
// ── Pre-generated key arrays (pointer-like, 1024-byte aligned) ──
/// <summary>
/// Keys for initial table fill (simulates cached addresses).
/// </summary>
private nuint[] _residentKeys = null!;
/// <summary>
/// Pre-built steady-state operation script. Each entry: positive index → lookup
/// _residentKeys[idx], negative value → cache miss trigger (evict + insert from
/// _externalKeys).
/// </summary>
private int[] _steadyStateScript = null!;
/// <summary>
/// Uniform random lookup sequence (baseline comparison).
/// </summary>
private nuint[] _uniformLookups = null!;
/// <summary>
/// Zipfian-skewed lookup sequence (hot 20% accessed 80% of time).
/// </summary>
private nuint[] _zipfianLookups = null!;
private Dictionary<nuint, nuint> _dictLookup = null!;
private Dictionary<nuint, nuint> _freshDict = null!;
// ── Fresh instances for insert/mixed (rebuilt each iteration) ──
private SwissTable _freshSwiss = null!;
// ── Pre-filled lookup targets ──
private SwissTable _swissLookup = null!;
/// <summary>
/// Active cache line count. Matches L4 capacity range: 5 MB / 1024B ≈ 5120, 10 MB /
/// 1024B ≈ 10240. 512 and 1024 test low-occupancy; 4096 tests near-capacity.
/// </summary>
[Params(512, 1024, 4096)]
public int KeyCount;
[GlobalCleanup]
public void Cleanup()
{
_dictLookup.Clear();
_dictLookup.TrimExcess();
}
// ════════════════════════════════════════════════════════════
// Insert — bulk fill with scattered pointer addresses
// Simulates L4 initial cache warm-up phase.
// ════════════════════════════════════════════════════════════
[Benchmark]
public void Dictionary_Insert()
{
Dictionary<nuint, nuint> dict = _freshDict;
nuint[] keys = _residentKeys;
for (int i = 0; i < keys.Length; i++) {
dict[keys[i]] = keys[i] ^ unchecked((nuint)0xDEAD_BEEF);
}
}
// ════════════════════════════════════════════════════════════
// Lookup — Uniform (baseline comparison)
// ════════════════════════════════════════════════════════════
[Benchmark]
public nuint Dictionary_Lookup_Uniform()
{
nuint sum = 0;
nuint[] keys = _uniformLookups;
Dictionary<nuint, nuint> dict = _dictLookup;
for (int i = 0; i < keys.Length; i++)
{
if (dict.TryGetValue(keys[i], out nuint value)) {
sum += value;
}
}
return sum;
}
// ════════════════════════════════════════════════════════════
// Lookup — Zipfian (L4 hot path, skewed access)
// 20% of cache lines receive 80% of accesses.
// This is the most representative L4 benchmark.
// ════════════════════════════════════════════════════════════
[Benchmark(Baseline = true)]
public nuint Dictionary_Lookup_Zipfian()
{
nuint sum = 0;
nuint[] keys = _zipfianLookups;
Dictionary<nuint, nuint> dict = _dictLookup;
for (int i = 0; i < keys.Length; i++)
{
if (dict.TryGetValue(keys[i], out nuint value)) {
sum += value;
}
}
return sum;
}
// ════════════════════════════════════════════════════════════
// SteadyState — the KEY L4 benchmark
// Pre-filled table at capacity, then 4096 operations:
// ~95% are lookups (cache hits)
// ~5% are miss → evict cold entry → insert new entry → lookup new
// This directly simulates L4's main runtime loop.
// ════════════════════════════════════════════════════════════
[Benchmark]
public nuint Dictionary_SteadyState()
{
nuint sum = 0;
Dictionary<nuint, nuint> dict = _freshDict;
nuint[] resident = _residentKeys;
nuint[] external = _externalKeys;
int[] script = _steadyStateScript;
int[] evict = _evictOrder;
int evictCursor = 0;
int externalCursor = 0;
for (int i = 0; i < script.Length; i++)
{
int op = script[i];
if (op >= 0)
{
// Cache hit: lookup existing entry
if (dict.TryGetValue(resident[op], out nuint value))
{
sum += value;
}
} else
{
// Cache miss: evict a cold entry, insert a new one, then lookup
int evictIdx = evict[evictCursor % evict.Length];
evictCursor++;
dict.Remove(resident[evictIdx]);
int extIdx = externalCursor % external.Length;
externalCursor++;
nuint newKey = external[extIdx];
nuint newVal = newKey ^ unchecked((nuint)0xDEAD_BEEF);
dict[newKey] = newVal;
if (dict.TryGetValue(newKey, out nuint v)) {
sum += v;
}
}
}
return sum;
}
[IterationSetup(Targets = [
nameof(SwissTable_Insert),
nameof(SwissTable_SteadyState),
nameof(Dictionary_Insert),
nameof(Dictionary_SteadyState)
])]
public void IterationSetup()
{
_freshSwiss = SwissTable.Create();
_freshDict = new Dictionary<nuint, nuint>(KeyCount);
// Pre-fill for steady-state benchmarks
for (int i = 0; i < KeyCount; i++)
{
nuint key = _residentKeys[i];
nuint val = key ^ unchecked((nuint)0xDEAD_BEEF);
_freshSwiss.TryAddOrSet(key, val, out _);
_freshDict[key] = val;
}
}
// ════════════════════════════════════════════════════════════
// Setup / Teardown
// ════════════════════════════════════════════════════════════
[GlobalSetup]
public void Setup()
{
Random rng = new Random(42);
// ── Generate pointer-like keys (HeapBase + random_offset * 1024) ──
_residentKeys = GeneratePointerKeys(KeyCount, rng);
_externalKeys = GeneratePointerKeys(KeyCount, rng); // disjoint set for miss simulation
// ── Build lookup sequences ──
_zipfianLookups = GenerateZipfianLookups(_residentKeys, LookupIterations, rng);
_uniformLookups = GenerateUniformLookups(_residentKeys, LookupIterations, rng);
// ── Build steady-state operation script ──
_steadyStateScript = GenerateSteadyStateScript(KeyCount, SteadyStateOps, MissRate, rng);
// ── Build eviction order (cold tail entries first) ──
_evictOrder = new int[KeyCount];
for (int i = 0; i < KeyCount; i++) {
_evictOrder[i] = i;
}
// Shuffle so eviction isn't just sequential
Shuffle(_evictOrder, rng);
// ── Pre-fill lookup tables ──
_swissLookup = SwissTable.Create();
_dictLookup = new Dictionary<nuint, nuint>(KeyCount);
for (int i = 0; i < KeyCount; i++)
{
nuint key = _residentKeys[i];
nuint val = key ^ unchecked((nuint)0xDEAD_BEEF); // simulated cache pointer
_swissLookup.TryAddOrSet(key, val, out _);
_dictLookup[key] = val;
}
}
[Benchmark]
public void SwissTable_Insert()
{
SwissTable swiss = _freshSwiss;
nuint[] keys = _residentKeys;
for (int i = 0; i < keys.Length; i++) {
swiss.TryAddOrSet(keys[i], keys[i] ^ unchecked((nuint)0xDEAD_BEEF), out _);
}
}
[Benchmark]
public nuint SwissTable_Lookup_Uniform()
{
nuint sum = 0;
nuint[] keys = _uniformLookups;
SwissTable swiss = _swissLookup;
for (int i = 0; i < keys.Length; i++)
{
if (swiss.TryGet(keys[i], out nuint value)) {
sum += value;
}
}
return sum;
}
[Benchmark]
public nuint SwissTable_Lookup_Zipfian()
{
nuint sum = 0;
nuint[] keys = _zipfianLookups;
SwissTable swiss = _swissLookup;
for (int i = 0; i < keys.Length; i++)
{
if (swiss.TryGet(keys[i], out nuint value)) {
sum += value;
}
}
return sum;
}
[Benchmark]
public nuint SwissTable_SteadyState()
{
nuint sum = 0;
SwissTable swiss = _freshSwiss;
nuint[] resident = _residentKeys;
nuint[] external = _externalKeys;
int[] script = _steadyStateScript;
int[] evict = _evictOrder;
int evictCursor = 0;
int externalCursor = 0;
for (int i = 0; i < script.Length; i++)
{
int op = script[i];
if (op >= 0)
{
// Cache hit: lookup existing entry
if (swiss.TryGet(resident[op], out nuint value))
{
sum += value;
}
} else
{
// Cache miss: evict a cold entry, insert a new one, then lookup
int evictIdx = evict[evictCursor % evict.Length];
evictCursor++;
swiss.TryRemove(resident[evictIdx], out _);
int extIdx = externalCursor % external.Length;
externalCursor++;
nuint newKey = external[extIdx];
nuint newVal = newKey ^ unchecked((nuint)0xDEAD_BEEF);
swiss.TryAddOrSet(newKey, newVal, out _);
if (swiss.TryGet(newKey, out nuint v)) {
sum += v;
}
}
}
return sum;
}
// ════════════════════════════════════════════════════════════
// Data generation helpers
// ════════════════════════════════════════════════════════════
/// <summary>
/// Generate <paramref name="count" /> unique pointer-like keys. Each key = HeapBase +
/// randomOffset * CacheLineSize, simulating HLSF-allocated 1024-byte aligned addresses
/// scattered across a ~1 GB virtual address range.
/// </summary>
private static nuint[] GeneratePointerKeys(
int count,
Random rng
)
{
nuint[] keys = new nuint[count];
HashSet<long> used = new HashSet<long>(count);
for (int i = 0; i < count; i++)
{
long offset;
do
{
// Random offset within ~1M cache lines (1 GB range)
offset = (long)rng.Next(0, 1 << 20) * CacheLineSize;
} while (!used.Add(offset));
keys[i] = (nuint)(HeapBase + offset);
}
return keys;
}
/// <summary>
/// Generate a steady-state operation script. Each entry: non-negative value = index into
/// _residentKeys (lookup/hit), negative value (-1) = cache miss trigger (evict → insert →
/// lookup). Lookup indices are Zipfian-skewed.
/// </summary>
private static int[] GenerateSteadyStateScript(
int keyCount,
int opCount,
double missRate,
Random rng
)
{
int hotSetSize = Math.Max(1, keyCount / 5);
int[] script = new int[opCount];
for (int i = 0; i < opCount; i++)
{
if (rng.NextDouble() < missRate)
{
script[i] = -1; // miss
} else
{
// Zipfian hit
if (rng.NextDouble() < 0.8)
{
script[i] = rng.Next(0, hotSetSize);
} else
{
script[i] = rng.Next(hotSetSize, keyCount);
}
}
}
return script;
}
/// <summary>
/// Generate uniform random lookup sequence (baseline for comparison with Zipfian).
/// </summary>
private static nuint[] GenerateUniformLookups(
nuint[] sourceKeys,
int count,
Random rng
)
{
nuint[] lookups = new nuint[count];
for (int i = 0; i < count; i++) {
lookups[i] = sourceKeys[rng.Next(0, sourceKeys.Length)];
}
return lookups;
}
/// <summary>
/// Generate Zipfian-skewed lookup sequence: 80% of accesses hit the hottest 20% of keys
/// (sorted by index as "hotness proxy"), 20% of accesses hit the remaining 80%. This
/// models matrix computation where a small panel working set is reused heavily.
/// </summary>
private static nuint[] GenerateZipfianLookups(
nuint[] sourceKeys,
int count,
Random rng
)
{
int hotSetSize = Math.Max(1, sourceKeys.Length / 5);
nuint[] lookups = new nuint[count];
for (int i = 0; i < count; i++)
{
if (rng.NextDouble() < 0.8)
{
lookups[i] = sourceKeys[rng.Next(0, hotSetSize)];
} else
{
lookups[i] = sourceKeys[rng.Next(hotSetSize, sourceKeys.Length)];
}
}
return lookups;
}
/// <summary>
/// Fisher-Yates shuffle.
/// </summary>
private static void Shuffle(
int[] array,
Random rng
)
{
for (int i = array.Length - 1; i > 0; i--)
{
int j = rng.Next(0, i + 1);
(array[i], array[j]) = (array[j], array[i]);
}
}
}
}