diff --git a/Microsoft.Toolkit.Mvvm/Messaging/Internals/Object2.cs b/Microsoft.Toolkit.Mvvm/Messaging/Internals/Object2.cs deleted file mode 100644 index 230b8fed..00000000 --- a/Microsoft.Toolkit.Mvvm/Messaging/Internals/Object2.cs +++ /dev/null @@ -1,41 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.Runtime.CompilerServices; - -namespace Microsoft.Toolkit.Mvvm.Messaging.Internals -{ - /// - /// A simple type representing an immutable pair of objects. - /// - /// - /// This type replaces a simple when specifically dealing with a - /// pair of handler and recipient. Unlike , this type is readonly. - /// - internal readonly struct Object2 - { - /// - /// Initializes a new instance of the struct. - /// - /// The input handler. - /// The input recipient. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public Object2(object handler, object recipient) - { - Handler = handler; - Recipient = recipient; - } - - /// - /// The current handler (which is actually some instance. - /// - public readonly object Handler; - - /// - /// The current recipient (guaranteed to match the type constraints for . - /// - public readonly object Recipient; - } -} diff --git a/Microsoft.Toolkit.Mvvm/Messaging/WeakReferenceMessenger.cs b/Microsoft.Toolkit.Mvvm/Messaging/WeakReferenceMessenger.cs index 558b945f..d17e332b 100644 --- a/Microsoft.Toolkit.Mvvm/Messaging/WeakReferenceMessenger.cs +++ b/Microsoft.Toolkit.Mvvm/Messaging/WeakReferenceMessenger.cs @@ -169,7 +169,8 @@ namespace Microsoft.Toolkit.Mvvm.Messaging where TMessage : class where TToken : IEquatable { - ArrayPoolBufferWriter pairs; + ArrayPoolBufferWriter bufferWriter; + int i = 0; lock (this.recipientsMap) { @@ -181,7 +182,7 @@ namespace Microsoft.Toolkit.Mvvm.Messaging return message; } - pairs = ArrayPoolBufferWriter.Create(); + bufferWriter = ArrayPoolBufferWriter.Create(); // We need a local, temporary copy of all the pending recipients and handlers to // invoke, to avoid issues with handlers unregistering from messages while we're @@ -195,26 +196,30 @@ namespace Microsoft.Toolkit.Mvvm.Messaging if (map.TryGetValue(token, out object? handler)) { - pairs.Add(new Object2(handler!, pair.Key)); + bufferWriter.Add(handler!); + bufferWriter.Add(pair.Key); + i++; } } } try { - foreach (ref readonly Object2 pair in pairs.Span) + ReadOnlySpan pairs = bufferWriter.Span; + + for (int j = 0; j < i; j++) { // Just like in the other messenger, here we need an unsafe cast to be able to // invoke a generic delegate with a contravariant input argument, with a less // derived reference, without reflection. This is guaranteed to work by how the // messenger tracks registered recipients and their associated handlers, so the // type conversion will always be valid (the recipients are the rigth instances). - Unsafe.As>(pair.Handler)(pair.Recipient, message); + Unsafe.As>(pairs[2 * j])(pairs[(2 * j) + 1], message); } } finally { - pairs.Dispose(); + bufferWriter.Dispose(); } return message;