From 0197adc5c67d1f21f13c27bf5aea8564f9cf12df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=80=9D=E6=B5=B7?= <1464576565@qq.com> Date: Tue, 10 Mar 2026 18:56:51 +0800 Subject: [PATCH] Update EventContainer.cs --- Runtime/ABase/Event/EventContainer.cs | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/Runtime/ABase/Event/EventContainer.cs b/Runtime/ABase/Event/EventContainer.cs index e97b733..1a810e8 100644 --- a/Runtime/ABase/Event/EventContainer.cs +++ b/Runtime/ABase/Event/EventContainer.cs @@ -14,7 +14,6 @@ namespace AlicizaX private static readonly int InitialSize = EventInitialSize.Size; private static readonly int TypeId; - // 优化3: SoA布局,提升缓存命中率 private static Action[] _callbacks; private static int[] _versions; private static int[] _activeSlots; @@ -65,7 +64,6 @@ namespace AlicizaX int handlerIndex = GetFreeSlot(); - // 优化4: 提前扩容检查 if (_activeCount >= _activeIndices.Length) { Array.Resize(ref _activeIndices, _activeIndices.Length << 1); @@ -93,13 +91,11 @@ namespace AlicizaX int oldLen = _callbacks.Length; int newSize = oldLen == 0 ? 64 : oldLen << 1; - // 批量扩容 Array.Resize(ref _callbacks, newSize); Array.Resize(ref _versions, newSize); Array.Resize(ref _activeSlots, newSize); Array.Resize(ref _freeSlots, newSize); - // 逆序添加,保持小索引优先(缓存友好) for (int i = newSize - 1; i >= oldLen; i--) { _freeSlots[_freeCount++] = i; @@ -111,14 +107,14 @@ namespace AlicizaX [MethodImpl(MethodImplOptions.AggressiveInlining)] private static void Unsubscribe(int handlerIndex, int version) { - // 优化5: 提前版本检查 + if (_versions[handlerIndex] != version) return; int lastActiveIndex = --_activeCount; int lastHandlerIndex = _activeIndices[lastActiveIndex]; int currentActiveIndex = _activeSlots[handlerIndex]; - // Swap-remove + _activeIndices[currentActiveIndex] = lastHandlerIndex; _activeSlots[lastHandlerIndex] = currentActiveIndex; @@ -137,21 +133,19 @@ namespace AlicizaX _freeSlots[_freeCount++] = handlerIndex; } - // 优化6: 改进Publish,减少数组访问和分支 + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Publish(in TPayload payload) { int count = _activeCount; - if (count == 0) return; // 快速路径 + if (count == 0) return; int[] indices = _activeIndices; Action[] callbacks = _callbacks; - // 优化7: 更高效的循环展开 int i = 0; int unrolled = count & ~3; // count - (count % 4) - // 4路展开主循环 for (; i < unrolled; i += 4) { int idx0 = indices[i]; @@ -165,7 +159,6 @@ namespace AlicizaX callbacks[idx3](payload); } - // 优化8: 使用Duff's Device处理剩余 switch (count - i) { case 3: @@ -178,14 +171,12 @@ namespace AlicizaX } } - // 额外优化方法 public static int SubscriberCount { [MethodImpl(MethodImplOptions.AggressiveInlining)] get => _activeCount; } - // 预热容量,避免运行时扩容 public static void EnsureCapacity(int capacity) { if (_callbacks.Length >= capacity) return; @@ -203,7 +194,6 @@ namespace AlicizaX } } - // 清空所有订阅 public static void Clear() { for (int i = 0; i < _activeCount; i++)