From 352d9a1f41f37da5027b834085ea29f9f84187ae Mon Sep 17 00:00:00 2001 From: Aaron Stannard Date: Wed, 5 Oct 2022 09:22:09 -0500 Subject: [PATCH] eliminate mailbox delegate allocations (#6134) --- src/core/Akka/Dispatch/Mailbox.cs | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/core/Akka/Dispatch/Mailbox.cs b/src/core/Akka/Dispatch/Mailbox.cs index d2168005b3d..07412b0ef88 100644 --- a/src/core/Akka/Dispatch/Mailbox.cs +++ b/src/core/Akka/Dispatch/Mailbox.cs @@ -11,6 +11,7 @@ using System.Runtime.CompilerServices; using System.Threading; using Akka.Actor; +using Akka.Actor.Internal; using Akka.Configuration; using Akka.Dispatch.MessageQueues; using Akka.Dispatch.SysMsg; @@ -147,7 +148,7 @@ internal LatestFirstSystemMessageList SystemQueue { // Note: contrary how it looks, there is no allocation here, as SystemMessageList is a value class and as such // it just exists as a typed view during compile-time. The actual return type is still SystemMessage. - return new LatestFirstSystemMessageList(Volatile.Read(ref _systemQueueDoNotCallMeDirectly)); + return new LatestFirstSystemMessageList(_systemQueueDoNotCallMeDirectly); } } @@ -271,7 +272,7 @@ private bool UpdateStatus(int oldStatus, int newStatus) [MethodImpl(MethodImplOptions.AggressiveInlining)] private void SetStatus(int newStatus) { - Volatile.Write(ref _statusDotNotCallMeDirectly, newStatus); + _statusDotNotCallMeDirectly = newStatus; } /// @@ -371,13 +372,19 @@ public void Run() { try { - if (!IsClosed()) // Volatile read, needed here + if (IsClosed()) return; // Volatile read, needed here + + var tmp = InternalCurrentActorCellKeeper.Current; + InternalCurrentActorCellKeeper.Current = Actor; + try + { + ProcessAllSystemMessages(); // First, deal with any system messages + ProcessMailbox(); // Then deal with messages + } + finally { - Actor.UseThreadContext(() => - { - ProcessAllSystemMessages(); // First, deal with any system messages - ProcessMailbox(); // Then deal with messages - }); + //ensure we set back the old context + InternalCurrentActorCellKeeper.Current = tmp; } } finally