perf(GFramework.Core): 优化PriorityEvent事件处理器性能

- 在MergeAndSortHandlers方法中创建_handlers和_contextHandlers的快照副本
- 避免在迭代期间直接访问集合可能发生的修改问题
- 提高事件处理时的性能表现和稳定性
This commit is contained in:
GeWuYou 2026-02-25 21:33:41 +08:00 committed by gewuyou
parent 78e0c75641
commit c454fa1acf

View File

@ -191,11 +191,13 @@ public class PriorityEvent<T> : IEvent
private List<(int Priority, Action? Handler, Action<EventContext<T>>? ContextHandler, bool IsContext)>
MergeAndSortHandlers(T t)
{
var normalSnapshot = _handlers.ToArray();
var contextSnapshot = _contextHandlers.ToArray();
// 使用快照避免迭代期间修改
return _handlers
return normalSnapshot
.Select(h => (h.Priority, Handler: (Action?)(() => h.Handler.Invoke(t)),
ContextHandler: (Action<EventContext<T>>?)null, IsContext: false))
.Concat(_contextHandlers
.Concat(contextSnapshot
.Select(h => (h.Priority, Handler: (Action?)null,
ContextHandler: (Action<EventContext<T>>?)h.Handler, IsContext: true)))
.OrderByDescending(h => h.Priority)