From 6c3ab86df253cccf9e5296c95107dec89a5c0ffd Mon Sep 17 00:00:00 2001 From: cneicy Date: Sun, 8 Mar 2026 17:29:31 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20RecordEditorTrace=E9=87=8C=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E6=9E=84=E9=80=A0EventHandlerInfo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Runtime/EventBus.cs | 18 ++---------------- Runtime/ListenerList.cs | 20 ++++++++++++-------- 2 files changed, 14 insertions(+), 24 deletions(-) diff --git a/Runtime/EventBus.cs b/Runtime/EventBus.cs index ef80ff6..4fee5c6 100644 --- a/Runtime/EventBus.cs +++ b/Runtime/EventBus.cs @@ -310,23 +310,9 @@ namespace ShrinkEventBus where TEvent : EventBase { if (!EnableDebugRecord) return; + var listenerList = eventArgs.GetListenerList(); for (var i = 0; i < handlers.Length; i++) - { - var handler = handlers[i]; - MethodInfo originalMethod = null; - var type = handler.GetType(); - var prop = type.GetProperty("OriginalMethod"); - if (prop != null) originalMethod = prop.GetValue(handler) as MethodInfo; - else - { - var field = type.GetField("OriginalMethod"); - if (field != null) originalMethod = field.GetValue(handler) as MethodInfo; - } - - eventArgs.GetListenerList().Add(handler.Handler, handler.Priority, handler.NumericPriority, - handler.ReceiveCanceled, handler.DebugInfo, originalMethod); - } - + listenerList.Add(handlers[i]); OnEventTriggeredForEditor?.Invoke(eventArgs, eventType.Name, GetSenderInfo()); } diff --git a/Runtime/ListenerList.cs b/Runtime/ListenerList.cs index 4336db7..7890de0 100644 --- a/Runtime/ListenerList.cs +++ b/Runtime/ListenerList.cs @@ -8,28 +8,33 @@ namespace ShrinkEventBus public class ListenerList { private readonly object _lock = new(); - private readonly List _list = new(); - private EventHandlerInfo[] _snapshot = Array.Empty(); private bool _dirty; - public void Add(Delegate handler, EventPriority priority, int numericPriority, bool receiveCanceled, string debugInfo = "", MethodInfo? originalMethod = null) { var newInfo = new EventHandlerInfo(handler, priority, numericPriority, receiveCanceled, debugInfo, originalMethod); + AddInfo(newInfo); + } + public void Add(EventHandlerInfo info) + { + AddInfo(info); + } + + private void AddInfo(EventHandlerInfo info) + { lock (_lock) { - var index = BinarySearchInsertIndex(priority, numericPriority); - _list.Insert(index, newInfo); + var index = BinarySearchInsertIndex(info.Priority, info.NumericPriority); + _list.Insert(index, info); _dirty = true; } } - public bool Remove(Delegate handler) { lock (_lock) @@ -47,7 +52,6 @@ namespace ShrinkEventBus } } - public void RemoveTarget(object target) { lock (_lock) @@ -105,7 +109,7 @@ namespace ShrinkEventBus var cmp = existing.Priority.CompareTo(priority); if (cmp == 0) - cmp = numericPriority.CompareTo(existing.NumericPriority); // 降序 + cmp = numericPriority.CompareTo(existing.NumericPriority); if (cmp < 0) lo = mid + 1;