Skip to content

Commit 8502ffb

Browse files
committed
Make sure boxing effect detectable
With this change the two benchmarks shows the effect of boxing: - lambdaBench: 445.352 ns/op - lambdaNoboxBench: 60.220 ns/op Previously, they have the same result because the boxing effect is shadowed by the method call effect. By doing more unboxing work in the method, we make sure the effect is visible in the result.
1 parent 62339a0 commit 8502ffb

File tree

1 file changed

+18
-6
lines changed
  • bench-run/src/main/scala/dotty/tools/benchmarks/specialization

1 file changed

+18
-6
lines changed

bench-run/src/main/scala/dotty/tools/benchmarks/specialization/Functions.scala

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,38 +33,50 @@ class Functions {
3333
}
3434

3535

36-
var fn = (x: Int) => x + 1
36+
var fn = (x: Int) => 100000.times { x } + 5
3737
var arr = Array(fn, null)
3838
@Benchmark
39-
def lambdaBench(): Int = 10000.times {
39+
def lambdaBench(): Int = 1000.times {
4040
arr(1) = null
4141
arr(0)(2)
4242
}
4343

44+
class FunctionNoBox extends Function1[Int, Int] {
45+
def apply(x: Int): Int = 10000.times { x } + 5
46+
}
47+
var fnNobox = new FunctionNoBox
48+
var arrNobox: Array[FunctionNoBox] = Array(fnNobox, null)
49+
@Benchmark
50+
def lambdaNoboxBench(): Int = 1000.times {
51+
arrNobox(1) = null
52+
arrNobox(0)(2)
53+
}
54+
55+
4456
class Func1[T](fn: T => Int) extends Function1[T, Int] {
4557
def apply(x: T): Int = fn(x)
4658
}
47-
class Fn extends Func1(identity[Int])
59+
class Fn extends Func1((x: Int) => 10000.times { x } + 5)
4860

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

5264
@Benchmark
53-
def extendFun1Bench(): Int = 10000.times {
65+
def extendFun1Bench(): Int = 1000.times {
5466
arr1(1) = null
5567
arr1(0)(12)
5668
}
5769

5870

5971
class Func2 extends Function2[Int, Int, Int] {
60-
def apply(i: Int, j: Int) = i + j
72+
def apply(i: Int, j: Int) = 10000.times { i } + j
6173
}
6274

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

6678
@Benchmark
67-
def extendFun2Bench(): Int = 10000.times {
79+
def extendFun2Bench(): Int = 1000.times {
6880
arr2(1) = null
6981
arr2(0)(1300, 37)
7082
}

0 commit comments

Comments
 (0)