Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eliminate mailbox delegate allocations #6134

Conversation

Aaronontheweb
Copy link
Member

Changes

Reduces delegate allocations while processing actor mailbox contents to zero - eliminates the vast majority of allocations that occur during mailbox processing.

Checklist

For significant changes, please ensure that the following have been completed (delete if not relevant):

Latest v1.4 Benchmarks

Running on .NET 6 - note: I modified the MailboxThroughputBenchmarks to only use the CallingThreadDispatcher as that's a cleaner signal for measuring throughput impact of these changes, which otherwise won't show up as easily using the .NET ThreadPool.

BenchmarkDotNet=v0.13.1, OS=Windows 10.0.19044.2006 (21H2)
AMD Ryzen 7 1700, 1 CPU, 16 logical and 8 physical cores
.NET SDK=6.0.201
  [Host]     : .NET 6.0.3 (6.0.322.12309), X64 RyuJIT
  Job-OMCNHR : .NET 6.0.3 (6.0.322.12309), X64 RyuJIT

InvocationCount=1  UnrollFactor=1  
Method MsgCount Mean Error StdDev Allocated
EnqueuePerformance 10000 214.1 μs 3.84 μs 3.59 μs 385 KB
RunPerformance 10000 2,236.2 μs 22.53 μs 21.07 μs 21 KB
EnqueuePerformance 100000 2,210.9 μs 41.63 μs 36.90 μs 3,074 KB
RunPerformance 100000 10,839.9 μs 65.64 μs 58.19 μs 203 KB

This PR's Benchmarks

BenchmarkDotNet=v0.13.1, OS=Windows 10.0.19044.2006 (21H2)
AMD Ryzen 7 1700, 1 CPU, 16 logical and 8 physical cores
.NET SDK=6.0.201
  [Host]     : .NET 6.0.3 (6.0.322.12309), X64 RyuJIT
  Job-HWVIXV : .NET 6.0.3 (6.0.322.12309), X64 RyuJIT

InvocationCount=1  UnrollFactor=1  
Method MsgCount Mean Error StdDev Allocated
EnqueuePerformance 10000 218.3 μs 4.28 μs 7.27 μs 394,736 B
RunPerformance 10000 2,150.5 μs 21.07 μs 18.68 μs 1,008 B
EnqueuePerformance 100000 2,232.1 μs 42.88 μs 42.11 μs 3,148,016 B
RunPerformance 100000 9,565.5 μs 123.70 μs 177.41 μs 1,008 B

Perf Summary

Reduces allocations on 100,000 msgs processed from 203kb to 1,008 bytes. Improves throughput slightly.

@Aaronontheweb Aaronontheweb added akka-actor perf akka.net v1.4 Issues affecting Akka.NET v1.4 labels Oct 5, 2022
@Aaronontheweb Aaronontheweb added this to the 1.4.44 milestone Oct 5, 2022
Copy link
Member Author

@Aaronontheweb Aaronontheweb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed my own PR

@@ -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));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed - field is already marked as volatile

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed - field is already marked as volatile

@@ -251,7 +252,7 @@ private bool UpdateStatus(int oldStatus, int newStatus)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void SetStatus(int newStatus)
{
Volatile.Write(ref _statusDotNotCallMeDirectly, newStatus);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed - field is already marked as volatile

if (!IsClosed()) // Volatile read, needed here
if (IsClosed()) return; // Volatile read, needed here

var tmp = InternalCurrentActorCellKeeper.Current;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inlined the contents of ActorCell.UseThreadContext here.

Copy link
Contributor

@Arkatufus Arkatufus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Aaronontheweb Aaronontheweb merged commit 1cee4d5 into akkadotnet:v1.4 Oct 5, 2022
@Aaronontheweb Aaronontheweb deleted the perf-eliminate-mailbox-delegates branch October 5, 2022 14:22
@Aaronontheweb Aaronontheweb mentioned this pull request Oct 5, 2022
3 tasks
Aaronontheweb added a commit to Aaronontheweb/akka.net that referenced this pull request Oct 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
akka.net v1.4 Issues affecting Akka.NET v1.4 akka-actor perf
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants