chore: Make use of pattern matching on singleton type instead. (#1026)

More info: @som-snytt point out in https://scala-lang.org/files/archive/spec/2.13/08-pattern-matching.html#type-patterns see singleton.
This commit is contained in:
He-Pin(kerr) 2024-01-27 23:33:06 +08:00 committed by GitHub
parent a2b5b5df22
commit db4f57396d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 60 additions and 29 deletions

View file

@ -15,14 +15,15 @@ package org.apache.pekko.stream
import com.typesafe.config.ConfigFactory
import org.apache.pekko
import org.apache.pekko.stream.ActorAttributes.SupervisionStrategy
import org.apache.pekko.stream.Attributes.SourceLocation
import org.apache.pekko.stream.impl.Stages.DefaultAttributes
import org.apache.pekko.stream.impl.fusing.Collect
import org.apache.pekko.stream.stage.{ GraphStage, GraphStageLogic, InHandler, OutHandler }
import org.openjdk.jmh.annotations._
import pekko.actor.ActorSystem
import pekko.stream.ActorAttributes.SupervisionStrategy
import pekko.stream.Attributes.SourceLocation
import pekko.stream.impl.Stages.DefaultAttributes
import pekko.stream.impl.fusing.Collect
import pekko.stream.impl.fusing.Collect.NotApplied
import pekko.stream.scaladsl._
import pekko.stream.stage.{ GraphStage, GraphStageLogic, InHandler, OutHandler }
import java.util.concurrent.TimeUnit
import scala.annotation.nowarn
@ -82,6 +83,7 @@ class CollectBenchmark {
private lazy val decider = inheritedAttributes.mandatoryAttribute[SupervisionStrategy].decider
import Collect.NotApplied
@nowarn("msg=Any")
override def onPush(): Unit =
try {
pf.applyOrElse(grab(in), NotApplied) match {
@ -116,4 +118,28 @@ class CollectBenchmark {
def benchNewCollect(): Unit =
Await.result(newCollect.run(), Duration.Inf)
@nowarn("msg=Any")
def collectOnSingleton[A](a: A, pf: PartialFunction[A, A]): A = pf.applyOrElse(a, NotApplied) match {
case NotApplied => a
case _ => a
}
@nowarn("msg=Any")
def collectOnSingletonType[A](a: A, pf: PartialFunction[A, A]): A = pf.applyOrElse(a, NotApplied) match {
case _: NotApplied.type => a
case _ => a
}
private val string2String: PartialFunction[String, String] = { case a => a }
@Benchmark
@OperationsPerInvocation(OperationsPerInvocation)
def benchCollectOnSingleton(): Unit =
collectOnSingleton("", string2String)
@Benchmark
@OperationsPerInvocation(OperationsPerInvocation)
def benchCollectOnSingletonType(): Unit =
collectOnSingletonType("", string2String)
}