From 917930ba5364e27f81ef447d5fa35ab642b3abfd Mon Sep 17 00:00:00 2001 From: Nikolay Igotti Date: Wed, 7 Feb 2018 10:52:51 +0300 Subject: [PATCH] Make empty array handling more efficient, also fixes KT-21758 (#1285) * Make empty array handling more efficient, also fixes KT-21758 * Fix WebAssembly. --- runtime/src/main/cpp/Arrays.cpp | 12 ++++++++++++ runtime/src/main/cpp/Types.h | 1 - runtime/src/main/kotlin/kotlin/Arrays.kt | 6 ++---- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/runtime/src/main/cpp/Arrays.cpp b/runtime/src/main/cpp/Arrays.cpp index 4b5dc271176..f344e9b7cfd 100644 --- a/runtime/src/main/cpp/Arrays.cpp +++ b/runtime/src/main/cpp/Arrays.cpp @@ -39,6 +39,14 @@ static inline void copyImpl(KConstRef thiz, KInt fromIndex, count * sizeof(T)); } +namespace { + +const ArrayHeader anEmptyArray = { + theArrayTypeInfo, /* permanent object */ 0, /* element count */ 0 +}; + +} // namespace + extern "C" { // TODO: those must be compiler intrinsics afterwards. @@ -98,6 +106,10 @@ void Kotlin_Array_copyImpl(KConstRef thiz, KInt fromIndex, } // Arrays.kt +OBJ_GETTER0(Kotlin_emptyArray) { + RETURN_OBJ(const_cast(anEmptyArray.obj())); +} + KByte Kotlin_ByteArray_get(KConstRef thiz, KInt index) { const ArrayHeader* array = thiz->array(); if (static_cast(index) >= array->count_) { diff --git a/runtime/src/main/cpp/Types.h b/runtime/src/main/cpp/Types.h index 2219cc818b4..33338e71065 100644 --- a/runtime/src/main/cpp/Types.h +++ b/runtime/src/main/cpp/Types.h @@ -71,7 +71,6 @@ extern "C" { #endif extern const TypeInfo* theAnyTypeInfo; -extern const TypeInfo* theCloneableTypeInfo; extern const TypeInfo* theArrayTypeInfo; extern const TypeInfo* theByteArrayTypeInfo; extern const TypeInfo* theCharArrayTypeInfo; diff --git a/runtime/src/main/kotlin/kotlin/Arrays.kt b/runtime/src/main/kotlin/kotlin/Arrays.kt index ec785784b3f..168705f9c80 100644 --- a/runtime/src/main/kotlin/kotlin/Arrays.kt +++ b/runtime/src/main/kotlin/kotlin/Arrays.kt @@ -12667,10 +12667,8 @@ public inline fun Collection.toTypedArray(): Array { @Suppress("UNCHECKED_CAST") public inline fun arrayOf(vararg elements: T): Array = elements as Array -private val kEmptyArray = arrayOf() - -@Suppress("UNCHECKED_CAST") -public fun emptyArray() = kEmptyArray as Array +@SymbolName("Kotlin_emptyArray") +external public fun emptyArray(): Array /** * Returns an array containing the specified [Double] numbers.