6
6
using System . Buffers ;
7
7
using System . Collections . Generic ;
8
8
using System . Runtime . CompilerServices ;
9
- using System . Runtime . InteropServices ;
10
9
using System . Threading ;
11
10
using Microsoft . Collections . Extensions ;
12
11
@@ -404,17 +403,19 @@ public unsafe TMessage Send<TMessage, TToken>(TMessage message, TToken token)
404
403
lock ( this . recipientsMap )
405
404
{
406
405
// Check whether there are any registered recipients
407
- if ( ! TryGetMapping ( out Mapping < TMessage , TToken > ? mapping ) )
408
- {
409
- return message ;
410
- }
406
+ _ = TryGetMapping ( out Mapping < TMessage , TToken > ? mapping ) ;
411
407
412
408
// We need to make a local copy of the currently registered handlers,
413
409
// since users might try to unregister (or register) new handlers from
414
410
// inside one of the currently existing handlers. We can use memory pooling
415
411
// to reuse arrays, to minimize the average memory usage. In practice,
416
412
// we usually just need to pay the small overhead of copying the items.
417
- int totalHandlersCount = mapping ! . TotalHandlersCount ;
413
+ int totalHandlersCount = mapping ? . TotalHandlersCount ?? 0 ;
414
+
415
+ if ( totalHandlersCount == 0 )
416
+ {
417
+ return message ;
418
+ }
418
419
419
420
handlers = ArrayPool < object > . Shared . Rent ( totalHandlersCount ) ;
420
421
recipients = ArrayPool < object > . Shared . Rent ( totalHandlersCount ) ;
@@ -428,7 +429,7 @@ public unsafe TMessage Send<TMessage, TToken>(TMessage message, TToken token)
428
429
// handlers for different tokens. We can reuse the same variable
429
430
// to count the number of matching handlers to invoke later on.
430
431
// This will be the array slice with valid actions in the rented buffer.
431
- var mappingEnumerator = mapping . GetEnumerator ( ) ;
432
+ var mappingEnumerator = mapping ! . GetEnumerator ( ) ;
432
433
433
434
// Explicit enumerator usage here as we're using a custom one
434
435
// that doesn't expose the single standard Current property.
0 commit comments