perf: reduce diagnostics and queued dispatch allocations in 2.3.2
This commit is contained in:
8 files changed
+152
-36
No files matched your search
+35
-7
@@ -57,6 +57,8 @@ namespace ShrinkEventBus
|
||||
private static readonly IShrinkEventBus DefaultBus = CreateDefaultBus();
|
||||
private static Action<IShrinkEvent, Type, ShrinkBusKey>? _posted;
|
||||
private static Action<ShrinkEventTrace>? _detailedPosted;
|
||||
private static readonly object DetailedObserverGate = new();
|
||||
private static Action<ShrinkEventTrace>[]? _detailedObserverSnapshot;
|
||||
|
||||
internal static event Action<IShrinkEvent, Type, ShrinkBusKey> Posted
|
||||
{
|
||||
@@ -78,14 +80,41 @@ namespace ShrinkEventBus
|
||||
|
||||
internal static event Action<ShrinkEventTrace> DetailedPosted
|
||||
{
|
||||
add => _detailedPosted += value;
|
||||
remove => _detailedPosted -= value;
|
||||
add
|
||||
{
|
||||
lock (DetailedObserverGate)
|
||||
{
|
||||
_detailedPosted += value;
|
||||
PublishDetailedObserverSnapshot();
|
||||
}
|
||||
}
|
||||
remove
|
||||
{
|
||||
lock (DetailedObserverGate)
|
||||
{
|
||||
_detailedPosted -= value;
|
||||
PublishDetailedObserverSnapshot();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void PublishDetailedObserverSnapshot()
|
||||
{
|
||||
var invocationList = _detailedPosted?.GetInvocationList();
|
||||
Action<ShrinkEventTrace>[]? snapshot = null;
|
||||
if (invocationList != null)
|
||||
{
|
||||
snapshot = new Action<ShrinkEventTrace>[invocationList.Length];
|
||||
for (var i = 0; i < snapshot.Length; i++)
|
||||
snapshot[i] = (Action<ShrinkEventTrace>)invocationList[i];
|
||||
}
|
||||
Volatile.Write(ref _detailedObserverSnapshot, snapshot);
|
||||
}
|
||||
|
||||
internal static bool HasDetailedPostedObservers
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => Volatile.Read(ref _detailedPosted) != null;
|
||||
get => Volatile.Read(ref _detailedObserverSnapshot) != null;
|
||||
}
|
||||
|
||||
public static IShrinkEventBus Default => DefaultBus;
|
||||
@@ -212,15 +241,14 @@ namespace ShrinkEventBus
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal static void NotifyDetailedPosted(ShrinkEventTrace trace)
|
||||
{
|
||||
var observers = Volatile.Read(ref _detailedPosted);
|
||||
var observers = Volatile.Read(ref _detailedObserverSnapshot);
|
||||
if (observers == null)
|
||||
return;
|
||||
var invocationList = observers.GetInvocationList();
|
||||
for (var i = 0; i < invocationList.Length; i++)
|
||||
for (var i = 0; i < observers.Length; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
((Action<ShrinkEventTrace>)invocationList[i])(trace);
|
||||
observers[i](trace);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user