From 15f90aed29d3a40277694630bdaa896005d8d351 Mon Sep 17 00:00:00 2001 From: Youmu Liu <105042599+Nitload-NSI@users.noreply.github.com> Date: Thu, 21 May 2026 19:40:00 +0800 Subject: [PATCH] L4 is now a burden --- .../HighPerformance/Memory/L4.CacheLine.cs | 32 +++++++++++++ .../HighPerformance/Memory/L4.Prefetch.cs | 5 --- StgSharp.Common/HighPerformance/Memory/L4.cs | 45 ++----------------- 3 files changed, 36 insertions(+), 46 deletions(-) diff --git a/StgSharp.Common/HighPerformance/Memory/L4.CacheLine.cs b/StgSharp.Common/HighPerformance/Memory/L4.CacheLine.cs index c42466d9..f33c74bd 100644 --- a/StgSharp.Common/HighPerformance/Memory/L4.CacheLine.cs +++ b/StgSharp.Common/HighPerformance/Memory/L4.CacheLine.cs @@ -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); + } + + } + } } diff --git a/StgSharp.Common/HighPerformance/Memory/L4.Prefetch.cs b/StgSharp.Common/HighPerformance/Memory/L4.Prefetch.cs index 357ff251..b218708a 100644 --- a/StgSharp.Common/HighPerformance/Memory/L4.Prefetch.cs +++ b/StgSharp.Common/HighPerformance/Memory/L4.Prefetch.cs @@ -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 span = new(_prediction, PredictionCount); diff --git a/StgSharp.Common/HighPerformance/Memory/L4.cs b/StgSharp.Common/HighPerformance/Memory/L4.cs index 4e2d14b7..3c93616b 100644 --- a/StgSharp.Common/HighPerformance/Memory/L4.cs +++ b/StgSharp.Common/HighPerformance/Memory/L4.cs @@ -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,8 +156,8 @@ namespace StgSharp.HighPerformance.Memory } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public L4CacheLine Map( - ref CacheLineDescription prediction + public Handle Map( + ref CacheLineDescription prediction ) { if ((_aheadCount < PredictionLeastAhead) && !_exhausted) @@ -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); - } - - } }