@@ -131,9 +131,6 @@ public void Register<TRecipient, TMessage, TToken>(TRecipient recipient, TToken
131
131
// Treat the input delegate as if it was covariant (see comments below in the Send method)
132
132
registeredHandler = handler ;
133
133
134
- // Update the total counter for handlers for the current type parameters
135
- mapping . TotalHandlersCount ++ ;
136
-
137
134
// Make sure this registration map is tracked for the current recipient
138
135
ref HashSet < IMapping > ? set = ref this . recipientsMap . GetOrAddValueRef ( key ) ;
139
136
@@ -171,8 +168,6 @@ public void UnregisterAll(object recipient)
171
168
// The handlers map is the IDictionary<TToken, TMessage> instance for the mapping.
172
169
int handlersCount = Unsafe . As < IDictionarySlim > ( handlersMap ) . Count ;
173
170
174
- mapping . TotalHandlersCount -= handlersCount ;
175
-
176
171
if ( mapping . Count == 0 )
177
172
{
178
173
// Maps here are really of type Mapping<,> and with unknown type arguments.
@@ -259,15 +254,6 @@ public void UnregisterAll<TToken>(object recipient, TToken token)
259
254
// for the current message type (unknown from here).
260
255
if ( holder . Remove ( token ) )
261
256
{
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
-
271
257
if ( holder . Count == 0 )
272
258
{
273
259
// 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)
335
321
// Remove the target handler
336
322
if ( dictionary ! . Remove ( token ) )
337
323
{
338
- // Decrement the total count, as above
339
- mapping . TotalHandlersCount -- ;
340
-
341
324
// If the map is empty, it means that the current recipient has no remaining
342
325
// registered handlers for the current <TMessage, TToken> combination, regardless,
343
326
// 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)
376
359
// Check whether there are any registered recipients
377
360
_ = TryGetMapping ( out Mapping < TMessage , TToken > ? mapping ) ;
378
361
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 ;
385
374
386
375
if ( totalHandlersCount == 0 )
387
376
{
@@ -541,9 +530,6 @@ public Mapping()
541
530
542
531
/// <inheritdoc/>
543
532
public Type2 TypeArguments { get ; }
544
-
545
- /// <inheritdoc/>
546
- public int TotalHandlersCount { get ; set ; }
547
533
}
548
534
549
535
/// <summary>
@@ -556,11 +542,6 @@ private interface IMapping : IDictionarySlim<Recipient>
556
542
/// Gets the <see cref="Type2"/> instance representing the current type arguments.
557
543
/// </summary>
558
544
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 ; }
564
545
}
565
546
566
547
/// <summary>
0 commit comments