Skip to content

Update Scala 2.13-M5 - include cats.data.Chain #5

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
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 3 additions & 4 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@ name := """scala-benchmarks"""

version := "1.0.0"

scalaVersion := "2.12.6"
scalaVersion := "2.13.0-M5"

scalacOptions := Seq(
"-opt:l:inline",
"-opt-inline-from:**",
"-deprecation",
"-Ypartial-unification",
"-Ywarn-value-discard",
"-Ywarn-unused-import",
"-Ywarn-dead-code",
"-Ywarn-numeric-widen"
)

libraryDependencies ++= Seq(
"org.scalaz" %% "scalaz-core" % "7.2.24"
"org.typelevel" %% "cats-core" % "1.6.0",
"org.scalaz" %% "scalaz-core" % "7.2.27"
)

/* To run benchmarks:
Expand Down
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.1.4
sbt.version=1.2.8
2 changes: 1 addition & 1 deletion project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.3.3")
addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.3.4")
18 changes: 17 additions & 1 deletion src/main/scala/benchmarks/AccumClassBench.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import scala.annotation.tailrec
import scala.collection.immutable.VectorBuilder
import scala.collection.mutable.{ArrayBuilder, ListBuffer}

import cats.data.Chain
import org.openjdk.jmh.annotations._
import scalaz.{IList, ICons, INil}

Expand Down Expand Up @@ -98,6 +99,15 @@ class AccumClassBench {
b.result
}

def chain(n: Int): Chain[Pair] = {

@tailrec def work(acc: Chain[Pair], m: Int): Chain[Pair] = m match {
case _ if m == n => acc
case _ => work(Pair(m, m) +: acc, m + 1)
}
work(Chain.empty[Pair], 0)
}

def foryield(n: Int): IndexedSeq[Pair] = for (i <- 0 to n) yield Pair(i, i)

@Benchmark
Expand Down Expand Up @@ -149,11 +159,17 @@ class AccumClassBench {
@Benchmark
def buffer100000: List[Pair] = buffer(100000)

@Benchmark
def chain1000: Chain[Pair] = chain(1000)
@Benchmark
def chain10000: Chain[Pair] = chain(10000)
@Benchmark
def chain100000: Chain[Pair] = chain(100000)

@Benchmark
def foryield1000: IndexedSeq[Pair] = foryield(1000)
@Benchmark
def foryield10000: IndexedSeq[Pair] = foryield(10000)
@Benchmark
def foryield100000: IndexedSeq[Pair] = foryield(100000)

}
71 changes: 50 additions & 21 deletions src/main/scala/benchmarks/ConcatBench.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package benchmarks

import java.util.concurrent.TimeUnit

import cats.data.Chain
import org.openjdk.jmh.annotations._
import scalaz.{IList, EphemeralStream => EStream}

Expand Down Expand Up @@ -57,14 +58,14 @@ class ConcatBench {
var arrayC2b: Array[Pair] = _
var arrayC3b: Array[Pair] = _

var stream0: Stream[Int] = _
var stream1: Stream[Int] = _
var stream2: Stream[Int] = _
var stream3: Stream[Int] = _
var stream0b: Stream[Int] = _
var stream1b: Stream[Int] = _
var stream2b: Stream[Int] = _
var stream3b: Stream[Int] = _
var lazyList0: LazyList[Int] = _
var lazyList1: LazyList[Int] = _
var lazyList2: LazyList[Int] = _
var lazyList3: LazyList[Int] = _
var lazyList0b: LazyList[Int] = _
var lazyList1b: LazyList[Int] = _
var lazyList2b: LazyList[Int] = _
var lazyList3b: LazyList[Int] = _

var estream0: EStream[Int] = _
var estream1: EStream[Int] = _
Expand All @@ -75,6 +76,15 @@ class ConcatBench {
var estream2b: EStream[Int] = _
var estream3b: EStream[Int] = _

var chain0: Chain[Int] = _
var chain1: Chain[Int] = _
var chain2: Chain[Int] = _
var chain3: Chain[Int] = _
var chain0b: Chain[Int] = _
var chain1b: Chain[Int] = _
var chain2b: Chain[Int] = _
var chain3b: Chain[Int] = _

@Setup
def setup: Unit = {
list0 = List.range(1, 1000)
Expand Down Expand Up @@ -122,14 +132,14 @@ class ConcatBench {
arrayC2b = Array.range(1, 100000).map(n => Pair(n, n))
arrayC3b = Array.range(1, 1000000).map(n => Pair(n, n))

stream0 = Stream.range(1, 1000)
stream1 = Stream.range(1, 10000)
stream2 = Stream.range(1, 100000)
stream3 = Stream.range(1, 1000000)
stream0b = Stream.range(1, 1000)
stream1b = Stream.range(1, 10000)
stream2b = Stream.range(1, 100000)
stream3b = Stream.range(1, 1000000)
lazyList0 = LazyList.range(1, 1000)
lazyList1 = LazyList.range(1, 10000)
lazyList2 = LazyList.range(1, 100000)
lazyList3 = LazyList.range(1, 1000000)
lazyList0b = LazyList.range(1, 1000)
lazyList1b = LazyList.range(1, 10000)
lazyList2b = LazyList.range(1, 100000)
lazyList3b = LazyList.range(1, 1000000)

estream0 = EStream.range(1, 1000)
estream1 = EStream.range(1, 10000)
Expand All @@ -139,15 +149,25 @@ class ConcatBench {
estream1b = EStream.range(1, 10000)
estream2b = EStream.range(1, 100000)
estream3b = EStream.range(1, 1000000)

chain0 = Chain.fromSeq(list0)
chain1 = Chain.fromSeq(list1)
chain2 = Chain.fromSeq(list2)
chain3 = Chain.fromSeq(list3)
chain0b = Chain.fromSeq(list0)
chain1b = Chain.fromSeq(list1)
chain2b = Chain.fromSeq(list2)
chain3b = Chain.fromSeq(list3)
}

def list(a: List[Int], b: List[Int]): List[Int] = a ++ b
def ilist(a: IList[Int], b: IList[Int]): IList[Int] = a ++ b
def vector(a: Vector[Int], b: Vector[Int]): Vector[Int] = a ++ b
def array(a: Array[Int], b: Array[Int]): Array[Int] = a ++ b
def arrayC(a: Array[Pair], b: Array[Pair]): Array[Pair] = a ++ b
def stream(a: Stream[Int], b: Stream[Int]): Stream[Int] = a ++ b
def lazyList(a: LazyList[Int], b: LazyList[Int]): LazyList[Int] = a ++ b
def estream(a: EStream[Int], b: EStream[Int]): EStream[Int] = a ++ b
def chain(a: Chain[Int], b: Chain[Int]): Chain[Int] = a ++ b

@Benchmark
def list1k: List[Int] = list(list0, list0b)
Expand Down Expand Up @@ -195,13 +215,13 @@ class ConcatBench {
def arrayC1000k: Array[Pair] = arrayC(arrayC3 , arrayC3b)

@Benchmark
def stream1k: Stream[Int] = stream(stream0 , stream0b)
def lazyList1k: LazyList[Int] = lazyList(lazyList0 , lazyList0b)
@Benchmark
def stream10k: Stream[Int] = stream(stream1 , stream1b)
def lazyList10k: LazyList[Int] = lazyList(lazyList1 , lazyList1b)
@Benchmark
def stream100k: Stream[Int] = stream(stream2 , stream2b)
def lazyList100k: LazyList[Int] = lazyList(lazyList2 , lazyList2b)
@Benchmark
def stream1000k: Stream[Int] = stream(stream3 , stream3b)
def lazyList1000k: LazyList[Int] = lazyList(lazyList3 , lazyList3b)

@Benchmark
def estream1k: EStream[Int] = estream(estream0 , estream0b)
Expand All @@ -211,4 +231,13 @@ class ConcatBench {
def estream100k: EStream[Int] = estream(estream2 , estream2b)
@Benchmark
def estream1000k: EStream[Int] = estream(estream3 , estream3b)

@Benchmark
def chain1k: Chain[Int] = chain(chain0 , chain0b)
@Benchmark
def chain10k: Chain[Int] = chain(chain1 , chain1b)
@Benchmark
def chain100k: Chain[Int] = chain(chain2 , chain2b)
@Benchmark
def chain1000k: Chain[Int] = chain(chain3 , chain3b)
}
51 changes: 41 additions & 10 deletions src/main/scala/benchmarks/FoldBench.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import java.util.concurrent.TimeUnit

import scala.annotation.tailrec

import cats.data.Chain
import org.openjdk.jmh.annotations._
import scalaz.{IList, ICons, INil, EphemeralStream => EStream}

Expand All @@ -18,17 +19,19 @@ class FoldBench {
var ilist: IList[Int] = _
var vector: Vector[Int] = _
var array: Array[Int] = _
var stream: Stream[Int] = _
var lazyList: LazyList[Int] = _
var estream: EStream[Int] = _
var chain: Chain[Int] = _

@Setup
def setup: Unit = {
list = List.range(1, 10000)
ilist = IList.fromList(list)
vector = Vector.range(1, 10000)
array = Array.range(1, 10000)
stream = Stream.range(1, 10000)
lazyList = LazyList.range(1, 10000)
estream = EStream.range(1, 10000)
chain = Chain.fromSeq(list)
}

@Benchmark
Expand Down Expand Up @@ -128,22 +131,22 @@ class FoldBench {
def arraySum: Int = array.sum

@Benchmark
def streamFoldLeft: Int = stream.foldLeft(0)(_ + _)
def lazyListFoldLeft: Int = lazyList.foldLeft(0)(_ + _)
@Benchmark
def streamFoldRight: Int = stream.foldRight(0)(_ + _)
def lazyListFoldRight: Int = lazyList.foldRight(0)(_ + _)
@Benchmark
def streamTailrec: Int = {
@tailrec def work(l: Stream[Int], acc: Int): Int = l match {
def lazyListTailrec: Int = {
@tailrec def work(l: LazyList[Int], acc: Int): Int = l match {
case _ if l.isEmpty => acc
case h #:: rest => work(rest, h + acc)
}

work(stream, 0)
work(lazyList, 0)
}
@Benchmark
def streamWhile: Int = {
def lazyListWhile: Int = {
var i: Int = 0
var l: Stream[Int] = stream
var l: LazyList[Int] = lazyList

while (!l.isEmpty) {
i += l.head
Expand All @@ -153,10 +156,38 @@ class FoldBench {
i
}
@Benchmark
def streamSum: Int = stream.sum
def lazyListSum: Int = lazyList.sum

@Benchmark
def estreamFoldLeft: Int = estream.foldLeft(0) { acc => { n => acc + n } }
@Benchmark
def estreamFoldRight: Int = estream.foldRight(0) { n => { acc => n + acc } }

@Benchmark
def chainFoldLeft: Int = chain.foldLeft(0)(_ + _)
@Benchmark
def chainFoldRight: Int = chain.foldRight(0)(_ + _)
@Benchmark
def chainTailrec: Int = {
@tailrec def work(l: Chain[Int], acc: Int): Int = l.uncons match {
case None => acc
case Some((h, rest)) => work(rest, h + acc)
}

work(chain, 0)
}
@Benchmark
def chainWhile: Int = {
var i: Int = 0
var l: Chain[Int] = chain
var uc = l.uncons

while (!uc.isEmpty) {
i += uc.get._1
l = uc.get._2
uc = l.uncons
}

i
}
}
48 changes: 39 additions & 9 deletions src/main/scala/benchmarks/FoldClassBench.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import java.util.concurrent.TimeUnit

import scala.annotation.tailrec

import cats.data.Chain
import org.openjdk.jmh.annotations._
import scalaz.{IList, ICons, INil}

Expand All @@ -18,15 +19,17 @@ class FoldClassBench {
var ilist: IList[Pair] = _
var vector: Vector[Pair] = _
var array: Array[Pair] = _
var stream: Stream[Pair] = _
var lazyList: LazyList[Pair] = _
var chain: Chain[Pair] = _

@Setup
def setup: Unit = {
list = List.range(1, 10000).map(n => Pair(n, n))
ilist = IList.fromList(list)
vector = Vector.range(1, 10000).map(n => Pair(n, n))
array = Array.range(1, 10000).map(n => Pair(n, n))
stream = Stream.range(1, 10000).map(n => Pair(n, n))
lazyList = LazyList.range(1, 10000).map(n => Pair(n, n))
chain = Chain.fromSeq(list)
}

@Benchmark
Expand Down Expand Up @@ -120,22 +123,22 @@ class FoldClassBench {
}

@Benchmark
def streamFoldLeft: Pair = stream.foldLeft(Pair(0,0))(_ + _)
def lazyListFoldLeft: Pair = lazyList.foldLeft(Pair(0,0))(_ + _)
@Benchmark
def streamFoldRight: Pair = stream.foldRight(Pair(0,0))(_ + _)
def lazyListFoldRight: Pair = lazyList.foldRight(Pair(0,0))(_ + _)
@Benchmark
def streamTailrec: Pair = {
@tailrec def work(l: Stream[Pair], acc: Pair): Pair = l match {
def lazyListTailrec: Pair = {
@tailrec def work(l: LazyList[Pair], acc: Pair): Pair = l match {
case _ if l.isEmpty => acc
case h #:: rest => work(rest, h + acc)
}

work(stream, Pair(0,0))
work(lazyList, Pair(0,0))
}
@Benchmark
def streamWhile: Pair = {
def lazyListWhile: Pair = {
var i: Pair = Pair(0,0)
var l: Stream[Pair] = stream
var l: LazyList[Pair] = lazyList

while (!l.isEmpty) {
i = i + l.head
Expand All @@ -145,4 +148,31 @@ class FoldClassBench {
i
}

@Benchmark
def chainFoldLeft: Pair = chain.foldLeft(Pair(0,0))(_ + _)
@Benchmark
def chainFoldRight: Pair = chain.foldRight(Pair(0,0))(_ + _)
@Benchmark
def chainTailrec: Pair = {
@tailrec def work(l: Chain[Pair], acc: Pair): Pair = l.uncons match {
case None => acc
case Some((h, rest)) => work(rest, h + acc)
}

work(chain, Pair(0,0))
}
@Benchmark
def chainWhile: Pair = {
var i: Pair = Pair(0,0)
var l: Chain[Pair] = chain
var uc = l.uncons

while (!uc.isEmpty) {
i += uc.get._1
l = uc.get._2
uc = l.uncons
}

i
}
}
Loading