This commit is contained in:
陈思海 2026-04-21 15:24:00 +08:00
parent 3788d001ba
commit 24bb3f9e94
2 changed files with 5 additions and 19 deletions

View File

@ -73,7 +73,7 @@ namespace AlicizaX
if (item == null) return; if (item == null) return;
s_ReleaseCount++; s_ReleaseCount++;
// 防止 ClearAll 后 Release 导致负数
if (s_CurrentInUse > 0) if (s_CurrentInUse > 0)
s_CurrentInUse--; s_CurrentInUse--;
@ -96,7 +96,6 @@ namespace AlicizaX
if (frameCount == s_LastTickFrame) return; if (frameCount == s_LastTickFrame) return;
s_LastTickFrame = frameCount; s_LastTickFrame = frameCount;
// 更新高水位:基于实际并发使用量(在外对象数),而非单帧 Acquire 次数
if (s_PeakInUse > s_HighWaterMark) if (s_PeakInUse > s_HighWaterMark)
s_HighWaterMark = s_PeakInUse; s_HighWaterMark = s_PeakInUse;
@ -109,7 +108,6 @@ namespace AlicizaX
if (s_Count <= MIN_KEEP) return; if (s_Count <= MIN_KEEP) return;
// ---- 双阶段渐进回收 ----
if (s_IdleFrames >= IDLE_THRESHOLD) if (s_IdleFrames >= IDLE_THRESHOLD)
{ {
int target = Math.Max((int)(s_HighWaterMark * 1.5f), MIN_KEEP); int target = Math.Max((int)(s_HighWaterMark * 1.5f), MIN_KEEP);
@ -117,7 +115,7 @@ namespace AlicizaX
if (s_Count > target) if (s_Count > target)
{ {
int excess = s_Count - target; int excess = s_Count - target;
// 阶段1温和回收 / 阶段2激进回收
float ratio = s_IdleFrames < IDLE_AGGRESSIVE ? 0.25f : 0.5f; float ratio = s_IdleFrames < IDLE_AGGRESSIVE ? 0.25f : 0.5f;
int removeCount = Math.Max((int)(excess * ratio), 1); int removeCount = Math.Max((int)(excess * ratio), 1);
@ -159,9 +157,7 @@ namespace AlicizaX
} }
} }
/// <summary>
/// 预热池,启动时调用避免运行时 GC。
/// </summary>
public static void Prewarm(int count) public static void Prewarm(int count)
{ {
count = Math.Min(count, s_MaxCapacity); count = Math.Min(count, s_MaxCapacity);
@ -181,9 +177,6 @@ namespace AlicizaX
} }
} }
/// <summary>
/// 缩容:场景切换时调用,释放多余内存。
/// </summary>
public static void Shrink(int keepCount) public static void Shrink(int keepCount)
{ {
if (keepCount >= s_Count) return; if (keepCount >= s_Count) return;

View File

@ -4,10 +4,7 @@ using System.Runtime.CompilerServices;
namespace AlicizaX namespace AlicizaX
{ {
/// <summary>
/// 内存池注册表。非泛型路径通过此表查找对应的泛型池。
/// 注册发生在 MemoryPool&lt;T&gt; 静态构造器中,仅一次。
/// </summary>
public static class MemoryPoolRegistry public static class MemoryPoolRegistry
{ {
internal sealed class MemoryPoolHandle internal sealed class MemoryPoolHandle
@ -63,7 +60,6 @@ namespace AlicizaX
if (s_Handles.TryGetValue(type, out var handle)) if (s_Handles.TryGetValue(type, out var handle))
return handle.Acquire(); return handle.Acquire();
// 首次访问:触发 MemoryPool<T> 静态构造器
RuntimeHelpers.RunClassConstructor( RuntimeHelpers.RunClassConstructor(
typeof(MemoryPool<>).MakeGenericType(type).TypeHandle); typeof(MemoryPool<>).MakeGenericType(type).TypeHandle);
@ -85,7 +81,6 @@ namespace AlicizaX
return; return;
} }
// 首次访问:触发静态构造器
RuntimeHelpers.RunClassConstructor( RuntimeHelpers.RunClassConstructor(
typeof(MemoryPool<>).MakeGenericType(type).TypeHandle); typeof(MemoryPool<>).MakeGenericType(type).TypeHandle);
@ -138,9 +133,7 @@ namespace AlicizaX
} }
} }
/// <summary>
/// 每帧由 MemoryPoolTicker 调用,驱动所有池的自动回收。
/// </summary>
public static void TickAll(int frameCount) public static void TickAll(int frameCount)
{ {
if (s_TickArrayDirty) if (s_TickArrayDirty)