8000 [K/N] Add ring/GenericArrayView bench · AndroidKotlinID/kotlin@b01ef84 · GitHub
[go: up one dir, main page]

Skip to content

Commit b01ef84

Browse files
Aleksei.GlushkoSpace Team
authored andcommitted
[K/N] Add ring/GenericArrayView bench
also .gitignore cmake-build-debug on all levels Merge-request: KT-MR-8412 Merged-by: Alexey Glushko <aleksei.glushko@jetbrains.com>
1 parent 6a343e7 commit b01ef84

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

kotlin-native/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ samples/**/*.kt.bc-build
6060
tools/performance-server/node_modules
6161
tools/performance-server/server
6262
tools/performance-server/ui/js
63-
runtime/cmake-build-debug/
63+
cmake-build-debug/
6464

6565
# compilation database
6666
compile_commands.json

kotlin-native/performance/ring/src/main/kotlin/main.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,10 @@ class RingLauncher : Launcher() {
229229
"Calls.interfaceMethodHexamorphic" to BenchmarkEntryWithInit.create(::CallsBenchmark, { interfaceMethodCall_HexamorphicCallsite() }),
230230
"LocalObjects.localArray" to BenchmarkEntryWithInit.create(::LocalObjectsBenchmark, { localArray() }),
231231
"ComplexArrays.outerProduct" to BenchmarkEntryWithInit.create(::ComplexArraysBenchmark, { outerProduct() }),
232+
"GenericArrayView.origin" to BenchmarkEntryWithInit.create(::GenericArrayViewBenchmark, { origin() }),
233+
"GenericArrayView.inlined" to BenchmarkEntryWithInit.create(::GenericArrayViewBenchmark, { inlined() }),
234+
"GenericArrayView.specialized" to BenchmarkEntryWithInit.create(::GenericArrayViewBenchmark, { specialized() }),
235+
"GenericArrayView.manual" to BenchmarkEntryWithInit.create(::GenericArrayViewBenchmark, { manual() }),
232236
"WeakRefBenchmark.aliveReference" to BenchmarkEntryWithInit.create(::WeakRefBenchmark, { aliveReference() }),
233237
"WeakRefBenchmark.deadReference" to BenchmarkEntryWithInit.create(::WeakRefBenchmark, { deadReference() }),
234238
"WeakRefBenchmark.dyingReference" to BenchmarkEntryWithInit.create(::WeakRefBenchmark, { dyingReference() }),
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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

Comments
 (0)
0