1

Here are two definitions both achieving the same result:

  def sendTrigger(teamId:Long, data:String) {
    EngineSync.browserSockets.collect{ case ((i,(u,t)),s) => if(t==teamId) { s.send(data) } }
  }

  def sendTrigger(teamId:Long, data:String) {
    EngineSync.browserSockets.foreach{ case ((i,(u,t)),s) => if(t==teamId) { s.send(data) } }
  }

What's happening is I am looping through a list of sockets and filtering them to send data. Being a newbie to Scala, I am concerned about performance when this begins to scale. From what I understand foreach performance is poor compared to other methods, does anyone know if collect would fare better or if this is the wrong approach entirely?

crockpotveggies
  • 12,682
  • 12
  • 70
  • 140
  • 3
    Why don't you save a `Map` `teamId -> List(Socket)`? Lookup is cheap and you get all the relevant socket instances easily – Leo Sep 12 '12 at 18:55
  • Definitely considering that ;) how would that compare to a class that stored information about each socket (in some instances, the socket is relevant to a user and not a team)? – crockpotveggies Sep 12 '12 at 19:41
  • 2
    Performance of `foreach` is just fine. `collect` will be slower because it has to be build a new collection. Also - don't nest tuples like that; use a case class. – Luigi Plinge Sep 12 '12 at 20:55

1 Answers1

1

Looping through a fair sized collection vs. performing network IO (at least when blocking) are entirely different scale operations, therefore I would not worry about performance issures at this phase.

If you really care about performance when scaling massively:

  • Use NIO for asynchronous socket IO
  • Wrap up the socket access logic inside an Actor (and maybe use Futures in clients to hide the Actors)
  • Or even spare yourself the time and use Akka's IO module
Community
  • 1
  • 1
ron
  • 9,262
  • 4
  • 40
  • 73
  • Actually you're the second to suggest to me to wrap socket access in Actors. I love Akka, and I'm convinced it needs to be done. I can say, however, that the websocket framework itself is built on Netty (Webbit) so I'm not worried about it scaling... – crockpotveggies Sep 13 '12 at 16:29