@@ -20,33 +20,52 @@ class Functions {
20
20
def foo (x : => Int ): Int = x
21
21
}
22
22
23
+ // outsmart JVM with storage in mutable array
23
24
var byName = new ByName
25
+ var arrByName = Array (byName, null )
24
26
25
27
@ 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
+ }
27
34
28
35
29
36
var fn = (x : Int ) => x + 1
37
+ var arr = Array (fn, null )
30
38
@ Benchmark
31
- def lambdaBench (): Int = 10000 .times { fn(2 ) }
39
+ def lambdaBench (): Int = 10000 .times {
40
+ arr(1 ) = null
41
+ arr(0 )(2 )
42
+ }
32
43
33
44
class Func1 [T ](fn : T => Int ) extends Function1 [T , Int ] {
34
45
def apply (x : T ): Int = fn(x)
35
46
}
36
47
class Fn extends Func1 (identity[Int ])
37
48
38
49
var fn1 : Function1 [Int , Int ] = new Fn
50
+ var arr1 = Array (fn1, null )
39
51
40
52
@ Benchmark
41
- def extendFun1Bench (): Int = 10000 .times { fn1(12 ) }
53
+ def extendFun1Bench (): Int = 10000 .times {
54
+ arr1(1 ) = null
55
+ arr1(0 )(12 )
56
+ }
42
57
43
58
44
59
class Func2 extends Function2 [Int , Int , Int ] {
45
60
def apply (i : Int , j : Int ) = i + j
46
61
}
47
62
48
63
var fn2 : Function2 [Int , Int , Int ] = new Func2
64
+ var arr2 = Array (fn2, null )
49
65
50
66
@ 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
+ }
52
71
}
0 commit comments