further improve performance

This commit is contained in:
MidoriKami 2025-11-28 09:08:24 -08:00
parent c3e3e4aa85
commit f8725e5f37
2 changed files with 67 additions and 34 deletions

View file

@ -38,7 +38,8 @@ internal unsafe class AddonLifecycle : IInternalDisposableService
/// <summary> /// <summary>
/// Gets a list of all AddonLifecycle Event Listeners. /// Gets a list of all AddonLifecycle Event Listeners.
/// </summary> /// </summary>
internal Dictionary<AddonEvent, HashSet<AddonLifecycleEventListener>> EventListeners { get; } = []; /// Mapping is: EventType -> AddonName -> ListenerList
internal Dictionary<AddonEvent, Dictionary<string, HashSet<AddonLifecycleEventListener>>> EventListeners { get; } = [];
/// <inheritdoc/> /// <inheritdoc/>
void IInternalDisposableService.DisposeService() void IInternalDisposableService.DisposeService()
@ -61,8 +62,18 @@ internal unsafe class AddonLifecycle : IInternalDisposableService
/// <param name="listener">The listener to register.</param> /// <param name="listener">The listener to register.</param>
internal void RegisterListener(AddonLifecycleEventListener listener) internal void RegisterListener(AddonLifecycleEventListener listener)
{ {
this.EventListeners.TryAdd(listener.EventType, [ listener ]); if (!this.EventListeners.ContainsKey(listener.EventType))
this.EventListeners[listener.EventType].Add(listener); {
this.EventListeners.TryAdd(listener.EventType, []);
}
// Note: string.Empty is a valid addon name, as that will trigger on any addon for this event type
if (!this.EventListeners[listener.EventType].ContainsKey(listener.AddonName))
{
this.EventListeners[listener.EventType].TryAdd(listener.AddonName, []);
}
this.EventListeners[listener.EventType][listener.AddonName].Add(listener);
} }
/// <summary> /// <summary>
@ -71,9 +82,12 @@ internal unsafe class AddonLifecycle : IInternalDisposableService
/// <param name="listener">The listener to unregister.</param> /// <param name="listener">The listener to unregister.</param>
internal void UnregisterListener(AddonLifecycleEventListener listener) internal void UnregisterListener(AddonLifecycleEventListener listener)
{ {
if (this.EventListeners.TryGetValue(listener.EventType, out var listenerList)) if (this.EventListeners.TryGetValue(listener.EventType, out var addonListeners))
{ {
listenerList.Remove(listener); if (addonListeners.TryGetValue(listener.AddonName, out var addonListener))
{
addonListener.Remove(listener);
}
} }
} }
@ -86,22 +100,37 @@ internal unsafe class AddonLifecycle : IInternalDisposableService
internal void InvokeListenersSafely(AddonEvent eventType, AddonArgs args, [CallerMemberName] string blame = "") internal void InvokeListenersSafely(AddonEvent eventType, AddonArgs args, [CallerMemberName] string blame = "")
{ {
// Early return if we don't have any listeners of this type // Early return if we don't have any listeners of this type
if (!this.EventListeners.TryGetValue(eventType, out var listenerList)) return; if (!this.EventListeners.TryGetValue(eventType, out var addonListeners)) return;
// Do not use linq; this is a high-traffic function, and more heap allocations avoided, the better. // Handle listeners for this event type that don't care which addon is triggering it
foreach (var listener in listenerList) if (addonListeners.TryGetValue(string.Empty, out var globalListeners))
{
foreach (var listener in globalListeners)
{ {
// Match on string.empty for listeners that want events for all addons.
if (!string.IsNullOrWhiteSpace(listener.AddonName) && !args.IsAddon(listener.AddonName))
continue;
try try
{ {
listener.FunctionDelegate.Invoke(eventType, args); listener.FunctionDelegate.Invoke(eventType, args);
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e, $"Exception in {blame} during {eventType} invoke."); Log.Error(e, $"Exception in {blame} during {eventType} invoke, for global addon event listener.");
}
}
}
// Handle listeners that are listening for this addon and event type specifically
if (addonListeners.TryGetValue(args.AddonName, out var addonListener))
{
foreach (var listener in addonListener)
{
try
{
listener.FunctionDelegate.Invoke(eventType, args);
}
catch (Exception e)
{
Log.Error(e, $"Exception in {blame} during {eventType} invoke, for specific addon {args.AddonName}.");
}
} }
} }
} }

View file

@ -2,7 +2,8 @@ using System.Diagnostics.CodeAnalysis;
using Dalamud.Bindings.ImGui; using Dalamud.Bindings.ImGui;
using Dalamud.Game.Addon.Lifecycle; using Dalamud.Game.Addon.Lifecycle;
using Dalamud.Interface.Utility; using Dalamud.Interface.Utility.Raii;
using Dalamud.Utility;
namespace Dalamud.Interface.Internal.Windows.Data.Widgets; namespace Dalamud.Interface.Internal.Windows.Data.Widgets;
@ -57,35 +58,38 @@ public class AddonLifecycleWidget : IDataWindowWidget
{ {
if (!this.Ready) return; if (!this.Ready) return;
foreach (var (listenerType, listeners) in this.AddonLifecycle.EventListeners) foreach (var (eventType, addonListeners) in this.AddonLifecycle.EventListeners)
{ {
if (ImGui.CollapsingHeader(listenerType.ToString())) using var eventId = ImRaii.PushId(eventType.ToString());
if (ImGui.CollapsingHeader(eventType.ToString()))
{ {
ImGui.Indent(); using var eventIndent = ImRaii.PushIndent();
if (addonListeners.Count == 0)
{
ImGui.Text("No Addons Registered for Event"u8);
}
foreach (var (addonName, listeners) in addonListeners)
{
using var addonId = ImRaii.PushId(addonName);
if (ImGui.CollapsingHeader(addonName.IsNullOrEmpty() ? "GLOBAL" : addonName))
{
using var addonIndent = ImRaii.PushIndent();
if (listeners.Count == 0) if (listeners.Count == 0)
{ {
ImGui.Text("No Listeners Registered for Event"u8); ImGui.Text("No Listeners Registered for Event"u8);
} }
if (ImGui.BeginTable("AddonLifecycleListenersTable"u8, 2))
{
ImGui.TableSetupColumn("##AddonName"u8, ImGuiTableColumnFlags.WidthFixed, 100.0f * ImGuiHelpers.GlobalScale);
ImGui.TableSetupColumn("##MethodInvoke"u8, ImGuiTableColumnFlags.WidthStretch);
foreach (var listener in listeners) foreach (var listener in listeners)
{ {
ImGui.TableNextColumn();
ImGui.Text(listener.AddonName is "" ? "GLOBAL" : listener.AddonName);
ImGui.TableNextColumn();
ImGui.Text($"{listener.FunctionDelegate.Method.DeclaringType?.FullName ?? "Unknown Declaring Type"}::{listener.FunctionDelegate.Method.Name}"); ImGui.Text($"{listener.FunctionDelegate.Method.DeclaringType?.FullName ?? "Unknown Declaring Type"}::{listener.FunctionDelegate.Method.Name}");
} }
}
ImGui.EndTable(); }
}
ImGui.Unindent();
} }
} }
} }