From f542ce885c27deba72959a1ae114fdd3a09720d0 Mon Sep 17 00:00:00 2001 From: Patrik Nordwall Date: Thu, 9 Mar 2017 17:01:36 +0100 Subject: [PATCH] optimize actor names --- .../main/scala/akka/stream/Attributes.scala | 22 +++++++++------- .../impl/PhasedFusingActorMaterializer.scala | 25 ++++++++----------- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/akka-stream/src/main/scala/akka/stream/Attributes.scala b/akka-stream/src/main/scala/akka/stream/Attributes.scala index 41a0de334a..325fb1f449 100644 --- a/akka-stream/src/main/scala/akka/stream/Attributes.scala +++ b/akka-stream/src/main/scala/akka/stream/Attributes.scala @@ -170,12 +170,7 @@ final case class Attributes(attributeList: List[Attributes.Attribute] = Nil) { /** * Extracts Name attributes and concatenates them. */ - def nameLifted: Option[String] = Option(nameOrDefault(null)) - - /** - * INTERNAL API - */ - def nameOrDefault(default: String = "unknown-operation"): String = { + def nameLifted: Option[String] = { @tailrec def concatNames(i: Iterator[Attribute], first: String, buf: StringBuilder): String = if (i.hasNext) i.next() match { @@ -192,10 +187,19 @@ final case class Attributes(attributeList: List[Attributes.Attribute] = Nil) { else if (buf eq null) first else buf.toString - concatNames(attributeList.reverseIterator, null, null) match { - case null ⇒ default - case some ⇒ some + Option(concatNames(attributeList.reverseIterator, null, null)) + } + + /** + * INTERNAL API + */ + def nameOrDefault(default: String = "unnamed"): String = { + @tailrec def find(attrs: List[Attribute]): String = attrs match { + case Attributes.Name(name) :: _ ⇒ name + case _ :: tail ⇒ find(tail) + case Nil ⇒ default } + find(attributeList) } } diff --git a/akka-stream/src/main/scala/akka/stream/impl/PhasedFusingActorMaterializer.scala b/akka-stream/src/main/scala/akka/stream/impl/PhasedFusingActorMaterializer.scala index db9751089f..bd08427ae3 100644 --- a/akka-stream/src/main/scala/akka/stream/impl/PhasedFusingActorMaterializer.scala +++ b/akka-stream/src/main/scala/akka/stream/impl/PhasedFusingActorMaterializer.scala @@ -28,6 +28,7 @@ import scala.concurrent.ExecutionContextExecutor import scala.annotation.tailrec import akka.stream.impl.fusing.GraphInterpreter.DownstreamBoundaryStageLogic import akka.stream.impl.fusing.GraphInterpreter.UpstreamBoundaryStageLogic +import akka.util.OptionVal object PhasedFusingActorMaterializer { @@ -556,6 +557,7 @@ final class GraphStageIsland( private val connections = Array.ofDim[Connection](64) private var maxConnections = 0 private var outConnections: List[Connection] = Nil + private var fullIslandName: OptionVal[String] = OptionVal.None val shell = new GraphInterpreterShell( connections = null, @@ -575,6 +577,10 @@ final class GraphStageIsland( logic.attributes = attributes logics.add(logic) logic.stageId = logics.size() - 1 + fullIslandName match { + case OptionVal.Some(_) ⇒ // already set + case OptionVal.None ⇒ fullIslandName = OptionVal.Some(islandName + "-" + logic.attributes.nameOrDefault()) + } matAndLogic } @@ -673,23 +679,14 @@ final class GraphStageIsland( case _ ⇒ val props = ActorGraphInterpreter.props(shell) .withDispatcher(effectiveSettings.dispatcher) - materializer.actorOf(props, fullIslandName) + val actorName = fullIslandName match { + case OptionVal.Some(n) ⇒ n + case OptionVal.None ⇒ islandName + } + materializer.actorOf(props, actorName) } } - private def fullIslandName: String = { - @tailrec def findUsefulName(i: Int): String = { - if (i == logics.size) islandName - else logics.get(i) match { - case _: DownstreamBoundaryStageLogic[_] | _: UpstreamBoundaryStageLogic[_] ⇒ - findUsefulName(i + 1) - case _ ⇒ - islandName + "-" + logics.get(i).attributes.nameOrDefault() - } - } - findUsefulName(0) - } - override def toString: String = "GraphStagePhase" }