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

feat: Use unbounded channel pattern for mailbox to improve performance #187

Closed
wants to merge 1 commit into from

Conversation

AlexanderTar
Copy link
Collaborator

@AlexanderTar AlexanderTar commented Dec 15, 2023

Inspired by https://github.com/golang-design/chann but using thread-safe buffer underneath

Copy link
Owner

@Tochemey Tochemey left a comment

Choose a reason for hiding this comment

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

I really appreciate the PR. However, I suggest that you add a new mailbox that implements the Mailbox interface.

I am also open if you are suggesting changing the Mailbox interface.

// Mailbox defines the actor mailbox.
// It is responsible for storing messages that are to be processed by the actor and is an
// actor itself. It is also the policy for how messages are enqueued and dequeued from the mailbox.
// Any implementation should be a thread-safe FIFO
type Mailbox interface {
Copy link
Owner

Choose a reason for hiding this comment

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

Please don't change the Mailbox interface. It is there to allow anyone to implement a custom mailbox. For instance, you can add your custom implementation using this interface.

Copy link
Owner

Choose a reason for hiding this comment

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

Also, I would like to know whether you are suggesting changing the mailbox interface.
The interface is there to allow a custom mailbox implementation.

@Tochemey
Copy link
Owner

@AlexanderTar Also, once you complete your new mailbox implementation, I suggest running the benchmark code to see whether there is a performance improvement.

@@ -1,6 +1,8 @@
module github.com/tochemey/goakt

go 1.20
go 1.21
Copy link
Owner

Choose a reason for hiding this comment

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

Also I suggest not to change the go version

@Tochemey
Copy link
Owner

@AlexanderTar I run your pr locally to check the performance gain and this is the result:
image

The same bench mark run on the main branch:
image

I think there is no much difference comparing the two. I am sure there are places in the code we can improve upon.

@AlexanderTar
Copy link
Collaborator Author

@Tochemey thanks for looking into this so quickly. I admit, I didn't run benchmarks test, so didn't realise the benefits are negligible.

I do, however, suggest we change Mailbox interface slightly by introducing Iterator() method (#188). It does improve benchmark slightly, but the main improvement is that we avoid busy thread in receive() method of pid. When I was experimenting with eGo for my own service, I noticed that spawning projections immediately makes CPU run at 100%, which profiler suggested is due to pid.receive() spinning, by introducing Iterator() as part of Mailbox interface we can now block until message is available instead of pointlessly spin in for waiting for a new message

@Tochemey
Copy link
Owner

@Tochemey thanks for looking into this so quickly. I admit, I didn't run benchmarks test, so didn't realise the benefits are negligible.

I do, however, suggest we change Mailbox interface slightly by introducing Iterator() method (#188). It does improve benchmark slightly, but the main improvement is that we avoid busy thread in receive() method of pid. When I was experimenting with eGo for my own service, I noticed that spawning projections immediately makes CPU run at 100%, which profiler suggested is due to pid.receive() spinning, by introducing Iterator() as part of Mailbox interface we can now block until message is available instead of pointlessly spin in for waiting for a new message

Thanks for this observation. That makes sense. I will take a look at the PR.

@Tochemey
Copy link
Owner

@AlexanderTar I merged the other PR. I am going to close this PR. Feel free to bring out any suggestion that will enhance performance 😉

@Tochemey Tochemey closed this Dec 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants