com.alicizax.unity.framework/Runtime/MemoryPool/MemoryPool.cs
陈思海 9afd5d9ff9 [Opt] ObjectPoolService && MemoryPool [Add]Benchmark Example
优化ObjcetPoolService 优化MemoryPoolService
增加ObjectPool 和MemoryPool的Benchmark
2026-04-27 14:38:24 +08:00

127 lines
3.2 KiB
C#

using System;
using System.Runtime.CompilerServices;
namespace AlicizaX
{
public static partial class MemoryPool
{
private static bool _enableStrictCheck;
private static int _strictCheckVersion;
public static bool EnableStrictCheck
{
get => _enableStrictCheck;
set
{
if (_enableStrictCheck == value)
return;
_enableStrictCheck = value;
_strictCheckVersion++;
}
}
internal static int StrictCheckVersion => _strictCheckVersion;
public static int Count => MemoryPoolRegistry.Count;
#if UNITY_EDITOR
public static MemoryPoolInfo[] GetAllMemoryPoolInfos()
{
return MemoryPoolRegistry.GetAllInfos();
}
#endif
public static int GetAllMemoryPoolInfos(MemoryPoolInfo[] infos)
{
return MemoryPoolRegistry.GetAllInfos(infos);
}
public static void ClearAll()
{
MemoryPoolRegistry.ClearAll();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T Acquire<T>() where T : class, IMemory, new()
{
return MemoryPool<T>.Acquire();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IMemory Acquire(Type memoryType)
{
return MemoryPoolRegistry.Acquire(memoryType);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Release<T>(T memory) where T : class, IMemory, new()
{
MemoryPool<T>.Release(memory);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Release(IMemory memory)
{
MemoryPoolRegistry.Release(memory);
}
public static void Add<T>(int count) where T : class, IMemory, new()
{
MemoryPool<T>.Prewarm(count);
}
public static void Add(Type memoryType, int count)
{
MemoryPoolRegistry.Prewarm(memoryType, count);
}
public static void Remove<T>(int count) where T : class, IMemory, new()
{
int target = MemoryPool<T>.UnusedCount - count;
MemoryPool<T>.Shrink(target);
}
public static void Remove(Type memoryType, int count)
{
MemoryPoolRegistry.RemoveFromType(memoryType, count);
}
public static void RemoveAll<T>() where T : class, IMemory, new()
{
MemoryPool<T>.ClearAll();
}
public static void SetCapacity<T>(int softCapacity, int hardCapacity) where T : class, IMemory, new()
{
MemoryPool<T>.SetCapacity(softCapacity, hardCapacity);
}
public static void Compact<T>() where T : class, IMemory, new()
{
MemoryPool<T>.Compact();
}
public static void Compact(Type memoryType)
{
MemoryPoolRegistry.CompactType(memoryType);
}
public static void CompactAll()
{
MemoryPoolRegistry.CompactAll();
}
public static void RemoveAll(Type memoryType)
{
MemoryPoolRegistry.ClearType(memoryType);
}
}
}