How Clojure's agents compare to Scala's actors?
Asked Answered
A

1

29

I wrote a simulation of the Ring network topology in Scala (source here) (Scala 2.8 RC7) and Clojure (source here) (Clojure 1.1) for a comparison of Actors and Agents.

While the Scala version shows almost constant message exchange rate as I increase the number of nodes in network from 100 to 1000000, the Clojure version shows message rates which decrease with the increase in the number of nodes. Also during a single run, the message rate in Clojure version decreases as the time passes.

So I am curious about how the Scala's Actors compare to Clojure's Agents? Are Agents inherently less concurrent than Actors or is the code inefficiently written (autoboxing?)?

PS: I noted that the memory usage in the Scala version increases a lot with the increase in the number of nodes (> 500 MB for 1 million nodes) while the Clojure one uses much less memory (~ 100 MB for 1 million nodes).

Edit:

Both the versions are running on same JVM with all the JVM args and Actor and Agent configuration parameters set as default. On my machine, the Scala version gives a message rate of around 5000 message/sec consistently for 100 to 1 million nodes, whereas the Clojure version starts with 60000 message/sec for 100 nodes which decreases to 200 messages/sec for 1 million nodes.

Edit 2

Turns out that my Clojure version was inefficiently written. I changed the type of nodes collection from list to vector and now it shows consistent behaviour: 100000 message/sec for 100 nodes and 80000 message/sec for 100000 nodes. So Clojure Agents seem to be faster than Scala Actors. I have updated the linked sources too.

Absorbing answered 15/7, 2010 at 19:38 Comment(7)
There is a way to make a Scala actor "threadless". I don't know much about Clojure, but it would be nice if you posted (relevant parts of) your source code.Gritty
He linked to the source for both versions.Flashlight
In your Edit 2 you probably mean inefficiently?Cadaver
I don't know Scala so the only thing I can say here is that there are two Acters one (witch does not scale to good) is one actor per thread and the other is Event based so maybe thats a way to improv your scala code? The only thing I can say about Agents is that the use threats from a threadpool. I would be intressting to here a reply of an expert. Maybe ask on the Clojure and Scala Groups.Gadget
@Gadget i have used the event based type here.Absorbing
See the benchmark implementation in Akka mentioned by Viktor, there is one Akka implementation and one standard Scala actor implementationGuarantee
in edit 2 you speak of using 100,000 nodes yet originally state using 1e6 nodes for scala. is this a typo? just curious because you report that clojure runs faster than scala. just curiousFenton
R
28

[Disclaimer: I'm on the Akka team]

A Clojure Agent is a different beast from a Scala actor, most notably if you think about who controls the behavior. In Agents the behavior is defined outside and is pushed to the Agent, and in Actors the behavior is defined inside the Actor.

Without knowing anything about your code I really cannot say much, are you using the same JVM parameters, warming things up the same, sensible settings for Actors vs. sensible settings for Agents, or are they tuned separately?

As a side note: Akka has an implementation of the ring bench located here: http://github.com/jboner/akka-bench/tree/master/ring/

Would be interesting to see what the result is compared to your Clojure test on your machine.

Rathenau answered 15/7, 2010 at 21:7 Comment(2)
This is almost three years old, now :) Would be very interesting to see an update now that clojure is around version 1.5.1 and scala is around 2.10.1Reborn
@Reborn out of interest I updated the code to run on latest Scala (2.11.2), Akka (2.3.5) and Clojure (1.6.0) versions. I get similar results for Clojure (~80K msg/sec for 1K to 100K nodes), but much better performance from Scala (+1M msg/sec for 10M nodes). This is probably to be expected since Akka team would be actively optimizing for message throughput while Clojure solution is more general-purpose.Krak

© 2022 - 2024 — McMap. All rights reserved.