From 1c1492da3ddcdcd27c64d9eacb2e3ea12e3bd186 Mon Sep 17 00:00:00 2001 From: Abduqodiri Qurbonzoda Date: Tue, 2 Mar 2021 10:45:00 +0300 Subject: [PATCH] Optimize K/N ArrayList.toArray #KT-42720 --- .../kotlin/kotlin/collections/ArrayList.kt | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/collections/ArrayList.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/collections/ArrayList.kt index 553fa452b73..bca09b4a0f7 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/collections/ArrayList.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/collections/ArrayList.kt @@ -164,6 +164,26 @@ actual class ArrayList private constructor( return array.subarrayContentToString(offset, length) } + @Suppress("UNCHECKED_CAST") + override fun toArray(destination: Array): Array { + if (destination.size < length) { + return array.copyOfRange(fromIndex = offset, toIndex = offset + length) as Array + } + + (array as Array).copyInto(destination, 0, startIndex = offset, endIndex = offset + length) + + if (destination.size > length) { + destination[length] = null as T // null-terminate + } + + return destination + } + + override fun toArray(): Array { + @Suppress("UNCHECKED_CAST") + return array.copyOfRange(fromIndex = offset, toIndex = offset + length) as Array + } + // ---------------------------- private ---------------------------- private fun checkElementIndex(index: Int) {