Skip to content

Commit

Permalink
eliminate mailbox delegate allocations (#6134)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaronontheweb authored Oct 5, 2022
1 parent 7441faa commit 1cee4d5
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/core/Akka/Dispatch/Mailbox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -142,7 +143,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);
}
}

Expand Down Expand Up @@ -201,7 +202,7 @@ public virtual void SetActor(ActorCell actorCell)
/// TBD
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal int CurrentStatus() { return Volatile.Read(ref _statusDotNotCallMeDirectly); }
internal int CurrentStatus() { return _statusDotNotCallMeDirectly; }

/// <summary>
/// TBD
Expand Down Expand Up @@ -251,7 +252,7 @@ private bool UpdateStatus(int oldStatus, int newStatus)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void SetStatus(int newStatus)
{
Volatile.Write(ref _statusDotNotCallMeDirectly, newStatus);
_statusDotNotCallMeDirectly = newStatus;
}

/// <summary>
Expand Down Expand Up @@ -350,13 +351,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
Expand Down

0 comments on commit 1cee4d5

Please sign in to comment.