Skip to content

Add benchmarks for specialization #9752

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

Closed
wants to merge 8 commits into from
Closed
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
1 change: 0 additions & 1 deletion bench-run/inputs/map.in

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion bench-run/inputs/zip.in

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package dotty.tools.benchmarks.specialization

import org.openjdk.jmh.annotations._
import scala.util.Random


@State(Scope.Benchmark)
class Functions {
extension (x: Int)
inline def times(inline work: Int): Int = {
var res = 0
var count = 0
while count < x do
Copy link
Member

Choose a reason for hiding this comment

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

I haven't checked what is being done here exactly but note that it's not recommended to use loops when benchmarking on the JVM: http://tutorials.jenkov.com/java-performance/jmh.html#writing-good-benchmarks, instead let JMH run things as many times as needed to get good statistics

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the reminder, @smarter . It's really subtle to defeat optimization in microbenchmarks.

res += work + 1
count += 1
res
}

class ByName {
def foo(x: => Int): Int = x
}

// outsmart JVM with storage in mutable array
var byName = new ByName
var arrByName = Array(byName, null)

@Benchmark
def byNameBench(): Int = 10000.times {
// necessary to outsmart JVM
// remove it will result in 200x speed up
arrByName(1) = null
arrByName(0).foo(6)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It turns out we also need this turnaround to defeat JVM optimization.



var fn = (x: Int) => x + 1
var arr = Array(fn, null)
@Benchmark
def lambdaBench(): Int = 10000.times {
arr(1) = null
arr(0)(2)
}

class Func1[T](fn: T => Int) extends Function1[T, Int] {
def apply(x: T): Int = fn(x)
}
class Fn extends Func1(identity[Int])

var fn1: Function1[Int, Int] = new Fn
var arr1 = Array(fn1, null)

@Benchmark
def extendFun1Bench(): Int = 10000.times {
arr1(1) = null
arr1(0)(12)
}


class Func2 extends Function2[Int, Int, Int] {
def apply(i: Int, j: Int) = i + j
}

var fn2: Function2[Int, Int, Int] = new Func2
var arr2 = Array(fn2, null)

@Benchmark
def extendFun2Bench(): Int = 10000.times {
arr2(1) = null
arr2(0)(1300, 37)
}
}