Skip to content

Commit

Permalink
Benchmarks for collectFirst on Chain
Browse files Browse the repository at this point in the history
  • Loading branch information
LMnet committed May 1, 2019
1 parent e78385d commit 9dd3b61
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 2 deletions.
108 changes: 108 additions & 0 deletions bench/src/main/scala/cats/bench/ChainCollectFirstBench.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package cats.bench

import cats.Foldable
import cats.data.Chain
import org.openjdk.jmh.annotations.{Benchmark, Scope, State}

@State(Scope.Thread)
class ChainCollectFirstBench {

private val foldableChain = Foldable[Chain]

private val small = Chain(1, 2, 3, 4, 5)
private val smallLast = 5

private val medium = (0 to 10)
.foldLeft(Chain.empty[Int])((acc, _) => acc ++ Chain.fromSeq(0 to 10))
private val mediumLast = 10 * 10

private val large = (0 to 1000)
.foldLeft(Chain.empty[Int])((acc, _) => acc ++ Chain.fromSeq(0 to 1000))
private val largeLast = 1000 * 1000

@Benchmark
def collectFirstSmallNew: Option[Int] = small.collectFirst {
case x if x == smallLast => x
}

@Benchmark
def collectFirstSmallFoldable: Option[Int] = foldableChain.collectFirst(small) {
case x if x == smallLast => x
}

@Benchmark
def collectFirstMediumNew: Option[Int] = medium.collectFirst {
case x if x == mediumLast => x
}

@Benchmark
def collectFirstMediumFoldable: Option[Int] = foldableChain.collectFirst(medium) {
case x if x == mediumLast => x
}

@Benchmark
def collectFirstLargeNew: Option[Int] = medium.collectFirst {
case x if x == largeLast => x
}

@Benchmark
def collectFirstLargeFoldable: Option[Int] = foldableChain.collectFirst(medium) {
case x if x == largeLast => x
}



@Benchmark
def collectFirstSomeSmallNew: Option[Int] = small.collectFirstSome {
case x if x == smallLast => Some(x)
case _ => None
}

@Benchmark
def collectFirstSome2SmallNew: Option[Int] = small.collectFirstSome2 {
case x if x == smallLast => Some(x)
case _ => None
}

@Benchmark
def collectFirstSomeSmallFoldable: Option[Int] = foldableChain.collectFirstSome(small) {
case x if x == smallLast => Some(x)
case _ => None
}

@Benchmark
def collectFirstSomeMediumNew: Option[Int] = medium.collectFirstSome {
case x if x == mediumLast => Some(x)
case _ => None
}

@Benchmark
def collectFirstSome2MediumNew: Option[Int] = medium.collectFirstSome2 {
case x if x == mediumLast => Some(x)
case _ => None
}

@Benchmark
def collectFirstSomeMediumFoldable: Option[Int] = foldableChain.collectFirstSome(medium) {
case x if x == mediumLast => Some(x)
case _ => None
}

@Benchmark
def collectFirstSomeLargeNew: Option[Int] = medium.collectFirstSome {
case x if x == largeLast => Some(x)
case _ => None
}

@Benchmark
def collectFirstSome2LargeNew: Option[Int] = medium.collectFirstSome2 {
case x if x == largeLast => Some(x)
case _ => None
}

@Benchmark
def collectFirstSomeLargeFoldable: Option[Int] = foldableChain.collectFirstSome(medium) {
case x if x == largeLast => Some(x)
case _ => None
}
}
15 changes: 13 additions & 2 deletions core/src/main/scala/cats/data/Chain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,19 @@ sealed abstract class Chain[+A] {
final def collectFirstSome[B](f: A => Option[B]): Option[B] =
collectFirst(Function.unlift(f))

final def collectFirstSome2[B](f: A => Option[B]): Option[B] = {
var result: Option[B] = None
foreachUntil { a =>
val x = f(a)
if (x.isDefined) {
result = x
true
} else false
}
result
}


/**
* Remove elements not matching the predicate
*/
Expand Down Expand Up @@ -584,8 +597,6 @@ sealed abstract private[data] class ChainInstances extends ChainInstances1 {
override def forall[A](fa: Chain[A])(p: A => Boolean): Boolean = fa.forall(p)
override def find[A](fa: Chain[A])(f: A => Boolean): Option[A] = fa.find(f)
override def size[A](fa: Chain[A]): Long = fa.length
override def collectFirst[A, B](fa: Chain[A])(pf: PartialFunction[A, B]): Option[B] = fa.collectFirst(pf)
override def collectFirstSome[A, B](fa: Chain[A])(f: A => Option[B]): Option[B] = fa.collectFirstSome(f)

def coflatMap[A, B](fa: Chain[A])(f: Chain[A] => B): Chain[B] = {
@tailrec def go(as: Chain[A], res: ListBuffer[B]): Chain[B] =
Expand Down

0 comments on commit 9dd3b61

Please sign in to comment.