Update EventContainer.cs

This commit is contained in:
陈思海 2026-03-10 18:56:51 +08:00
parent d8a0f7a7c9
commit 0197adc5c6

View File

@ -14,7 +14,6 @@ namespace AlicizaX
private static readonly int InitialSize = EventInitialSize<TPayload>.Size; private static readonly int InitialSize = EventInitialSize<TPayload>.Size;
private static readonly int TypeId; private static readonly int TypeId;
// 优化3: SoA布局提升缓存命中率
private static Action<TPayload>[] _callbacks; private static Action<TPayload>[] _callbacks;
private static int[] _versions; private static int[] _versions;
private static int[] _activeSlots; private static int[] _activeSlots;
@ -65,7 +64,6 @@ namespace AlicizaX
int handlerIndex = GetFreeSlot(); int handlerIndex = GetFreeSlot();
// 优化4: 提前扩容检查
if (_activeCount >= _activeIndices.Length) if (_activeCount >= _activeIndices.Length)
{ {
Array.Resize(ref _activeIndices, _activeIndices.Length << 1); Array.Resize(ref _activeIndices, _activeIndices.Length << 1);
@ -93,13 +91,11 @@ namespace AlicizaX
int oldLen = _callbacks.Length; int oldLen = _callbacks.Length;
int newSize = oldLen == 0 ? 64 : oldLen << 1; int newSize = oldLen == 0 ? 64 : oldLen << 1;
// 批量扩容
Array.Resize(ref _callbacks, newSize); Array.Resize(ref _callbacks, newSize);
Array.Resize(ref _versions, newSize); Array.Resize(ref _versions, newSize);
Array.Resize(ref _activeSlots, newSize); Array.Resize(ref _activeSlots, newSize);
Array.Resize(ref _freeSlots, newSize); Array.Resize(ref _freeSlots, newSize);
// 逆序添加,保持小索引优先(缓存友好)
for (int i = newSize - 1; i >= oldLen; i--) for (int i = newSize - 1; i >= oldLen; i--)
{ {
_freeSlots[_freeCount++] = i; _freeSlots[_freeCount++] = i;
@ -111,14 +107,14 @@ namespace AlicizaX
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void Unsubscribe(int handlerIndex, int version) private static void Unsubscribe(int handlerIndex, int version)
{ {
// 优化5: 提前版本检查
if (_versions[handlerIndex] != version) return; if (_versions[handlerIndex] != version) return;
int lastActiveIndex = --_activeCount; int lastActiveIndex = --_activeCount;
int lastHandlerIndex = _activeIndices[lastActiveIndex]; int lastHandlerIndex = _activeIndices[lastActiveIndex];
int currentActiveIndex = _activeSlots[handlerIndex]; int currentActiveIndex = _activeSlots[handlerIndex];
// Swap-remove
_activeIndices[currentActiveIndex] = lastHandlerIndex; _activeIndices[currentActiveIndex] = lastHandlerIndex;
_activeSlots[lastHandlerIndex] = currentActiveIndex; _activeSlots[lastHandlerIndex] = currentActiveIndex;
@ -137,21 +133,19 @@ namespace AlicizaX
_freeSlots[_freeCount++] = handlerIndex; _freeSlots[_freeCount++] = handlerIndex;
} }
// 优化6: 改进Publish减少数组访问和分支
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Publish(in TPayload payload) public static void Publish(in TPayload payload)
{ {
int count = _activeCount; int count = _activeCount;
if (count == 0) return; // 快速路径 if (count == 0) return;
int[] indices = _activeIndices; int[] indices = _activeIndices;
Action<TPayload>[] callbacks = _callbacks; Action<TPayload>[] callbacks = _callbacks;
// 优化7: 更高效的循环展开
int i = 0; int i = 0;
int unrolled = count & ~3; // count - (count % 4) int unrolled = count & ~3; // count - (count % 4)
// 4路展开主循环
for (; i < unrolled; i += 4) for (; i < unrolled; i += 4)
{ {
int idx0 = indices[i]; int idx0 = indices[i];
@ -165,7 +159,6 @@ namespace AlicizaX
callbacks[idx3](payload); callbacks[idx3](payload);
} }
// 优化8: 使用Duff's Device处理剩余
switch (count - i) switch (count - i)
{ {
case 3: case 3:
@ -178,14 +171,12 @@ namespace AlicizaX
} }
} }
// 额外优化方法
public static int SubscriberCount public static int SubscriberCount
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _activeCount; get => _activeCount;
} }
// 预热容量,避免运行时扩容
public static void EnsureCapacity(int capacity) public static void EnsureCapacity(int capacity)
{ {
if (_callbacks.Length >= capacity) return; if (_callbacks.Length >= capacity) return;
@ -203,7 +194,6 @@ namespace AlicizaX
} }
} }
// 清空所有订阅
public static void Clear() public static void Clear()
{ {
for (int i = 0; i < _activeCount; i++) for (int i = 0; i < _activeCount; i++)