Skip to content
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
149 changes: 149 additions & 0 deletions bench/src/main/scala-2/cats/collections/bench/ChunkedSeqBench.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* Copyright (c) 2015 Typelevel
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package cats.collections
package bench

import org.openjdk.jmh.annotations._
import org.openjdk.jmh.infra.Blackhole

import scala.annotation.tailrec

@State(Scope.Thread)
class ChunkedSeqBench {
@Param(Array("100", "1000", "10000"))
var n: Int = _

var chunkedSeq: ChunkedSeq[Int] = _
var chunkedSeqFromPrepend: ChunkedSeq[Int] = _
var list: List[Int] = _
var vect: Vector[Int] = _

@Setup
def setup(): Unit = {
list = (0 until n).toList
chunkedSeq = ChunkedSeq.fromList(list)
vect = list.toVector

// Build a ChunkedSeq via prepend to test deep-tree performance
var cs: ChunkedSeq[Int] = ChunkedSeq.empty
(0 until n).foreach { i => cs = i :: cs }
chunkedSeqFromPrepend = cs
}

@Benchmark
def sumList(bh: Blackhole): Unit = {
@tailrec
def loop(ls: List[Int], acc: Int): Int =
ls match {
case Nil => acc
case h :: tail => loop(tail, acc + h)
}
bh.consume(loop(list, 0))
}

@Benchmark
def sumVector(bh: Blackhole): Unit = {
@tailrec
def loop(ls: Vector[Int], acc: Int): Int =
if (ls.isEmpty) acc
else loop(ls.init, ls.last + acc)
bh.consume(loop(vect, 0))
}

@Benchmark
def sumChunkedSeq(bh: Blackhole): Unit = {
bh.consume(chunkedSeq.foldLeft(0)(_ + _))
}

@Benchmark
def sumChunkedSeqFromPrepend(bh: Blackhole): Unit = {
bh.consume(chunkedSeqFromPrepend.foldLeft(0)(_ + _))
}

@Benchmark
def randomAccessList(bh: Blackhole): Unit = {
val rand = new java.util.Random(42)
@tailrec
def loop(cnt: Int, acc: Int): Int = {
val v = list((rand.nextInt() & Int.MaxValue) % n) + acc
if (cnt <= 0) v
else loop(cnt - 1, v)
}
bh.consume(loop(100, 0))
}

@Benchmark
def randomAccessVector(bh: Blackhole): Unit = {
val rand = new java.util.Random(42)
@tailrec
def loop(cnt: Int, acc: Int): Int = {
val v = vect((rand.nextInt() & Int.MaxValue) % n) + acc
if (cnt <= 0) v
else loop(cnt - 1, v)
}
bh.consume(loop(100, 0))
}

@Benchmark
def randomAccessChunkedSeq(bh: Blackhole): Unit = {
val rand = new java.util.Random(42)
@tailrec
def loop(cnt: Int, acc: Int): Int = {
val v = chunkedSeq.getUnsafe((rand.nextInt() & Int.MaxValue).toLong % n) + acc
if (cnt <= 0) v
else loop(cnt - 1, v)
}
bh.consume(loop(100, 0))
}

@Benchmark
def prependList(bh: Blackhole): Unit = {
var ls: List[Int] = Nil
var i = 0
while (i < n) { ls = i :: ls; i += 1 }
bh.consume(ls)
}

@Benchmark
def prependChunkedSeq(bh: Blackhole): Unit = {
var cs: ChunkedSeq[Int] = ChunkedSeq.empty
var i = 0
while (i < n) { cs = i :: cs; i += 1 }
bh.consume(cs)
}

@Benchmark
def appendVector(bh: Blackhole): Unit = {
var v: Vector[Int] = Vector.empty
var i = 0
while (i < n) { v = v :+ i; i += 1 }
bh.consume(v)
}

@Benchmark
def appendChunkedSeq(bh: Blackhole): Unit = {
var cs: ChunkedSeq[Int] = ChunkedSeq.empty
var i = 0
while (i < n) { cs = cs :+ i; i += 1 }
bh.consume(cs)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

can you post the results of running this benchmark?

Copy link
Author

Choose a reason for hiding this comment

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

Here are the benchmark results for the current (tree-of-chunks) implementation:

Environment: Linux, JDK 21.0.10 (OpenJDK), JMH 1.37, 3 warmup / 5 measurement iterations, 1 fork

Benchmark                                   (n)   Mode  Cnt        Score        Error  Units
ChunkedSeqBench.prependList                  100  thrpt    5  1404269.542 ± 319406.301  ops/s
ChunkedSeqBench.prependChunkedSeq            100  thrpt    5   260304.083 ±  99441.675  ops/s
ChunkedSeqBench.prependList                 1000  thrpt    5    92515.375 ±  16912.586  ops/s
ChunkedSeqBench.prependChunkedSeq           1000  thrpt    5    26580.504 ±   4959.982  ops/s
ChunkedSeqBench.prependList                10000  thrpt    5     9157.360 ±   1389.371  ops/s
ChunkedSeqBench.prependChunkedSeq          10000  thrpt    5     2683.471 ±    658.784  ops/s

ChunkedSeqBench.appendVector                 100  thrpt    5   106538.875 ±  30318.740  ops/s
ChunkedSeqBench.appendChunkedSeq             100  thrpt    5   222856.981 ±  56341.714  ops/s
ChunkedSeqBench.appendVector                1000  thrpt    5    10952.448 ±   1249.280  ops/s
ChunkedSeqBench.appendChunkedSeq            1000  thrpt    5    18305.411 ±   3957.131  ops/s
ChunkedSeqBench.appendVector               10000  thrpt    5     1269.500 ±    522.176  ops/s
ChunkedSeqBench.appendChunkedSeq           10000  thrpt    5     2053.364 ±    545.453  ops/s

ChunkedSeqBench.sumList                      100  thrpt    5  2607832.084 ± 269022.770  ops/s
ChunkedSeqBench.sumChunkedSeq                100  thrpt    5  1123287.645 ± 132675.176  ops/s
ChunkedSeqBench.sumChunkedSeqFromPrepend     100  thrpt    5   284501.765 ±  38095.094  ops/s
ChunkedSeqBench.sumList                     1000  thrpt    5   254294.620 ±  40244.658  ops/s
ChunkedSeqBench.sumChunkedSeq               1000  thrpt    5   116728.143 ±   9345.607  ops/s
ChunkedSeqBench.sumChunkedSeqFromPrepend    1000  thrpt    5    27174.670 ±   4925.705  ops/s
ChunkedSeqBench.sumList                    10000  thrpt    5    24305.200 ±   2782.436  ops/s
ChunkedSeqBench.sumChunkedSeq              10000  thrpt    5    10597.345 ±   2107.648  ops/s
ChunkedSeqBench.sumChunkedSeqFromPrepend   10000  thrpt    5     2671.329 ±    905.274  ops/s

ChunkedSeqBench.randomAccessVector           100  thrpt    5   435440.887 ±  35263.835  ops/s
ChunkedSeqBench.randomAccessChunkedSeq       100  thrpt    5   224730.551 ±  38789.073  ops/s
ChunkedSeqBench.randomAccessList             100  thrpt    5    94615.869 ±  16056.867  ops/s
ChunkedSeqBench.randomAccessVector          1000  thrpt    5   420804.935 ±  33169.638  ops/s
ChunkedSeqBench.randomAccessChunkedSeq      1000  thrpt    5   157360.262 ±  32886.195  ops/s
ChunkedSeqBench.randomAccessList            1000  thrpt    5     7131.094 ±   1797.918  ops/s
ChunkedSeqBench.randomAccessVector         10000  thrpt    5   355877.005 ±  37412.779  ops/s
ChunkedSeqBench.randomAccessChunkedSeq     10000  thrpt    5   105170.238 ±  16941.927  ops/s
ChunkedSeqBench.randomAccessList           10000  thrpt    5      664.834 ±     61.015  ops/s

Thanks @johnynek , the current Concat-tree design doesn't beat List on prepend or iteration 😞 , which is the whole point. The tree overhead negates the cache locality benefit (especially visible in sumChunkedSeqFromPrepend where the tree built by repeated :: is 9× slower than List's sum).

I'll please rework the implementation to the linked-list-of-arrays design you described and re-run these benchmarks.

Loading