l4 switch to RRPV eviction

This commit is contained in:
Youmu Liu
2026-03-11 09:00:51 +08:00
parent 3f2b1fcd97
commit c842f72934
6 changed files with 858 additions and 395 deletions
+47 -27
View File
@@ -1,30 +1,27 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="L4.cs"
// file="L4"
// Project: StgSharp
// AuthorGroup: Nitload Space
// Copyright (c) Nitload Space. All rights reserved.
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the “Software”), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and
// this permission notice shall be included in all copies
// or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”,
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
@@ -44,11 +41,18 @@ namespace StgSharp.HighPerformance.Memory
{
private const int CacheLineCount = 8192;
private const int PredictionCount = 2048;
private const int PredictionLeaseAhead = 1024;
private readonly EvictionArray* _eviction;
private readonly CacheLinePrediction* _prediction;
private bool _disposed;
private int _activeCount;
private readonly nuint _baseAddress;
private int _aheadCount;
private readonly nuint _baseAddress;
private readonly SlabAllocator<CacheLineData> _cacheLineAllocator;
private readonly SlabAllocator<CacheLineHead> _entryAllocator;
@@ -60,13 +64,17 @@ namespace StgSharp.HighPerformance.Memory
int cacheSize = Unsafe.SizeOf<CacheLineData>() * CacheLineCount;
int entrySize = Unsafe.SizeOf<CacheLineHead>() * CacheLineCount;
int mapSize = SwissTable.RequestedNativeMemorySize;
int pprvSize = Unsafe.SizeOf<EvictionArray>();
_baseAddress = (nuint)NativeMemory.AlignedAlloc((nuint)(cacheSize + entrySize + mapSize), 16);
nuint _buffer = _baseAddress;
_cacheLineAllocator = SlabAllocator.FromBuffer<CacheLineData>(_buffer, CacheLineCount, _ => { });
_entryAllocator = SlabAllocator.FromBuffer<CacheLineHead>(_buffer + (nuint)cacheSize, CacheLineCount, _ => { });
_map = SwissTable.Create(_baseAddress + (nuint)(cacheSize + entrySize), _ => { });
_eviction = (EvictionArray*)(_baseAddress + (nuint)(cacheSize + entrySize + mapSize));
}
public IPrefetchPredict PrefetchPredict { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage(
"Design",
"CA1063:Implement IDisposable correctly",
@@ -85,7 +93,9 @@ namespace StgSharp.HighPerformance.Memory
GC.SuppressFinalize(this);
}
public CacheLine Map(nuint origin)
public CacheLine Map(
nuint origin
)
{
if (_map.TryGet(origin, out nuint entryHandle))
{
@@ -99,9 +109,15 @@ namespace StgSharp.HighPerformance.Memory
* post command
* then wait for completion
*/
return origin; //error now
return /*origin/**/default; //error now
}
private partial void Prefetch();
private partial bool TestHit(
nuint origin
);
public ref struct CacheLine : IDisposable
{
@@ -109,7 +125,11 @@ namespace StgSharp.HighPerformance.Memory
private ref int _refCount;
private nuint _handle;
internal CacheLine(nuint handle, ref int activeCount, ref int cacheRefCount)
internal CacheLine(
nuint handle,
ref int activeCount,
ref int cacheRefCount
)
{
_handle = handle;
_refCount = ref cacheRefCount;