some chore

This commit is contained in:
Youmu Liu
2026-03-11 18:55:45 +08:00
parent c842f72934
commit 9b5dfec9d5
7 changed files with 313 additions and 32 deletions
@@ -0,0 +1,81 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="L4.InternalOp"
// Project: StgSharp
// 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.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.HighPerformance.Memory
{
public partial class L4
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private unsafe void AgeAll()
{
Vector128<byte> one = Vector128.Create((byte)0x01);
Vector128<byte> max = Vector128.Create((byte)0x03);
byte* p = (byte*)_eviction;
for (int i = 0; i < CacheLineCount; i += 16)
{
Vector128<byte> v = Sse2.LoadVector128(p + i);
v = Sse2.AddSaturate(v, one);
v = Sse2.Min(v, max);
Sse2.Store(p + i, v);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private unsafe void Evict(
int count = 1
)
{
int remain = count;
Vector128<byte> one = Vector128.Create((byte)0x01);
Vector128<byte> three = Vector128.Create((byte)0x03);
byte* p = (byte*)_eviction;
for (int i = 0; i < CacheLineCount && remain > 0; i += 16)
{
Vector128<byte> v = Sse2.LoadVector128(p + i);
Vector128<byte> mask = Sse2.CompareEqual(v, three);
int evictMask = Sse2.MoveMask(mask);
while (evictMask != 0 && remain > 0)
{
int index = BitOperations.TrailingZeroCount(evictMask);
evictMask &= (evictMask - 1);
EvictLine(i + index);
remain--;
}
}
}
}
}
@@ -45,6 +45,7 @@ namespace StgSharp.HighPerformance.Memory
Span<CacheLinePrediction> span = new(_prediction, PredictionCount);
int actualCount = PrefetchPredict.Predict(span);
int missIndex = 0;
AgeAll();
for (int i = 0; i < actualCount; i++)
{
ref CacheLinePrediction handle = ref span[i];
@@ -62,6 +63,9 @@ namespace StgSharp.HighPerformance.Memory
{
ref CacheLinePrediction handle = ref span[i];
nuint address = handle.Address;
nuint mapPtr = _cacheLineAllocator.Allocate();
CacheLineHead* head = (CacheLineHead*)_entryAllocator.Allocate();
// pre fetch them here
}
@@ -171,15 +171,7 @@ SoA 布局 — 平行数组
**逐出标志位布局**(`_evictionFlags[i]`):
```
bit: 7 6 5 4 3 2 1 0
───────── ─ ───── ─
保留 C RRPV D
D = bit 0 = dirty(0=只读副本, 1=有脏数据待写回)
RRPV = bit 1-2 = Re-Reference Prediction Value(0~3)
C = bit 3 = costClass(0=低成本, 1=高成本)
```
RRPV数组使用byte存储每一个cacheline的使用情况,其低二位表示如下逻辑
**RRPV 语义**:
@@ -203,7 +195,7 @@ SoA 布局 — 平行数组
| 对比项 | v0.1 环形链表 | v0.3 RRPV 掩码 + 回调 |
|---------------|------------------------|------------------------------|
| 数据结构 | 4 条双向链表 + next 指针 | byte 数组 + 函数指针数组 |
| 升温操作 | 摘节点 + 插节点(4 指针)| `&= 0xF9`(1 字节写入) |
| 升温操作 | 摘节点 + 插节点(4 指针)| `+= 1`(1 字节写入) |
| 老化操作 | 逐节点移链表 | SIMD 批量 +1(16 个/周期) |
| 逐出扫描 | 遍历链表头(指针追逐) | SIMD 模式匹配(16 个/周期) |
| 缓存友好性 | 差(指针跳跃) | 好(连续内存扫描) |