Actors library: receive vs. react

In Monday's meeting Dustin mentioned that in his own testing with many actors making an HTTP request, many (too many) threads were created for receive but it seemed like none or two few were created for react. (Please correct me if I've misstated.) React is supposed to lower the required number of expensive OS-level threads, but at the same time it is expected to distribute the workload among actors and across available processors. I did a little test to better familiarize myself with the two options, using a fibonacci numbers implementation from somewhere on the web.

You can paste this into the Scala interpreter. I've tried it with values of n from 10 to 50. The surprising thing for me is that even with receive it does not immediately create n threads, though start is called for every actor by the `actor` factory method, before any actors are sent messages. In my process monitory, 6 threads are created after calling test. The actors are brought to life roughly 3 at a time. As actors finish they are garbage collected and the threads are freed, so that the total thread count remains steady until processing is complete; at that point, 2 threads are freed.

To exercise both processor cores on my system I found that values of n greater than 10 are necessary. Whether it's the library, JVM, or OS, something is holding back on distributing threads across cores for at least a few seconds, and then both cores are fully used and the aggregate idle drops to 0%.

With these apparent optimizations of receive, react does not behave any differently for this case. It creates the same number of threads and frees the same number when all processing has been completed. It scales out to both cores in about the same amount of time, and completes in about the same span. But as far as "is react working, despite its low thread count?", it is. With processing intensive actors it will make use of all available processors, the same as receive.

Of course, receive really is different from react and to see that you can uncomment the last expression of `test` so that `all` is returned to the interpreter. With that change, the actors can not be garbage collected as they complete their work and they will retain their threads. That will be one thread for all n actors with receive, plus a few other threads. React only uses and retains the same 7 threads as before. In either case, the background actors make the interpreter a little stubborn about exiting (ctrl+c).

We're about:

Scala is a programming language with an advanced type system that compiles into bytecode for widely used virtual machines. We host technical meetups on intermediate and advanced topics in the language, as well as workshops for Scala Types of any skill level. Some of our past meetups appear on our vimeo channel.