Skip to content

Commit 89efbea

Browse files
committed
Small code refactoring and improvements
1 parent b0f2edd commit 89efbea

File tree

1 file changed

+12
-31
lines changed

1 file changed

+12
-31
lines changed

Microsoft.Toolkit.Mvvm/Messaging/Messenger.cs

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,6 @@ public void Register<TRecipient, TMessage, TToken>(TRecipient recipient, TToken
131131
// Treat the input delegate as if it was covariant (see comments below in the Send method)
132132
registeredHandler = handler;
133133

134-
// Update the total counter for handlers for the current type parameters
135-
mapping.TotalHandlersCount++;
136-
137134
// Make sure this registration map is tracked for the current recipient
138135
ref HashSet<IMapping>? set = ref this.recipientsMap.GetOrAddValueRef(key);
139136

@@ -171,8 +168,6 @@ public void UnregisterAll(object recipient)
171168
// The handlers map is the IDictionary<TToken, TMessage> instance for the mapping.
172169
int handlersCount = Unsafe.As<IDictionarySlim>(handlersMap).Count;
173170

174-
mapping.TotalHandlersCount -= handlersCount;
175-
176171
if (mapping.Count == 0)
177172
{
178173
// Maps here are really of type Mapping<,> and with unknown type arguments.
@@ -259,15 +254,6 @@ public void UnregisterAll<TToken>(object recipient, TToken token)
259254
// for the current message type (unknown from here).
260255
if (holder.Remove(token))
261256
{
262-
// As above, we need to update the total number of registered handlers for the map.
263-
// In this case we also know that the current TToken type parameter is of interest
264-
// for the current method, as we're only unsubscribing handlers using that token.
265-
// This is because we're already working on the final <TToken, TMessage> mapping,
266-
// which associates a single handler with a given token, for a given recipient.
267-
// This means that we don't have to retrieve the count to subtract in this case,
268-
// we're just removing a single handler at a time. So, we just decrement the total.
269-
Unsafe.As<IMapping>(map).TotalHandlersCount--;
270-
271257
if (holder.Count == 0)
272258
{
273259
// If the map is empty, remove the recipient entirely from its container
@@ -335,9 +321,6 @@ public void Unregister<TMessage, TToken>(object recipient, TToken token)
335321
// Remove the target handler
336322
if (dictionary!.Remove(token))
337323
{
338-
// Decrement the total count, as above
339-
mapping.TotalHandlersCount--;
340-
341324
// If the map is empty, it means that the current recipient has no remaining
342325
// registered handlers for the current <TMessage, TToken> combination, regardless,
343326
// of the specific token value (ie. the channel used to receive messages of that type).
@@ -376,12 +359,18 @@ public unsafe TMessage Send<TMessage, TToken>(TMessage message, TToken token)
376359
// Check whether there are any registered recipients
377360
_ = TryGetMapping(out Mapping<TMessage, TToken>? mapping);
378361

379-
// We need to make a local copy of the currently registered handlers,
380-
// since users might try to unregister (or register) new handlers from
381-
// inside one of the currently existing handlers. We can use memory pooling
382-
// to reuse arrays, to minimize the average memory usage. In practice,
383-
// we usually just need to pay the small overhead of copying the items.
384-
int totalHandlersCount = mapping?.TotalHandlersCount ?? 0;
362+
// We need to make a local copy of the currently registered handlers, since users might
363+
// try to unregister (or register) new handlers from inside one of the currently existing
364+
// handlers. We can use memory pooling to reuse arrays, to minimize the average memory
365+
// usage. In practice, we usually just need to pay the small overhead of copying the items.
366+
// The current mapping contains all the currently registered recipients and handlers for
367+
// the <TMessage, TToken> combination in use. In the worst case scenario, all recipients
368+
// will have a registered handler with a token matching the input one, meaning that we could
369+
// have at worst a number of pending handlers to invoke equal to the total number of recipient
370+
// in the mapping. This relies on the fact that tokens are unique, and that there is only
371+
// one handler associated with a given token. We can use this upper bound as the requested
372+
// size for each array rented from the pool, which guarantees that we'll have enough space.
373+
int totalHandlersCount = mapping?.Count ?? 0;
385374

386375
if (totalHandlersCount == 0)
387376
{
@@ -541,9 +530,6 @@ public Mapping()
541530

542531
/// <inheritdoc/>
543532
public Type2 TypeArguments { get; }
544-
545-
/// <inheritdoc/>
546-
public int TotalHandlersCount { get; set; }
547533
}
548534

549535
/// <summary>
@@ -556,11 +542,6 @@ private interface IMapping : IDictionarySlim<Recipient>
556542
/// Gets the <see cref="Type2"/> instance representing the current type arguments.
557543
/// </summary>
558544
Type2 TypeArguments { get; }
559-
560-
/// <summary>
561-
/// Gets or sets the total number of handlers in the current instance.
562-
/// </summary>
563-
int TotalHandlersCount { get; set; }
564545
}
565546

566547
/// <summary>

0 commit comments

Comments
 (0)