working on panel building, preparing for panel compute

This commit is contained in:
Youmu Liu
2025-12-03 18:14:45 +08:00
parent 38dba88331
commit 588fa867af
14 changed files with 338 additions and 212 deletions
@@ -86,9 +86,6 @@ namespace StgSharp.HighPerformance.Memory
SliceMemory(handle->NextNear, remainder);
}
}
if (handle->PreviousNear == null || handle->NextNear == null) {
throw new HLSFPositionChainBreakException();
}
return new hlsfHandle(handle, size);
}
@@ -98,47 +98,6 @@ namespace StgSharp.HighPerformance.Memory
return;
}
/*
Console.WriteLine("============ Merge start =======================");
try
{
Console.WriteLine("forward from entry");
Entry* __cur = entry;
int __cnt = 0;
while (__cur != null && __cur != _spareMemory && __cnt < 512)
{
Console.WriteLine(EntryToString(__cur));
__cur = __cur->NextNear;
__cnt++;
if (__cur == entry)
{
Console.WriteLine("(looped back to start)");
break;
}
}
Console.WriteLine(EntryToString(__cur));
Console.WriteLine("MergeAndRemoveFromBuckets: backward from entry");
__cur = entry;
__cnt = 0;
while (__cur != null && __cur != _spareMemory && __cnt < 512)
{
Console.WriteLine(EntryToString(__cur));
__cur = __cur->PreviousNear;
__cnt++;
if (__cur == entry)
{
Console.WriteLine("(looped back to start)");
break;
}
}
Console.WriteLine(EntryToString(__cur));
}
catch { }
finally
{
Console.WriteLine("==========================================");
}
/**/
long size = entry->Size;
int originLevel = entry->Level;
@@ -228,26 +187,11 @@ namespace StgSharp.HighPerformance.Memory
[FieldOffset(0)] internal Entry* NextNear; // Position linked list: next pointer
[FieldOffset(8)] internal Entry* PreviousNear; // Position linked list: previous pointer
[FieldOffset(24)] internal int _Level; // Level of the block in the allocator
[FieldOffset(24)] internal int Level; // Level of the block in the allocator
[FieldOffset(16)] internal long Size; // Block size information
[FieldOffset(36)] public EntryState State;
[FieldOffset(28)] public nuint Position; // Memory block position
public int Level
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _Level;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set
{
int size = 64 * (1 << (value * 2));
if (Size / size > 3 && Size < 64 * 1024 * 1024L) {
throw new HLSFPositionChainBreakException();
}
_Level = value;
}
}
}
internal enum EntryState : uint
@@ -260,20 +204,5 @@ namespace StgSharp.HighPerformance.Memory
}
#region Entry Operations
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool IsNullOrEmptyEntryIndex(ulong index)
{
return index == 0;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool IsNullOrEmptyEntryIndex(Entry* entry)
{
return entry == null;
}
#endregion
}
}
@@ -3,12 +3,14 @@
A constant-time (O(1)) allocator for real-time, high-throughput scenarios. It uses layered size classes with segmented buckets and stores all metadata externally for clean user memory and better cache locality.
## Key Points
- O(1) allocate/free with 10 levels: 64B ★ 16MB; each level split into 1~/2~/3~ segments
- O(1) allocate/free with 10 levels: 64B x 16MB; each level split into 1x/2x/3x segments
- External metadata (`Entry`, `BucketNode`) in dedicated SLABs; user blocks have no headers
- Configurable alignment (-16B); large block cap per allocation: 32MB
- Configurable alignment (>=16B); large block cap per allocation: 32MB
- Single-threaded implementation (external sync required for multi-threaded use)
## Quick Use
```csharp
using var hlsf = new HybridLayerSegregatedFitAllocator(64 * 1024 * 1024);
var h = hlsf.Alloc(1024);
@@ -16,49 +18,83 @@ Span<byte> s = h.BufferHandle; s[0] = 42;
hlsf.Free(h);
```
## Benchmarks (this run)
Environment: .NET 8 (Release), x64, single-thread, arena=3072 MiB. Results from `StgSharpDebug/HlsfStressTest`.
## Core APIs
- Free throughput: 1KB ~ 262,144 ★ 27,062,260.6 free/s
- Alloc+free pairs: 256B ~ 5,000,000 ★ 19,256,409.9 ops/s
- Fragmentation cycle (「512MB): fast recovery in short cycle (see test for details)
### Alloc(uint size)
- Purpose: Allocate a block of at least `size` bytes.
- Input: `size` in bytes (<= 32MB for internal path; >32MB uses native large allocation).
- Output: `HybridLayerSegregatedFitAllocationHandle` with pointer access and `Span<byte>`.
- Behavior:
- Classify into level/segment and pop a matching bucket; escalate segments and levels if empty.
- Fallback to overflow bucket or carve 32MB from spare memory if all buckets are empty.
- Mark selected `Entry` as Alloc; slice any remainder into `Empty` entries and push to buckets.
- For >32MB requests, use `NativeMemory.AlignedAlloc` and mark `EntryState.Large`.
### Free(HybridLayerSegregatedFitAllocationHandle handle)
- Purpose: Free a previously allocated block.
- Behavior:
- Mark the corresponding `Entry` as `Empty`.
- Merge with adjacent `Empty` entries in both directions (boundary and level constraints).
- Remove merged neighbors from buckets, update position chain, and optionally promote level.
- Re-enqueue the final `Entry` into the appropriate bucket.
### Collect()
- Purpose: Reclaim and rebalance free entries without touching allocated blocks.
- Behavior:
- Consolidate fragmented regions where possible and improve bucket coverage.
- Intended to be cheap; safe to call between benchmark phases.
## Benchmarks (this run)
Environment: .NET 8 (Release), x64, single-thread, arena=3072 MiB. Results from StgSharpDebug/HlsfStressTest.
- Free throughput: 1KB x 262,144 -> 38,928,422.9 free/s
- Alloc+free pairs: 256B x 5,000,000 -> 25,006,976.9 ops/s
- Fragmentation cycle (~512MB): fast recovery in short cycle (see test for details)
Representative allocation throughput (higher is better):
| Size | Ops | alloc/s (no touch) | alloc/s (touch) |
|------|----:|--------------------:|----------------:|
| 64B | 2,000,000 | 7,902,555.2 | 14,176,544.8 |
| 1KB | 1,572,864 | 5,794,161.6 | 7,293,856.5 |
| 4KB | 393,216 | 5,210,132.6 | 6,646,372.3 |
| 32KB | 49,152 | 5,931,432.3 | 7,719,199.1 |
| 256KB | 6,144 | 14,663,484.5 | 10,425,929.1 |
| 512KB | 3,072 | 11,054,336.1 | 16,245,372.8 |
| 1MB | 1,536 | 13,391,456.0 | 9,159,212.9 |
| 2MB | 768 | 17,028,824.8 | 11,962,616.8 |
| 8MB | 192 | 13,061,224.5 | 9,142,857.1 |
| 32MB | 48 | 11,707,317.1 | 9,411,764.7 |
| 64B | 2,000,000 | 8,780,467.7 | 17,524,920.4 |
| 1KB | 1,572,864 | 8,974,865.4 | 9,585,531.6 |
| 4KB | 393,216 | 7,340,392.2 | 6,849,319.4 |
| 32KB | 49,152 | 10,376,625.6 | 7,019,407.9 |
| 256KB | 6,144 | 16,041,775.5 | 12,842,809.4 |
| 512KB | 3,072 | 20,466,355.8 | 14,195,933.5 |
| 1MB | 1,536 | 16,916,299.6 | 13,568,904.6 |
| 2MB | 768 | 17,985,948.5 | 12,569,558.1 |
| 8MB | 192 | 13,426,573.4 | 11,034,482.8 |
| 32MB | 48 | 11,162,790.7 | 9,411,764.7 |
Notes:
- Peak no-touch at 2MB (「17.0M alloc/s). Peak touch at 512KB (「16.25M alloc/s).
- Peak no-touch at 2MB (~17.99M alloc/s). Peak touch near 64B (~17.52M alloc/s).
- Non-aligned sizes (e.g., 768B, 3KB) typically lower due to slicing/boundary handling.
## Detailed Design
## Detailed Design (summary)
-
- Size classes: levelSizeArray = [64, 256, 1024, 4096, 16384, 65536, 262144, 1048576, 4194304, 16777216] (64B x 4^level)
- Segments per level: 1x, 2x, 3x
- Buckets: index = 3 * level + segment (overflow bucket for level > 9)
- Metadata: Entry (external SLAB, position chain), BucketNode (user region, bucket chain)
- Position chain: circular doubly linked list anchored by spare sentinel
- Allocation: search buckets by level/segment with escalation; if none, carve 32MB from spare; set Alloc; slice remainder into buckets
- Slicing: align to level boundaries, pack contiguous blocks forward/backward to minimize overhead
- Free/Merge: bidirectional merge under boundary and level constraints; update buckets; optional level promotion
- Alignment: default 16B; internal blocks aligned by slicing/edge calculation
- Complexity: average O(1) with bounded levels and constant-time bucket ops
### Size Classes and Segments
- Level sizes: `levelSizeArray = [64, 256, 1024, 4096, 16384, 65536, 262144, 1048576, 4194304, 16777216]` (64B , 4^L)
- Level selection:
- `GetLevelFromSize(uint S)`: `level = max(0, (log2(S) - 6) / 2)` using `BitOperations.LeadingZeroCount`
- Segment selection (within a level):
- `DetermineSegmentIndex(size, level) = (int)((size - 1) / levelSizeArray[level])` ★ 0/1/2
- Bucket index: `index = 3*level + segment` (for L + 9); overflow bucket at `^1` for level > 9
### Metadata Structures
- `Entry` (external SLAB): linkage in position chain, `Position`, `Size`, `Level`, `State`
- Invariants: near neighbors must form a consistent double-linked ring; `Level` setter validates size vs. level window
- `BucketNode` (in user region): `EntryRef`, `NextLevel`, `PreviousLevel`, `IsInBucket`
- Position chain: circular doubly linked list anchored by sentinel `_spareMemory` (`State.Spare`)
### Allocation Flow
1) Compute `level` and `segment` from requested `size`
2) Try pop bucket (`TryPopLevelForAllocation(level, segment, out entry)`) and escalate:
- Advance `segment` 0¡ú1¡ú2, then `level++`, until `MaxLevel`
@@ -67,6 +103,7 @@ Notes:
4) If remainder exists in the source region, call `SliceMemory(entry->NextNear, remainder)` to produce free chunks into buckets
### Slicing Logic (`SliceMemory`)
- Inputs: `next` entry position, `sizeToSlice`
- Compute `offset` and `end` relative to base buffer; pick starting level by trailing-zero count of `offset`:
- `i = (BitOperations.TrailingZeroCount(offset) - 6) / 2`, clamped to `¡Ü MaxLevel`
@@ -77,6 +114,7 @@ Notes:
- For each packed region: allocate new `Entry`, insert before `next`, mark `Empty`, and push corresponding `BucketNode` into the `(level, segment)` bucket (`PushBucketToLevel(i, count-1, b)`)
### Free and Merge (`MergeAndRemoveFromBuckets`)
- Given an `Entry` to free (turned `Empty` by caller):
- Merge left while neighbor `p` is `Empty`, same `Level`, and within boundary (`AssertBoundary`)
- Remove `p` from bucket (`RemoveFromBucket`), expand current, fix `Position`, unlink `p`
@@ -85,25 +123,30 @@ Notes:
- Ensure `BucketNode.EntryRef` points to the merged entry; caller re-enqueues into the proper bucket
### Buckets
- `PushBucketToLevel(level, segment, node)`: push to bucket head, set `IsInBucket`, maintain prev/next
- `TryPopLevel(level, segment, out node)`: pop head; if empty, return false
- `RemoveFromBucket(level, segment, node)`: unlink from middle or use `TryPopLevel` if it is the head
### Large Allocations and Spare Memory
- Requests > 32MB use `NativeMemory.AlignedAlloc(size, align)` and mark `State.Large`
- When buckets are empty, `TryAllocateNew32MBBlock` carves a 32MB region from `_spareMemory`, builds an `Empty` entry, and advances the spare pointer
### Alignment
- Default `_align = 16`; `AlignOfLevel(level)` returns `min(levelSizeArray[level], _align)`
- Internal blocks align to level boundaries by design of slicing/edge calculations
### Complexity and Invariants
- Allocate/free are O(1) on average with bounded levels (¡Ü10) and constant-time bucket ops
- Invariants guarded by exceptions (e.g., `HLSFPositionChainBreakException`) and pointer consistency checks
## Profiling (hotspots, current workloads)
- `SliceMemory` 「22% (self 「17%) ! slicing and bucket enqueue dominate allocation path
- `MergeAndRemoveFromBuckets` 「13% (self 「12%) ! merge + removal dominate free path
- `Collect` 「7.6% (self 「7.5%) ! low overall cost
Suggested focus: reduce per-slice math and bucket ops; add fast-paths for fully aligned regions; batch/lazy removals after merges.
- SliceMemory ~22% (self ~17%) ¡ª slicing and bucket enqueue dominate allocation path
- MergeAndRemoveFromBuckets ~13% (self ~12%) ¡ª merge + removal dominate free path
- Collect ~7.6% (self ~7.5%) ¡ª low overall cost
Focus: reduce per-slice math and bucket ops; add fast-paths for aligned regions; batch/lazy removals after merges.