Skip to content
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

Refactored Chain and CollectiveMonoid benchmarks #4264

Merged
merged 2 commits into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,69 +22,58 @@
package cats.bench

import cats.data.Chain
import fs2.Catenable
import chain.{Chain => OldChain}
import fs2.Chunk
import org.openjdk.jmh.annotations.{Benchmark, Scope, State}

@State(Scope.Thread)
class ChainBench {

private val intOption = Option(1)
private val smallChain = Chain(1, 2, 3, 4, 5)
private val smallCatenable = Catenable(1, 2, 3, 4, 5)
private val smallChunk = Chunk(1, 2, 3, 4, 5)
private val smallVector = Vector(1, 2, 3, 4, 5)
private val smallList = List(1, 2, 3, 4, 5)
private val smallOldChain = OldChain(smallList)

private val largeChain = (0 to 1000)
.foldLeft(Chain.empty[Int])((acc, _) => acc ++ Chain.fromSeq(0 to 1000))
private val largeCatenable = Catenable.fromSeq(0 to 1000000)
private val largeChunk = Chunk.seq(0 to 1000000)
private val largeVector = (0 to 1000000).toVector
private val largeList = (0 to 1000000).toList
private val largeOldChain = (0 to 1000).foldLeft(OldChain.empty[Int])((acc, _) => acc ++ OldChain(0 to 1000))
@Benchmark def mapSmallChain: Chain[Int] = smallChain.map(_ + 1)
@Benchmark def mapSmallCatenable: Catenable[Int] = smallCatenable.map(_ + 1)
@Benchmark def mapsmallChunk: Chunk[Int] = smallChunk.map(_ + 1)
@Benchmark def mapSmallVector: Vector[Int] = smallVector.map(_ + 1)
@Benchmark def mapSmallList: List[Int] = smallList.map(_ + 1)
@Benchmark def mapSmallOldChain: OldChain[Int] = smallOldChain.map(_ + 1)
@Benchmark def mapLargeChain: Chain[Int] = largeChain.map(_ + 1)
@Benchmark def mapLargeCatenable: Catenable[Int] = largeCatenable.map(_ + 1)
@Benchmark def maplargeChunk: Chunk[Int] = largeChunk.map(_ + 1)
@Benchmark def mapLargeVector: Vector[Int] = largeVector.map(_ + 1)
@Benchmark def mapLargeList: List[Int] = largeList.map(_ + 1)
@Benchmark def mapLargeOldChain: OldChain[Int] = largeOldChain.map(_ + 1)
@Benchmark def foldLeftSmallChain: Int = smallChain.foldLeft(0)(_ + _)
@Benchmark def foldLeftSmallCatenable: Int = smallCatenable.foldLeft(0)(_ + _)
@Benchmark def foldLeftsmallChunk: Int = smallChunk.foldLeft(0)(_ + _)
@Benchmark def foldLeftSmallVector: Int = smallVector.foldLeft(0)(_ + _)
@Benchmark def foldLeftSmallList: Int = smallList.foldLeft(0)(_ + _)
@Benchmark def foldLeftSmallOldChain: Int = smallOldChain.foldLeft(0)(_ + _)
@Benchmark def foldLeftLargeChain: Int = largeChain.foldLeft(0)(_ + _)
@Benchmark def foldLeftLargeCatenable: Int = largeCatenable.foldLeft(0)(_ + _)
@Benchmark def foldLeftlargeChunk: Int = largeChunk.foldLeft(0)(_ + _)
@Benchmark def foldLeftLargeVector: Int = largeVector.foldLeft(0)(_ + _)
@Benchmark def foldLeftLargeList: Int = largeList.foldLeft(0)(_ + _)
@Benchmark def foldLeftLargeOldChain: Int = largeOldChain.foldLeft(0)(_ + _)
@Benchmark def consSmallChain: Chain[Int] = 0 +: smallChain
@Benchmark def consSmallCatenable: Catenable[Int] = 0 +: smallCatenable
@Benchmark def conssmallChunk: Chunk[Int] = Chunk(0) ++ smallChunk
@Benchmark def consSmallVector: Vector[Int] = 0 +: smallVector
@Benchmark def consSmallList: List[Int] = 0 +: smallList
@Benchmark def consSmallOldChain: OldChain[Int] = 0 +: smallOldChain

@Benchmark def consLargeChain: Chain[Int] = 0 +: largeChain
@Benchmark def consLargeCatenable: Catenable[Int] = 0 +: largeCatenable
@Benchmark def conslargeChunk: Chunk[Int] = Chunk(0) ++ largeChunk
@Benchmark def consLargeVector: Vector[Int] = 0 +: largeVector
@Benchmark def consLargeList: List[Int] = 0 +: largeList
@Benchmark def consLargeOldChain: OldChain[Int] = 0 +: largeOldChain

@Benchmark def createTinyChain: Chain[Int] = Chain(1)
@Benchmark def createTinyCatenable: Catenable[Int] = Catenable(1)
@Benchmark def createTinyChunk: Chunk[Int] = Chunk(1)
@Benchmark def createTinyVector: Vector[Int] = Vector(1)
@Benchmark def createTinyList: List[Int] = List(1)
@Benchmark def createTinyOldChain: OldChain[Int] = OldChain.single(1)

@Benchmark def createSmallChain: Chain[Int] = Chain(1, 2, 3, 4, 5)
@Benchmark def createSmallCatenable: Catenable[Int] = Catenable(1, 2, 3, 4, 5)
@Benchmark def createsmallChunk: Chunk[Int] = Chunk(1, 2, 3, 4, 5)
@Benchmark def createSmallVector: Vector[Int] = Vector(1, 2, 3, 4, 5)
@Benchmark def createSmallList: List[Int] = List(1, 2, 3, 4, 5)
@Benchmark def createSmallOldChain: OldChain[Int] = OldChain(Seq(1, 2, 3, 4, 5))

@Benchmark def createChainSeqOption: Chain[Int] = Chain.fromSeq(intOption.toSeq)
@Benchmark def createChainOption: Chain[Int] = Chain.fromOption(intOption)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,16 @@

package cats.bench

import cats.Monoid
import cats.data.Chain
import cats.implicits._
import chain.{Chain => OldChain}
import org.openjdk.jmh.annotations.{Benchmark, Scope, State}

@State(Scope.Thread)
class CollectionMonoidBench {

private val largeList = (0 to 1000000).toList

implicit def monoidOldChain[A]: Monoid[OldChain[A]] = new Monoid[OldChain[A]] {
def empty: OldChain[A] = OldChain.empty[A]

def combine(x: OldChain[A], y: OldChain[A]): OldChain[A] = x ++ y
}

@Benchmark def accumulateChain: Chain[Int] = largeList.foldMap(Chain.one)
@Benchmark def accumulateVector: Vector[Int] = largeList.foldMap(Vector(_))
@Benchmark def accumulateList: List[Int] = largeList.foldMap(List(_))
@Benchmark def accumulateOldChain: OldChain[Int] = largeList.foldMap(OldChain.single)
}
13 changes: 4 additions & 9 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -265,15 +265,10 @@ lazy val bench = project
.settings(moduleName := "cats-bench")
.settings(commonJvmSettings)
.settings(
libraryDependencies ++= {
if (scalaVersion.value.startsWith("2.12"))
Seq(
"org.scalaz" %% "scalaz-core" % "7.3.6",
"org.spire-math" %% "chain" % "0.3.0",
"co.fs2" %% "fs2-core" % "0.10.7"
)
else Nil
},
libraryDependencies ++= Seq(
"org.scalaz" %% "scalaz-core" % "7.3.6",
"co.fs2" %% "fs2-core" % "3.2.8"
),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for all your work on this! Actually we should just drop Chunk from the benchmark because it creates a circular dependency with fs2. See #4194 (comment).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I'll remove it so, thanks for noticing it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed it in c3ec714

Now the ETA for the jmh run of CollectiveMonoid is just 04:18:20 for each scala version 😂

Copy link
Member Author

@TonioGela TonioGela Jul 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That means that I need 13 hours of ice packs 😇

evictionErrorLevel := Level.Warn
)
.enablePlugins(NoPublishPlugin, JmhPlugin)
Expand Down