From 446cdc087d1db1fff1a6e722d55c027d58196ef9 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Thu, 15 Oct 2015 22:59:06 +0300 Subject: [PATCH] Optimizations in listOf and arrayListOf constructor functions. Remove linkedListOf() from JS. #KT-5703 Fixed #KT-8519 Fixed --- js/js.libraries/src/core/javautilCode.kt | 33 ++++--------------- js/js.translator/testData/kotlin_lib.js | 2 ++ .../stdlib/src/kotlin/collections/JUtil.kt | 23 +++++++++---- 3 files changed, 24 insertions(+), 34 deletions(-) diff --git a/js/js.libraries/src/core/javautilCode.kt b/js/js.libraries/src/core/javautilCode.kt index d5f60308e1c..c7e51323c09 100644 --- a/js/js.libraries/src/core/javautilCode.kt +++ b/js/js.libraries/src/core/javautilCode.kt @@ -16,34 +16,13 @@ package java.util -public fun HashSet(c: Collection): HashSet { - val set: HashSet = HashSet(c.size()) - set.addAll(c) - return set -} +public fun HashSet(c: Collection): HashSet = HashSet(c.size()).apply { addAll(c) } -public fun LinkedHashSet(c: Collection): HashSet { - val set: LinkedHashSet = LinkedHashSet(c.size()) - set.addAll(c) - return set -} +public fun LinkedHashSet(c: Collection): HashSet = LinkedHashSet(c.size()).apply { addAll(c) } -public fun HashMap(m: Map): HashMap { - val map: HashMap = HashMap(m.size()) - map.putAll(m) - return map -} +public fun HashMap(m: Map): HashMap = HashMap(m.size()).apply { putAll(m) } -public fun LinkedHashMap(m: Map): LinkedHashMap { - val map: LinkedHashMap = LinkedHashMap(m.size()) - map.putAll(m) - return map -} +public fun LinkedHashMap(m: Map): LinkedHashMap = LinkedHashMap(m.size()).apply { putAll(m) } + +public fun ArrayList(c: Collection): ArrayList = ArrayList().apply { asDynamic().array = c.toTypedArray() } // black dynamic magic -public fun ArrayList(c: Collection): ArrayList { - val result = ArrayList() - for (element in c) { - result.add(element) - } - return result -} diff --git a/js/js.translator/testData/kotlin_lib.js b/js/js.translator/testData/kotlin_lib.js index a859754aa72..6f1b0cb7032 100644 --- a/js/js.translator/testData/kotlin_lib.js +++ b/js/js.translator/testData/kotlin_lib.js @@ -1018,6 +1018,8 @@ }; Kotlin.copyToArray = function (collection) { + if (typeof collection.toArray !== "undefined") return collection.toArray(); + var array = []; var it = collection.iterator(); while (it.hasNext()) { diff --git a/libraries/stdlib/src/kotlin/collections/JUtil.kt b/libraries/stdlib/src/kotlin/collections/JUtil.kt index 2f045f00cac..23c8418154f 100644 --- a/libraries/stdlib/src/kotlin/collections/JUtil.kt +++ b/libraries/stdlib/src/kotlin/collections/JUtil.kt @@ -44,29 +44,38 @@ internal object EmptyList : List, Serializable { private fun readResolve(): Any = EmptyList } - +private class ArrayAsCollection(val values: Array): Collection { + override val size: Int get() = values.size() + override fun isEmpty(): Boolean = values.isEmpty() + override fun contains(o: T): Boolean = values.contains(o) + override fun containsAll(c: Collection): Boolean = c.all { contains(it) } + override fun iterator(): Iterator = values.iterator() + // override of hidden toArray method implementation to prevent copying of values array + public fun toArray(): Array = values +} /** Returns an empty read-only list. The returned list is serializable (JVM). */ -public fun emptyList(): List = EmptyList +public fun emptyList(): List = EmptyList /** Returns a new read-only list of given elements. The returned list is serializable (JVM). */ -public fun listOf(vararg values: T): List = if (values.size() > 0) arrayListOf(*values) else emptyList() +public fun listOf(vararg values: T): List = if (values.size() > 0) values.asList() else emptyList() /** Returns an empty read-only list. The returned list is serializable (JVM). */ -public fun listOf(): List = emptyList() +public fun listOf(): List = emptyList() /** * Returns an immutable list containing only the specified object [value]. * The returned list is serializable. */ @JvmVersion -public fun listOf(value: T): List = Collections.singletonList(value) +public fun listOf(value: T): List = Collections.singletonList(value) /** Returns a new [LinkedList] with the given elements. */ -public fun linkedListOf(vararg values: T): LinkedList = values.toCollection(LinkedList()) +@JvmVersion +public fun linkedListOf(vararg values: T): LinkedList = LinkedList(ArrayAsCollection(values)) /** Returns a new [ArrayList] with the given elements. */ -public fun arrayListOf(vararg values: T): ArrayList = values.toCollection(ArrayList(values.size())) +public fun arrayListOf(vararg values: T): ArrayList = ArrayList(ArrayAsCollection(values)) /** Returns a new read-only list either of single given element, if it is not null, or empty list it the element is null. The returned list is serializable (JVM). */ public fun listOfNotNull(value: T?): List = if (value != null) listOf(value) else emptyList()