From ef399e283dc90fee5dc4b9b84fa6c7746c30d827 Mon Sep 17 00:00:00 2001 From: Viktor Klang Date: Thu, 15 Mar 2012 15:17:08 +0100 Subject: [PATCH] Switching to TreeSet, which should also receive quite some performance enhancements in Scala 2.10 --- .../src/main/scala/akka/event/EventBus.scala | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/akka-actor/src/main/scala/akka/event/EventBus.scala b/akka-actor/src/main/scala/akka/event/EventBus.scala index f79ee94a8c..bf19f5ed06 100644 --- a/akka-actor/src/main/scala/akka/event/EventBus.scala +++ b/akka-actor/src/main/scala/akka/event/EventBus.scala @@ -9,6 +9,7 @@ import akka.util.Index import java.util.concurrent.ConcurrentSkipListSet import java.util.Comparator import akka.util.{ Subclassification, SubclassifiedIndex } +import collection.immutable.TreeSet /** * Represents the base type for EventBuses @@ -236,8 +237,8 @@ trait ScanningClassification { self: EventBus ⇒ trait ActorClassification { this: ActorEventBus with ActorClassifier ⇒ import java.util.concurrent.ConcurrentHashMap import scala.annotation.tailrec - - protected val mappings = new ConcurrentHashMap[ActorRef, Vector[ActorRef]](mapSize) + private val empty = TreeSet.empty[ActorRef] + protected val mappings = new ConcurrentHashMap[ActorRef, TreeSet[ActorRef]](mapSize) @tailrec protected final def associate(monitored: ActorRef, monitor: ActorRef): Boolean = { @@ -246,15 +247,15 @@ trait ActorClassification { this: ActorEventBus with ActorClassifier ⇒ case null ⇒ if (monitored.isTerminated) false else { - if (mappings.putIfAbsent(monitored, Vector(monitor)) ne null) associate(monitored, monitor) + if (mappings.putIfAbsent(monitored, TreeSet(monitor)) ne null) associate(monitored, monitor) else if (monitored.isTerminated) !dissociate(monitored, monitor) else true } - case raw: Vector[_] ⇒ - val v = raw.asInstanceOf[Vector[ActorRef]] + case raw: TreeSet[_] ⇒ + val v = raw.asInstanceOf[TreeSet[ActorRef]] if (monitored.isTerminated) false if (v.contains(monitor)) true else { - val added = v :+ monitor + val added = v + monitor if (!mappings.replace(monitored, v, added)) associate(monitored, monitor) else if (monitored.isTerminated) !dissociate(monitored, monitor) else true } @@ -266,9 +267,9 @@ trait ActorClassification { this: ActorEventBus with ActorClassifier ⇒ def dissociateAsMonitored(monitored: ActorRef): Iterable[ActorRef] = { val current = mappings get monitored current match { - case null ⇒ Vector.empty[ActorRef] - case raw: Vector[_] ⇒ - val v = raw.asInstanceOf[Vector[ActorRef]] + case null ⇒ empty + case raw: TreeSet[_] ⇒ + val v = raw.asInstanceOf[TreeSet[ActorRef]] if (!mappings.remove(monitored, v)) dissociateAsMonitored(monitored) else v } @@ -280,8 +281,8 @@ trait ActorClassification { this: ActorEventBus with ActorClassifier ⇒ val entry = i.next() val v = entry.getValue v match { - case raw: Vector[_] ⇒ - val monitors = raw.asInstanceOf[Vector[ActorRef]] + case raw: TreeSet[_] ⇒ + val monitors = raw.asInstanceOf[TreeSet[ActorRef]] if (monitors.contains(monitor)) dissociate(entry.getKey, monitor) case _ ⇒ //Dun care @@ -297,8 +298,8 @@ trait ActorClassification { this: ActorEventBus with ActorClassifier ⇒ val current = mappings get monitored current match { case null ⇒ false - case raw: Vector[_] ⇒ - val v = raw.asInstanceOf[Vector[ActorRef]] + case raw: TreeSet[_] ⇒ + val v = raw.asInstanceOf[TreeSet[ActorRef]] val removed = v.filterNot(monitor ==) if (removed eq raw) false else if (removed.isEmpty) {