Update EventContainer.cs
This commit is contained in:
parent
d8a0f7a7c9
commit
0197adc5c6
@ -14,7 +14,6 @@ namespace AlicizaX
|
||||
private static readonly int InitialSize = EventInitialSize<TPayload>.Size;
|
||||
private static readonly int TypeId;
|
||||
|
||||
// 优化3: SoA布局,提升缓存命中率
|
||||
private static Action<TPayload>[] _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<TPayload>[] 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++)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user