Skip to content

Commit 62339a0

Browse files
committed
Outsmart JVM
1 parent cdc14b0 commit 62339a0

File tree

1 file changed

+23
-4
lines changed
  • bench-run/src/main/scala/dotty/tools/benchmarks/specialization

1 file changed

+23
-4
lines changed

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,52 @@ class Functions {
2020
def foo(x: => Int): Int = x
2121
}
2222

23+
// outsmart JVM with storage in mutable array
2324
var byName = new ByName
25+
var arrByName = Array(byName, null)
2426

2527
@Benchmark
26-
def byNameBench(): Int = 10000.times { byName.foo(6) }
28+
def byNameBench(): Int = 10000.times {
29+
// necessary to outsmart JVM
30+
// remove it will result in 200x speed up
31+
arrByName(1) = null
32+
arrByName(0).foo(6)
33+
}
2734

2835

2936
var fn = (x: Int) => x + 1
37+
var arr = Array(fn, null)
3038
@Benchmark
31-
def lambdaBench(): Int = 10000.times { fn(2) }
39+
def lambdaBench(): Int = 10000.times {
40+
arr(1) = null
41+
arr(0)(2)
42+
}
3243

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

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

4052
@Benchmark
41-
def extendFun1Bench(): Int = 10000.times { fn1(12) }
53+
def extendFun1Bench(): Int = 10000.times {
54+
arr1(1) = null
55+
arr1(0)(12)
56+
}
4257

4358

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

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

5066
@Benchmark
51-
def extendFun2Bench(): Int = 1000000.times { fn2(1300, 37) }
67+
def extendFun2Bench(): Int = 10000.times {
68+
arr2(1) = null
69+
arr2(0)(1300, 37)
70+
}
5271
}

0 commit comments

Comments
 (0)