diff --git a/kotlin-native/performance/ring/src/main/kotlin/main.kt b/kotlin-native/performance/ring/src/main/kotlin/main.kt index 19c7e6f9d87..952fac5fbee 100644 --- a/kotlin-native/performance/ring/src/main/kotlin/main.kt +++ b/kotlin-native/performance/ring/src/main/kotlin/main.kt @@ -212,7 +212,8 @@ class RingLauncher : Launcher() { "LinkedListWithAtomicsBenchmark" to BenchmarkEntryWithInit.create(::LinkedListWithAtomicsBenchmark, { ensureNext() }), "Inheritance.baseCalls" to BenchmarkEntryWithInit.create(::InheritanceBenchmark, { baseCalls() }), "ChainableBenchmark.testChainable" to BenchmarkEntryWithInit.create(::ChainableBenchmark, { testChainable() }), - "BunnymarkBenchmark.testBunnymark" to BenchmarkEntryWithInit.create(::BunnymarkBenchmark, { testBunnymark() }) + "BunnymarkBenchmark.testBunnymark" to BenchmarkEntryWithInit.create(::BunnymarkBenchmark, { testBunnymark() }), + "ArrayCopyBenchmark.copyInSameArray" to BenchmarkEntryWithInit.create(::ArrayCopyBenchmark, { copyInSameArray() }) ) ) } diff --git a/kotlin-native/performance/ring/src/main/kotlin/org/jetbrains/ring/ArrayCopyBenchmark.kt b/kotlin-native/performance/ring/src/main/kotlin/org/jetbrains/ring/ArrayCopyBenchmark.kt new file mode 100644 index 00000000000..932360077b5 --- /dev/null +++ b/kotlin-native/performance/ring/src/main/kotlin/org/jetbrains/ring/ArrayCopyBenchmark.kt @@ -0,0 +1,59 @@ +/* + * Copyright 2010-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the LICENSE file. + */ + +package org.jetbrains.ring + +import org.jetbrains.benchmarksLauncher.Blackhole +import kotlin.random.Random + +open class ArrayCopyBenchmark { + class CustomArray(capacity: Int = 0) { + private var hashes: IntArray = IntArray(capacity) + private var values: Array = arrayOfNulls(capacity) as Array + private var _size: Int = 0 + + fun add(index: Int, element: T): Boolean { + val oldSize = _size + + // Grow the array if needed. + if (oldSize == hashes.size) { + val newSize = if (oldSize > 0) oldSize * 2 else 2 + hashes = hashes.copyOf(newSize) + values = values.copyOf(newSize) + } + + // Shift the array if needed. + if (index < oldSize) { + hashes.copyInto( + hashes, + destinationOffset = index + 1, + startIndex = index, + endIndex = oldSize + ) + values.copyInto( + values, + destinationOffset = index + 1, + startIndex = index, + endIndex = oldSize + ) + } + + hashes[index] = element.hashCode() + values[index] = element + + _size++ + return true + } + } + + //Benchmark + fun copyInSameArray(): CustomArray { + val array = CustomArray() + for (i in 0 until 2 * BENCHMARK_SIZE) { + array.add(0, i) + } + return array + } +}