-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Sending messages in recursive functions performance degradation #4424
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
Comments
@tinglei8 there are two important things to consider here:
You will get better performance. In your current version, you are building a new tuple on every operation, that will eventually trigger the GC. And you will trigger it more frequently when the amount of messages to send increases which in turn will take more time because the GC may also scan your message queue (and you are sending messages to yourself). That said, two considerations:
|
Thank you for this fantastic example, it was highly educative. If you have more questions, please ask them! |
Thanks for the quick comment, looking forward to the improvement in erlang compiler! |
Environment
Current behavior
In following test snippet, we send two messages from a process to another process, and repeat for 5000, 50000, 500000 times. It's surprising to notice the send performance degrades dramatically with respect to message total number. In one of my run it shows sending 5000 count is in 5ms, while 500000 count is in 7.3s.
Even more confusing, uncomment either [Code Block #1] or [Code Block #2], and we get expected linear performance with respect to message count.
[Code Block #1] use Enum.each() instead of recursive functions.
[Code Block #2] simply send the message in the recursive function body instead of calling a third module function to send.
Code below:
To reproduce, save the code as test.exs, run it with "elixir test.exs".
Expected behavior
Sending of messages should be in linear performance with respect to message count, regardless of how and where the sending is done.
I'm not sure this is a erlang bug or elixir bug yet. Selective receive is already ruled out by raising exceptions on unmatched messages.
The text was updated successfully, but these errors were encountered: