From a855e58bfcd7e48043c5e6d34536c32cd4a97b3f Mon Sep 17 00:00:00 2001 From: He-Pin Date: Sun, 27 Aug 2023 22:09:15 +0800 Subject: [PATCH] =str Make use of statefulMap to implement zipWithIndex. Signed-off-by: He-Pin --- .../pekko/stream/ZipWithIndexBenchmark.scala | 86 +++++++++++++++++++ .../apache/pekko/stream/scaladsl/Flow.scala | 13 +-- 2 files changed, 89 insertions(+), 10 deletions(-) create mode 100644 bench-jmh/src/main/scala/org/apache/pekko/stream/ZipWithIndexBenchmark.scala diff --git a/bench-jmh/src/main/scala/org/apache/pekko/stream/ZipWithIndexBenchmark.scala b/bench-jmh/src/main/scala/org/apache/pekko/stream/ZipWithIndexBenchmark.scala new file mode 100644 index 0000000000..3393e54d84 --- /dev/null +++ b/bench-jmh/src/main/scala/org/apache/pekko/stream/ZipWithIndexBenchmark.scala @@ -0,0 +1,86 @@ +/* + * 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.collection.immutable +import scala.concurrent._ +import scala.concurrent.duration._ + +import com.typesafe.config.ConfigFactory +import org.openjdk.jmh.annotations._ + +import org.apache.pekko +import pekko.actor.ActorSystem +import pekko.stream.scaladsl._ + +object ZipWithIndexBenchmark { + final val OperationsPerInvocation = 100000 +} + +@State(Scope.Benchmark) +@OutputTimeUnit(TimeUnit.SECONDS) +@BenchmarkMode(Array(Mode.Throughput)) +class ZipWithIndexBenchmark { + import ZipWithIndexBenchmark._ + + 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("ZipWithIndexBenchmark", config) + + @TearDown + def shutdown(): Unit = { + Await.result(system.terminate(), 5.seconds) + } + + private val newZipWithIndex = Source.repeat(1) + .take(OperationsPerInvocation) + .zipWithIndex.toMat(Sink.ignore)(Keep.right) + + private val oldZipWithIndex = Source + .repeat(1) + .take(OperationsPerInvocation) + .statefulMapConcat[(Int, Long)] { () => + var index: Long = 0L + elem => { + val zipped = (elem, index) + index += 1 + immutable.Iterable[(Int, Long)](zipped) + } + } + .toMat(Sink.ignore)(Keep.right) + + @Benchmark + @OperationsPerInvocation(OperationsPerInvocation) + def benchOldZipWithIndex(): Unit = + Await.result(oldZipWithIndex.run(), Duration.Inf) + + @Benchmark + @OperationsPerInvocation(OperationsPerInvocation) + def benchNewZipWithIndex(): Unit = + Await.result(newZipWithIndex.run(), Duration.Inf) + +} diff --git a/stream/src/main/scala/org/apache/pekko/stream/scaladsl/Flow.scala b/stream/src/main/scala/org/apache/pekko/stream/scaladsl/Flow.scala index c3e2fcfe56..e8cc5f6f8c 100755 --- a/stream/src/main/scala/org/apache/pekko/stream/scaladsl/Flow.scala +++ b/stream/src/main/scala/org/apache/pekko/stream/scaladsl/Flow.scala @@ -2881,16 +2881,9 @@ trait FlowOps[+Out, +Mat] { * * '''Cancels when''' downstream cancels */ - def zipWithIndex: Repr[(Out, Long)] = { - statefulMapConcat[(Out, Long)] { () => - var index: Long = 0L - elem => { - val zipped = (elem, index) - index += 1 - immutable.Iterable[(Out, Long)](zipped) - } - } - } + def zipWithIndex: Repr[(Out, Long)] = statefulMap(() => 0L)({ + case (index, out) => (index + 1L, (out, index)) + }, _ => None).withAttributes(DefaultAttributes.zipWithIndex) /** * Interleave is a deterministic merge of the given [[Source]] with elements of this [[Flow]].