|
| 1 | +/* |
| 2 | + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license |
| 3 | + * that can be found in the LICENSE file. |
| 4 | + */ |
| 5 | + |
| 6 | +import org.jetbrains.benchmarksLauncher.Blackhole |
| 7 | +import org.jetbrains.benchmarksLauncher.Random |
| 8 | + |
| 9 | +// Benchmark is inspired by multik library. |
| 10 | + |
| 11 | +interface MemoryView<T> where T : Number { |
| 12 | + fun get(index: Int): T |
| 13 | +} |
| 14 | + |
| 15 | +class MemoryViewIntArray(val data
8000
: IntArray) : MemoryView<Int> { |
| 16 | + override fun get(index: Int): Int = data[index] |
| 17 | +} |
| 18 | + |
| 19 | +class MemoryViewLongArray(val data: LongArray) : MemoryView<Long> { |
| 20 | + override fun get(index: Int): Long = data[index] |
| 21 | +} |
| 22 | + |
| 23 | +class MemoryViewDoubleArray(val data: DoubleArray) : MemoryView<Double> { |
| 24 | + override fun get(index: Int): Double = data[index] |
| 25 | +} |
| 26 | + |
| 27 | +class Array2D<T>(val data: MemoryView<T>, val width: Int) where T : Number{ |
| 28 | + fun getGeneric(ind1: Int, ind2: Int): Int { |
| 29 | + return data.get(width * ind1 + ind2).toInt() |
| 30 | + } |
| 31 | + |
| 32 | + inline fun getGenericInlined(ind1: Int, ind2: Int): Int { |
| 33 | + return data.get(width * ind1 + ind2).toInt() |
| 34 | + } |
| 35 | + |
| 36 | + inline fun getSpecializedInlined(ind1: Int, ind2: Int): Int { |
| 37 | + return (data as MemoryViewIntArray).get(width * ind1 + ind2) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +open class GenericArrayViewBenchmark { |
| 42 | + private val N = 2000 |
| 43 | + |
| 44 | + private val intArr = Array2D(MemoryViewIntArray(IntArray(N * N) { Random.nextInt() }), N) |
| 45 | + // To confuse devirtualizer: |
| 46 | + private val longArr = Array2D(MemoryViewLongArray(LongArray(N * N) { Random.nextInt().toLong() }), N) |
| 47 | + private val doubleArr = Array2D(MemoryViewDoubleArray(DoubleArray(N * N) { Random.nextDouble() }), N) |
| 48 | + |
| 49 | + init { |
| 50 | + bench(longArr) { a, i, j -> a.getGeneric(i, j) } |
| 51 | + bench(doubleArr) { a, i, j -> a.getGenericInlined(i, j) } |
| 52 | + try { bench(longArr) { a, i, j -> a.getSpecializedInlined(i, j) } } catch (t: ClassCastException) {} |
| 53 | + } |
| 54 | + |
| 55 | + private inline fun <T> bench(arr: Array2D<T>, getter: (Array2D<T>, Int, Int) -> Int) where T : Number { |
| 56 | + var sum = 0 |
| 57 | + |
| 58 | + for (i in 0 until N) { |
| 59 | + for (j in 0 until N) { |
| 60 | + sum += getter(arr, i, j) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + Blackhole.consume(sum) |
| 65 | + } |
| 66 | + |
| 67 | + // Bench cases: |
| 68 | + |
| 69 | + fun origin() { bench(intArr) { a, i, j -> a.getGeneric(i, j) } } |
| 70 | + fun inlined() { bench(intArr) { a, i, j -> a.getGenericInlined(i, j) } } |
| 71 | + fun specialized() { bench(intArr) { a, i, j -> a.getSpecializedInlined(i, j) } } |
| 72 | + fun manual() { bench(intArr) { a, i, j -> a.width * i + j } } |
| 73 | +} |
0 commit comments