perf: RecordEditorTrace里重复构造EventHandlerInfo

This commit is contained in:
cneicy committed 2026-03-08 17:29:31 +08:00
1 parent 7d9e0566dd
commit 6c3ab86df2
2 files changed
+14 -24

No files matched your search

+2 -16
View File
@@ -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());
}
+12 -8
View File
@@ -8,28 +8,33 @@ namespace ShrinkEventBus
public class ListenerList
{
private readonly object _lock = new();
private readonly List<EventHandlerInfo> _list = new();
private EventHandlerInfo[] _snapshot = Array.Empty<EventHandlerInfo>();
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;