HLSf use nuint rather than long, L4 now uses hlsf rather than array to alloc cache line.

This commit is contained in:
Youmu Liu
2026-04-21 20:32:28 +08:00
parent 2e2d64a2ce
commit 4f3e206dcb
11 changed files with 151 additions and 133 deletions
+11 -10
View File
@@ -46,7 +46,7 @@ namespace StgSharp.HighPerformance.Memory
private static readonly IIntrinsicKernel _intrinsic = IIntrinsicKernel.Instance;
private readonly EvictionArray* _eviction;
private readonly CacheLineHead* _head;
private readonly CacheLinePrediction* _prediction;
private readonly CacheLineDescription* _prediction;
private bool _disposed;
private bool _exhausted;
@@ -73,7 +73,7 @@ namespace StgSharp.HighPerformance.Memory
// init order is fallow by align requirement of each component.
nuint entrySize = Slab.RequestedAllocation; // SIMDID, no less than 16 byte bit
nuint headSize = ((nuint)Unsafe.SizeOf<CacheLineHead>()) * CacheLineCount; // 16 byte align
nuint predictionSize = ((nuint)Unsafe.SizeOf<CacheLinePrediction>()) * PredictionCount; //16 byte align
nuint predictionSize = ((nuint)Unsafe.SizeOf<CacheLineDescription>()) * PredictionCount; //16 byte align
nuint mapSize = SwissTable.RequestedNativeMemorySize; // 16 byte align
nuint rrpvSize = (nuint)Unsafe.SizeOf<EvictionArray>(); // 16 byte align
nuint normalizedBufferSize = (bufferSize + (PageSize - 1)) & (~(PageSize - 1)); // align to 4096 byte, page size
@@ -94,7 +94,7 @@ namespace StgSharp.HighPerformance.Memory
ptr += entrySize;
_head = (CacheLineHead*)ptr;
ptr += headSize;
_prediction = (CacheLinePrediction*)ptr;
_prediction = (CacheLineDescription*)ptr;
ptr += predictionSize;
_map = SwissTable.Create(ptr, _ => { });
ptr += mapSize;
@@ -140,7 +140,7 @@ namespace StgSharp.HighPerformance.Memory
// init order is fallow by align requirement of each component.
nuint entrySize = Slab.RequestedAllocation; // SIMDID, no less than 16 byte bit
nuint headSize = ((nuint)Unsafe.SizeOf<CacheLineHead>()) * CacheLineCount; // 16 byte align
nuint predictionSize = ((nuint)Unsafe.SizeOf<CacheLinePrediction>()) * PredictionCount; //16 byte align
nuint predictionSize = ((nuint)Unsafe.SizeOf<CacheLineDescription>()) * PredictionCount; //16 byte align
nuint mapSize = SwissTable.RequestedNativeMemorySize; // 16 byte align
nuint rrpvSize = (nuint)Unsafe.SizeOf<EvictionArray>(); // 16 byte align
nuint normalizedBufferSize = ByteCapacity; // align to 4096 byte, page size
@@ -150,7 +150,7 @@ namespace StgSharp.HighPerformance.Memory
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public L4CacheLine Map(
nuint origin
ref CacheLineDescription prediction
)
{
if ((_aheadCount < PredictionLeastAhead) && !_exhausted)
@@ -173,19 +173,20 @@ namespace StgSharp.HighPerformance.Memory
}
while (true)
{
if (_map.TryGet(origin, out nuint entryHandle))
if (_map.TryGet(prediction.GetHashCodeLong(), out nuint entryHandle))
{
// cache hit
CacheLineHead* entry = (CacheLineHead*)entryHandle;
if (Interlocked.Increment(ref entry->_refCount) > 0)
if (Interlocked.Increment(ref entry->RefCount) > 0)
{
(*_eviction)[entry->_id] = 0;
(*_eviction)[entry->Id] = 0;
_ = Interlocked.Increment(ref _activeCount);
_ = Interlocked.Decrement(ref _aheadCount);
return new L4CacheLine(entry->_address, ref _activeCount, ref entry->_refCount);
_ = Interlocked.Decrement(ref entry->RemainingPredictionCount);
return new L4CacheLine(entry->Address, ref _activeCount, ref entry->RefCount);
} else
{
_ = Interlocked.Decrement(ref entry->_refCount);
_ = Interlocked.Decrement(ref entry->RefCount);
}
}
Thread.Sleep(0);