-
Notifications
You must be signed in to change notification settings - Fork 199
perf: optimize stream materializer wiring with HashMap and ArrayList replacements #3062
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
Open
He-Pin
wants to merge
8
commits into
main
Choose a base branch
from
optimize/stream-materializer-wiring
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+314
−27
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e9bac4d
perf: optimize stream materializer wiring with HashMap and ArrayList …
He-Pin 60dcc7e
fix: replace HashMap with sorted primitive arrays, fix benchmark headers
He-Pin cfd66b8
fix: clear dangling reference after forward wire removal
He-Pin 1229de6
fix: improve GC friendliness of forwardWires and outConnections
He-Pin 6e609de
docs: clarify why sorted arrays over IntMap for forwardWires
He-Pin f96c286
Potential fix for pull request finding
He-Pin 5b822b6
Potential fix for pull request finding
He-Pin f0f6185
Potential fix for pull request finding
He-Pin 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
123 changes: 123 additions & 0 deletions
123
bench-jmh/src/main/scala/org/apache/pekko/stream/AsyncBoundaryThroughputBenchmark.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,123 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.pekko.stream | ||
|
|
||
| import java.util.concurrent.CountDownLatch | ||
| import java.util.concurrent.TimeUnit | ||
|
|
||
| import scala.annotation.nowarn | ||
| import scala.concurrent.Await | ||
| import scala.concurrent.duration._ | ||
|
|
||
| import org.openjdk.jmh.annotations._ | ||
| import org.openjdk.jmh.infra.Blackhole | ||
|
|
||
| import org.apache.pekko | ||
| import pekko.NotUsed | ||
| import pekko.actor.ActorSystem | ||
| import pekko.stream.scaladsl._ | ||
| import pekko.stream.stage._ | ||
|
|
||
| import com.typesafe.config.ConfigFactory | ||
|
|
||
| object AsyncBoundaryThroughputBenchmark { | ||
| final val ElementCount = 100 * 1000 | ||
| } | ||
|
|
||
| @State(Scope.Benchmark) | ||
| @OutputTimeUnit(TimeUnit.SECONDS) | ||
| @BenchmarkMode(Array(Mode.Throughput)) | ||
| class AsyncBoundaryThroughputBenchmark { | ||
|
|
||
| import AsyncBoundaryThroughputBenchmark._ | ||
|
|
||
| val config = ConfigFactory.parseString(s""" | ||
| pekko.stream.materializer.sync-processing-limit = ${Int.MaxValue} | ||
| """) | ||
|
|
||
| implicit val system: ActorSystem = ActorSystem("AsyncBoundaryThroughputBenchmark", config) | ||
|
|
||
| @Param(Array("1", "3", "10")) | ||
| var asyncBoundaries = 0 | ||
|
|
||
| var source: Source[Int, NotUsed] = _ | ||
| var flow: Flow[Int, Int, NotUsed] = _ | ||
|
|
||
| @Setup | ||
| def setup(): Unit = { | ||
| SystemMaterializer(system).materializer | ||
| source = Source(1 to ElementCount) | ||
| var f: Flow[Int, Int, NotUsed] = Flow[Int] | ||
| for (_ <- 1 to asyncBoundaries) { | ||
| f = f.map(identity).async | ||
| } | ||
| flow = f | ||
| } | ||
|
|
||
| @Benchmark | ||
| @OperationsPerInvocation(ElementCount) | ||
| def async_boundary_throughput(blackhole: Blackhole): CountDownLatch = { | ||
| FusedGraphsBenchmark.blackhole = blackhole | ||
| val latch = source | ||
| .via(flow) | ||
| .toMat(Sink.fromGraph(new JitSafeCompletionLatchInt))(Keep.right) | ||
| .run() | ||
| if (!latch.await(30, TimeUnit.SECONDS)) | ||
| throw new RuntimeException("Latch timed out") | ||
| latch | ||
| } | ||
|
|
||
| @TearDown | ||
| def shutdown(): Unit = { | ||
| Await.result(system.terminate(), 5.seconds) | ||
| } | ||
| } | ||
|
|
||
| class JitSafeCompletionLatchInt extends GraphStageWithMaterializedValue[SinkShape[Int], CountDownLatch] { | ||
| val in = Inlet[Int]("JitSafeCompletionLatchInt.in") | ||
| override val shape = SinkShape(in) | ||
|
|
||
| @nowarn("cat=unused-params") | ||
| override def createLogicAndMaterializedValue(inheritedAttributes: Attributes): (GraphStageLogic, CountDownLatch) = { | ||
| val latch = new CountDownLatch(1) | ||
| val logic = new GraphStageLogic(shape) with InHandler { | ||
| private var count = 0 | ||
|
|
||
| override def preStart(): Unit = pull(in) | ||
| override def onPush(): Unit = { | ||
| grab(in) // consume element | ||
| count += 1 | ||
| pull(in) | ||
| } | ||
|
|
||
| override def onUpstreamFinish(): Unit = { | ||
| FusedGraphsBenchmark.blackhole.consume(count) | ||
| latch.countDown() | ||
| completeStage() | ||
| } | ||
|
Copilot marked this conversation as resolved.
|
||
|
|
||
| override def onUpstreamFailure(ex: Throwable): Unit = { | ||
| latch.countDown() | ||
| failStage(ex) | ||
| } | ||
|
Copilot marked this conversation as resolved.
|
||
|
|
||
| setHandler(in, this) | ||
| } | ||
| (logic, latch) | ||
| } | ||
| } | ||
132 changes: 132 additions & 0 deletions
132
bench-jmh/src/main/scala/org/apache/pekko/stream/MaterializerWiringBenchmark.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,132 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.pekko.stream | ||
|
|
||
| import java.util.concurrent.TimeUnit | ||
|
|
||
| import scala.concurrent.Await | ||
| import scala.concurrent.duration._ | ||
|
|
||
| import org.openjdk.jmh.annotations._ | ||
|
|
||
| import org.apache.pekko | ||
| import pekko.NotUsed | ||
| import pekko.actor.ActorSystem | ||
| import pekko.stream.scaladsl._ | ||
|
|
||
| object MaterializerWiringBenchmark { | ||
|
|
||
| val linearFlowBuilder: Int => RunnableGraph[NotUsed] = numOfOperators => { | ||
| var source = Source.single(()) | ||
| for (_ <- 1 to numOfOperators) { | ||
| source = source.map(identity) | ||
| } | ||
| source.to(Sink.ignore) | ||
| } | ||
|
|
||
| val broadcastMergeBuilder: Int => RunnableGraph[NotUsed] = numOfJunctions => | ||
| RunnableGraph.fromGraph(GraphDSL.create() { implicit b => | ||
| import GraphDSL.Implicits._ | ||
|
|
||
| val broadcast = b.add(Broadcast[Unit](numOfJunctions)) | ||
| var outlet = broadcast.out(0) | ||
| for (i <- 1 until numOfJunctions) { | ||
| val merge = b.add(Merge[Unit](2)) | ||
| outlet ~> merge | ||
| broadcast.out(i) ~> merge | ||
| outlet = merge.out | ||
| } | ||
|
|
||
| Source.single(()) ~> broadcast | ||
| outlet ~> Sink.ignore | ||
| ClosedShape | ||
| }) | ||
|
|
||
| val broadcastMergeImmediateBuilder: Int => RunnableGraph[NotUsed] = numOfJunctions => | ||
| RunnableGraph.fromGraph(GraphDSL.create() { implicit b => | ||
| import GraphDSL.Implicits._ | ||
|
|
||
| val broadcast = b.add(Broadcast[Unit](numOfJunctions)) | ||
| val merge = b.add(Merge[Unit](numOfJunctions)) | ||
| for (_ <- 0 until numOfJunctions) { | ||
| broadcast ~> merge | ||
| } | ||
|
|
||
| Source.single(()) ~> broadcast | ||
| merge ~> Sink.ignore | ||
| ClosedShape | ||
| }) | ||
|
|
||
| val importedFlowBuilder: Int => RunnableGraph[NotUsed] = numOfFlows => | ||
| RunnableGraph.fromGraph(GraphDSL.createGraph(Source.single(())) { implicit b => source => | ||
| import GraphDSL.Implicits._ | ||
| val flow = Flow[Unit].map(identity) | ||
| var out: Outlet[Unit] = source.out | ||
| for (_ <- 0 until numOfFlows) { | ||
| val flowShape = b.add(flow) | ||
| out ~> flowShape | ||
| out = flowShape.outlet | ||
| } | ||
| out ~> Sink.ignore | ||
| ClosedShape | ||
| }) | ||
| } | ||
|
|
||
| @State(Scope.Benchmark) | ||
| @OutputTimeUnit(TimeUnit.SECONDS) | ||
| @BenchmarkMode(Array(Mode.Throughput)) | ||
| class MaterializerWiringBenchmark { | ||
|
|
||
| import MaterializerWiringBenchmark._ | ||
|
|
||
| implicit val system: ActorSystem = ActorSystem("MaterializerWiringBenchmark") | ||
|
|
||
| var linearFlow: RunnableGraph[NotUsed] = _ | ||
| var broadcastMerge: RunnableGraph[NotUsed] = _ | ||
| var broadcastMergeImmediate: RunnableGraph[NotUsed] = _ | ||
| var importedFlow: RunnableGraph[NotUsed] = _ | ||
|
|
||
| @Param(Array("100", "500", "1000")) | ||
| var complexity = 0 | ||
|
|
||
| @Setup | ||
| def setup(): Unit = { | ||
| SystemMaterializer(system).materializer | ||
| linearFlow = linearFlowBuilder(complexity) | ||
| broadcastMerge = broadcastMergeBuilder(complexity) | ||
| broadcastMergeImmediate = broadcastMergeImmediateBuilder(complexity) | ||
| importedFlow = importedFlowBuilder(complexity) | ||
| } | ||
|
|
||
| @Benchmark | ||
| def linear(): NotUsed = linearFlow.run() | ||
|
|
||
| @Benchmark | ||
| def broadcast_merge_gradual(): NotUsed = broadcastMerge.run() | ||
|
|
||
| @Benchmark | ||
| def broadcast_merge_immediate(): NotUsed = broadcastMergeImmediate.run() | ||
|
|
||
| @Benchmark | ||
| def imported_flow(): NotUsed = importedFlow.run() | ||
|
|
||
| @TearDown | ||
| def shutdown(): Unit = { | ||
| Await.result(system.terminate(), 5.seconds) | ||
| } | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.