Reputation: 13
I'm new to scala and akka and I came across code that looked something like this:
object Foo extends App {
class Bar extends Actor with ActorLogging{
val graph: TitanGraph = _
private def setGraph() = {
graph = TitanFactory.open(...)
}
setGraph()
...
}
def startBar() {
val barSystem = ActorSystem("BarSystem")
for(i <- 0 until numActors) {
val barActor = barSystem.actorOf(Props[Bar], name="BarActor" + i)
...
barActor ! Start
}
}
startBar
}
Does this have any effect on performance as compared to?:
object Foo extends App {
override def main(args: Array[String]): Unit = {
val barSystem = ActorSystem("BarSystem")
for(i <- 0 until numActors) {
val barActor = barSystem.actorOf(Props[Bar], name="BarActor" + i)
...
barActor ! Start
}
}
}
object Bar {
val graph: TitanGraph = _
private def setGraph() = {
graph = TitanFactory.open(...)
}
setGraph()
def props = Props[Bar]
...
}
class Bar extends Actor with ActorLogging{
...
}
In the first case, you're creating multiple instances of the graph but in the second case, I assume that you're using a single instance shared across workers?
Also, I read somewhere that it is good practice to keep the Actor's props factory in the companion object but not sure if the previous code breaks actor encapsulation or if it affects performance at all.
Upvotes: 1
Views: 330
Reputation: 15472
Whether you place an Actor inside an object or outside does not change anything besides the class file name. Concerning sharing a single Actor object instance, that is illegal and will not work, but luckily you cannot easily fall into this trap.
Upvotes: 1