-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Optimize flatMapConcat for single element source, #25241 #25242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9259580
Optimize flatMapConcat for single element source, #25241
patriknw 1de7dad
Grab the SourceSingle via TraversalBuilder
patriknw 42fdbb2
Also handle the case when there is no demand
patriknw 7ae02d1
don't match when mapMaterializedValue and async
patriknw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
130 changes: 130 additions & 0 deletions
130
akka-bench-jmh/src/main/scala/akka/stream/FlatMapConcatBenchmark.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| /** | ||
| * Copyright (C) 2018 Lightbend Inc. <https://www.lightbend.com> | ||
| */ | ||
|
|
||
| package akka.stream | ||
|
|
||
| import java.util.concurrent.CountDownLatch | ||
| import java.util.concurrent.TimeUnit | ||
|
|
||
| import scala.concurrent.Await | ||
| import scala.concurrent.duration._ | ||
|
|
||
| import akka.NotUsed | ||
| import akka.actor.ActorSystem | ||
| import akka.remote.artery.BenchTestSource | ||
| import akka.remote.artery.LatchSink | ||
| import akka.stream.impl.PhasedFusingActorMaterializer | ||
| import akka.stream.impl.StreamSupervisor | ||
| import akka.stream.scaladsl._ | ||
| import akka.testkit.TestProbe | ||
| import com.typesafe.config.ConfigFactory | ||
| import org.openjdk.jmh.annotations._ | ||
| import akka.stream.impl.fusing.GraphStages | ||
|
|
||
| object FlatMapConcatBenchmark { | ||
| final val OperationsPerInvocation = 100000 | ||
| } | ||
|
|
||
| @State(Scope.Benchmark) | ||
| @OutputTimeUnit(TimeUnit.SECONDS) | ||
| @BenchmarkMode(Array(Mode.Throughput)) | ||
| class FlatMapConcatBenchmark { | ||
| import FlatMapConcatBenchmark._ | ||
|
|
||
| private val config = ConfigFactory.parseString( | ||
| """ | ||
| akka.actor.default-dispatcher { | ||
| executor = "fork-join-executor" | ||
| fork-join-executor { | ||
| parallelism-factor = 1 | ||
| } | ||
| } | ||
| """ | ||
| ) | ||
|
|
||
| private implicit val system: ActorSystem = ActorSystem("FlatMapConcatBenchmark", config) | ||
|
|
||
| var materializer: ActorMaterializer = _ | ||
|
|
||
| var testSource: Source[java.lang.Integer, NotUsed] = _ | ||
|
|
||
| @Setup | ||
| def setup(): Unit = { | ||
| val settings = ActorMaterializerSettings(system) | ||
| materializer = ActorMaterializer(settings) | ||
|
|
||
| testSource = Source.fromGraph(new BenchTestSource(OperationsPerInvocation)) | ||
| } | ||
|
|
||
| @TearDown | ||
| def shutdown(): Unit = { | ||
| Await.result(system.terminate(), 5.seconds) | ||
| } | ||
|
|
||
| @Benchmark | ||
| @OperationsPerInvocation(OperationsPerInvocation) | ||
| def sourceDotSingle(): Unit = { | ||
| val latch = new CountDownLatch(1) | ||
|
|
||
| testSource | ||
| .flatMapConcat(Source.single) | ||
| .runWith(new LatchSink(OperationsPerInvocation, latch))(materializer) | ||
|
|
||
| awaitLatch(latch) | ||
| } | ||
|
|
||
| @Benchmark | ||
| @OperationsPerInvocation(OperationsPerInvocation) | ||
| def internalSingleSource(): Unit = { | ||
| val latch = new CountDownLatch(1) | ||
|
|
||
| testSource | ||
| .flatMapConcat(elem ⇒ new GraphStages.SingleSource(elem)) | ||
| .runWith(new LatchSink(OperationsPerInvocation, latch))(materializer) | ||
|
|
||
| awaitLatch(latch) | ||
| } | ||
|
|
||
| @Benchmark | ||
| @OperationsPerInvocation(OperationsPerInvocation) | ||
| def oneElementList(): Unit = { | ||
| val latch = new CountDownLatch(1) | ||
|
|
||
| testSource | ||
| .flatMapConcat(n ⇒ Source(n :: Nil)) | ||
| .runWith(new LatchSink(OperationsPerInvocation, latch))(materializer) | ||
|
|
||
| awaitLatch(latch) | ||
| } | ||
|
|
||
| @Benchmark | ||
| @OperationsPerInvocation(OperationsPerInvocation) | ||
| def mapBaseline(): Unit = { | ||
| val latch = new CountDownLatch(1) | ||
|
|
||
| testSource | ||
| .map(elem ⇒ elem) | ||
| .runWith(new LatchSink(OperationsPerInvocation, latch))(materializer) | ||
|
|
||
| awaitLatch(latch) | ||
| } | ||
|
|
||
| private def awaitLatch(latch: CountDownLatch): Unit = { | ||
| if (!latch.await(30, TimeUnit.SECONDS)) { | ||
| dumpMaterializer() | ||
| throw new RuntimeException("Latch didn't complete in time") | ||
| } | ||
| } | ||
|
|
||
| private def dumpMaterializer(): Unit = { | ||
| materializer match { | ||
| case impl: PhasedFusingActorMaterializer ⇒ | ||
| val probe = TestProbe()(system) | ||
| impl.supervisor.tell(StreamSupervisor.GetChildren, probe.ref) | ||
| val children = probe.expectMsgType[StreamSupervisor.Children].children | ||
| children.foreach(_ ! StreamSupervisor.PrintDebugDump) | ||
| } | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,10 +10,12 @@ import akka.stream.impl.StreamLayout.AtomicModule | |
| import akka.stream.impl.TraversalBuilder.{ AnyFunction1, AnyFunction2 } | ||
| import akka.stream.scaladsl.Keep | ||
| import akka.util.OptionVal | ||
|
|
||
| import scala.language.existentials | ||
| import scala.collection.immutable.Map.Map1 | ||
|
|
||
| import akka.stream.impl.fusing.GraphStageModule | ||
| import akka.stream.impl.fusing.GraphStages.SingleSource | ||
|
|
||
| /** | ||
| * INTERNAL API | ||
| * | ||
|
|
@@ -334,6 +336,37 @@ import scala.collection.immutable.Map.Map1 | |
| } | ||
| slot | ||
| } | ||
|
|
||
| /** | ||
| * Try to find `SingleSource` or wrapped such. This is used as a | ||
| * performance optimization in FlattenMerge and possibly other places. | ||
| */ | ||
| def getSingleSource[A >: Null](graph: Graph[SourceShape[A], _]): OptionVal[SingleSource[A]] = { | ||
| graph match { | ||
| case single: SingleSource[A] @unchecked ⇒ OptionVal.Some(single) | ||
| case _ ⇒ | ||
| graph.traversalBuilder match { | ||
| case l: LinearTraversalBuilder ⇒ | ||
| l.pendingBuilder match { | ||
| case OptionVal.Some(a: AtomicTraversalBuilder) ⇒ | ||
| a.module match { | ||
| case m: GraphStageModule[_, _] ⇒ | ||
| m.stage match { | ||
| case single: SingleSource[A] @unchecked ⇒ | ||
| // It would be != EmptyTraversal if mapMaterializedValue was used and then we can't optimize. | ||
| if ((l.traversalSoFar eq EmptyTraversal) && !l.attributes.isAsync) | ||
| OptionVal.Some(single) | ||
| else OptionVal.None | ||
| case _ ⇒ OptionVal.None | ||
| } | ||
| case _ ⇒ OptionVal.None | ||
| } | ||
| case _ ⇒ OptionVal.None | ||
| } | ||
| case _ ⇒ OptionVal.None | ||
| } | ||
| } | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pyramid of optimization. |
||
| } | ||
|
|
||
| /** | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check also that
Source.single.{async/mapMatVal}causes a nested graph and does not match for extra measure? (Pretty sure they do, but just for extra measure.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@johanandren Good point, it was wrong. Matched for those cases also.
asyncis only adding attribute, andmapMaterializedValueis addingTransformtraversal. Fixed with additional checks for those in 7ae02d1There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, good!