L4 is now a burden

This commit is contained in:
Youmu Liu
2026-05-21 19:40:00 +08:00
parent 0681237c3b
commit 15f90aed29
3 changed files with 36 additions and 46 deletions
@@ -119,5 +119,37 @@ namespace StgSharp.HighPerformance.Memory
#endregion
}
[StructLayout(LayoutKind.Sequential)]
public unsafe ref struct Handle : IDisposable
{
private CacheLineMetadata* _head;
private nuint _handle;
private Handle(
nuint handle,
CacheLineMetadata* cacheRefCount
)
{
_handle = handle;
_head = cacheRefCount;
}
[Obsolete("You should not create a CacheLineHandle instance outside a L4", true)]
public Handle() { }
public readonly nuint Address => _handle;
public void Dispose()
{
if (_handle == 0) {
return;
}
_handle = 0;
_ = Interlocked.Decrement(ref _head->RefCount);
}
}
}
}
@@ -33,16 +33,11 @@ namespace StgSharp.HighPerformance.Memory
public unsafe partial class L4
{
public const int MaxActivateCacheLineCount = 1024;
private partial void Prefetch()
{
if (CurrentPredict is null) {
return;
}
if (_activeCount > MaxActivateCacheLineCount) {
throw new OverflowException("Too many activated cache line in external reference.");
}
IL4Predict predict = CurrentPredict;
int predictIndex = CurrentPredictIndex;
Span<CacheLineDescription> span = new(_prediction, PredictionCount);
+3 -40
View File
@@ -59,7 +59,6 @@ namespace StgSharp.HighPerformance.Memory
private readonly CacheLineDescription* _prediction;
private bool _disposed;
private bool _exhausted;
private int _activeCount;
private int _aheadCount;
@@ -123,7 +122,7 @@ namespace StgSharp.HighPerformance.Memory
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ContinueOnce(
L4CacheLine line
Handle line
)
{
if (line.Address > 0) {
@@ -157,7 +156,7 @@ namespace StgSharp.HighPerformance.Memory
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public L4CacheLine Map(
public Handle Map(
ref CacheLineDescription prediction
)
{
@@ -188,10 +187,9 @@ namespace StgSharp.HighPerformance.Memory
if (Interlocked.Increment(ref entry->RefCount) > 0)
{
(*_predictCount)[entry->Id] = 0;
_ = Interlocked.Increment(ref _activeCount);
_ = Interlocked.Decrement(ref _aheadCount);
_ = Interlocked.Decrement(ref (*_mapCount)[entry->Id]);
return new L4CacheLine((nuint)entry->Position, ref _activeCount, ref entry->RefCount);
return new Handle((nuint)entry->Position, ref entry->RefCount);
} else
{
_ = Interlocked.Decrement(ref entry->RefCount);
@@ -204,39 +202,4 @@ namespace StgSharp.HighPerformance.Memory
private partial void Prefetch();
}
public ref struct L4CacheLine : IDisposable
{
private ref int _activeCount;
private ref int _refCount;
private nuint _handle;
internal L4CacheLine(
nuint handle,
ref int activeCount,
ref int cacheRefCount
)
{
_handle = handle;
_refCount = ref cacheRefCount;
_activeCount = ref activeCount;
}
[Obsolete("You should not create a CacheLineHandle instance outside a L4", true)]
public L4CacheLine() { }
public readonly nuint Address => _handle;
public void Dispose()
{
if (_handle == 0) {
return;
}
_handle = 0;
_ = Interlocked.Decrement(ref _activeCount);
_ = Interlocked.Decrement(ref _refCount);
}
}
}