From 85e637b1e7962c1c4d61d754799d59d9bbdfdf72 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Tue, 12 May 2015 16:05:03 +0300 Subject: [PATCH] Revert toArrayList for primitive arrays back to trivial implementation. Use newly introduced Int.MAX_VALUE in JS. Inline mapCapacityForValues function. Precalculate capacity of linked hash set being created with toMutableSet function. --- libraries/stdlib/src/generated/_Sets.kt | 18 +++++----- libraries/stdlib/src/generated/_Snapshots.kt | 34 ++++++++++++++----- .../stdlib/src/kotlin/collections/JUtil.kt | 6 ++-- .../stdlib/src/kotlin/collections/Maps.kt | 14 +++----- .../kotlin-stdlib-gen/src/templates/Sets.kt | 2 +- .../src/templates/Snapshots.kt | 16 ++++++++- 6 files changed, 59 insertions(+), 31 deletions(-) diff --git a/libraries/stdlib/src/generated/_Sets.kt b/libraries/stdlib/src/generated/_Sets.kt index 7ef5e6323ed..c7f901df21c 100644 --- a/libraries/stdlib/src/generated/_Sets.kt +++ b/libraries/stdlib/src/generated/_Sets.kt @@ -264,7 +264,7 @@ public fun Iterable.subtract(other: Iterable): Set { * Returns a mutable set containing all distinct elements from the given collection. */ public fun Array.toMutableSet(): MutableSet { - val set = LinkedHashSet(size()) + val set = LinkedHashSet(mapCapacity(size())) for (item in this) set.add(item) return set } @@ -273,7 +273,7 @@ public fun Array.toMutableSet(): MutableSet { * Returns a mutable set containing all distinct elements from the given collection. */ public fun BooleanArray.toMutableSet(): MutableSet { - val set = LinkedHashSet(size()) + val set = LinkedHashSet(mapCapacity(size())) for (item in this) set.add(item) return set } @@ -282,7 +282,7 @@ public fun BooleanArray.toMutableSet(): MutableSet { * Returns a mutable set containing all distinct elements from the given collection. */ public fun ByteArray.toMutableSet(): MutableSet { - val set = LinkedHashSet(size()) + val set = LinkedHashSet(mapCapacity(size())) for (item in this) set.add(item) return set } @@ -291,7 +291,7 @@ public fun ByteArray.toMutableSet(): MutableSet { * Returns a mutable set containing all distinct elements from the given collection. */ public fun CharArray.toMutableSet(): MutableSet { - val set = LinkedHashSet(size()) + val set = LinkedHashSet(mapCapacity(size())) for (item in this) set.add(item) return set } @@ -300,7 +300,7 @@ public fun CharArray.toMutableSet(): MutableSet { * Returns a mutable set containing all distinct elements from the given collection. */ public fun DoubleArray.toMutableSet(): MutableSet { - val set = LinkedHashSet(size()) + val set = LinkedHashSet(mapCapacity(size())) for (item in this) set.add(item) return set } @@ -309,7 +309,7 @@ public fun DoubleArray.toMutableSet(): MutableSet { * Returns a mutable set containing all distinct elements from the given collection. */ public fun FloatArray.toMutableSet(): MutableSet { - val set = LinkedHashSet(size()) + val set = LinkedHashSet(mapCapacity(size())) for (item in this) set.add(item) return set } @@ -318,7 +318,7 @@ public fun FloatArray.toMutableSet(): MutableSet { * Returns a mutable set containing all distinct elements from the given collection. */ public fun IntArray.toMutableSet(): MutableSet { - val set = LinkedHashSet(size()) + val set = LinkedHashSet(mapCapacity(size())) for (item in this) set.add(item) return set } @@ -327,7 +327,7 @@ public fun IntArray.toMutableSet(): MutableSet { * Returns a mutable set containing all distinct elements from the given collection. */ public fun LongArray.toMutableSet(): MutableSet { - val set = LinkedHashSet(size()) + val set = LinkedHashSet(mapCapacity(size())) for (item in this) set.add(item) return set } @@ -336,7 +336,7 @@ public fun LongArray.toMutableSet(): MutableSet { * Returns a mutable set containing all distinct elements from the given collection. */ public fun ShortArray.toMutableSet(): MutableSet { - val set = LinkedHashSet(size()) + val set = LinkedHashSet(mapCapacity(size())) for (item in this) set.add(item) return set } diff --git a/libraries/stdlib/src/generated/_Snapshots.kt b/libraries/stdlib/src/generated/_Snapshots.kt index d3675e141a7..0afa499dcc7 100644 --- a/libraries/stdlib/src/generated/_Snapshots.kt +++ b/libraries/stdlib/src/generated/_Snapshots.kt @@ -21,56 +21,72 @@ public fun Array.toArrayList(): ArrayList { * Returns an ArrayList of all elements */ public fun BooleanArray.toArrayList(): ArrayList { - return this.asList().toArrayList() + val list = ArrayList(size()) + for (item in this) list.add(item) + return list } /** * Returns an ArrayList of all elements */ public fun ByteArray.toArrayList(): ArrayList { - return this.asList().toArrayList() + val list = ArrayList(size()) + for (item in this) list.add(item) + return list } /** * Returns an ArrayList of all elements */ public fun CharArray.toArrayList(): ArrayList { - return this.asList().toArrayList() + val list = ArrayList(size()) + for (item in this) list.add(item) + return list } /** * Returns an ArrayList of all elements */ public fun DoubleArray.toArrayList(): ArrayList { - return this.asList().toArrayList() + val list = ArrayList(size()) + for (item in this) list.add(item) + return list } /** * Returns an ArrayList of all elements */ public fun FloatArray.toArrayList(): ArrayList { - return this.asList().toArrayList() + val list = ArrayList(size()) + for (item in this) list.add(item) + return list } /** * Returns an ArrayList of all elements */ public fun IntArray.toArrayList(): ArrayList { - return this.asList().toArrayList() + val list = ArrayList(size()) + for (item in this) list.add(item) + return list } /** * Returns an ArrayList of all elements */ public fun LongArray.toArrayList(): ArrayList { - return this.asList().toArrayList() + val list = ArrayList(size()) + for (item in this) list.add(item) + return list } /** * Returns an ArrayList of all elements */ public fun ShortArray.toArrayList(): ArrayList { - return this.asList().toArrayList() + val list = ArrayList(size()) + for (item in this) list.add(item) + return list } /** @@ -84,6 +100,8 @@ public fun Collection.toArrayList(): ArrayList { * Returns an ArrayList of all elements */ public fun Iterable.toArrayList(): ArrayList { + if (this is Collection) + return this.toArrayList() return toCollection(ArrayList()) } diff --git a/libraries/stdlib/src/kotlin/collections/JUtil.kt b/libraries/stdlib/src/kotlin/collections/JUtil.kt index 4993a3bcdcf..d6fa2899de7 100644 --- a/libraries/stdlib/src/kotlin/collections/JUtil.kt +++ b/libraries/stdlib/src/kotlin/collections/JUtil.kt @@ -46,7 +46,7 @@ public fun listOf(vararg values: T): List = if (values.size() == 0) emptyL public fun listOf(): List = emptyList() /** Returns a new read-only ordered set with the given elements. */ -public fun setOf(vararg values: T): Set = if (values.size() == 0) emptySet() else values.toCollection(LinkedHashSet(mapCapacityForValues(values))) +public fun setOf(vararg values: T): Set = if (values.size() == 0) emptySet() else values.toCollection(LinkedHashSet(mapCapacity(values.size()))) /** Returns an empty read-only set. */ public fun setOf(): Set = emptySet() @@ -58,10 +58,10 @@ public fun linkedListOf(vararg values: T): LinkedList = values.toCollectio public fun arrayListOf(vararg values: T): ArrayList = values.toCollection(ArrayList(values.size())) /** Returns a new [HashSet] with the given elements. */ -public fun hashSetOf(vararg values: T): HashSet = values.toCollection(HashSet(mapCapacityForValues(values))) +public fun hashSetOf(vararg values: T): HashSet = values.toCollection(HashSet(mapCapacity(values.size()))) /** Returns a new [LinkedHashSet] with the given elements. */ -public fun linkedSetOf(vararg values: T): LinkedHashSet = values.toCollection(LinkedHashSet(mapCapacityForValues(values))) +public fun linkedSetOf(vararg values: T): LinkedHashSet = values.toCollection(LinkedHashSet(mapCapacity(values.size()))) /** * Returns an [IntRange] of the valid indices for this collection. diff --git a/libraries/stdlib/src/kotlin/collections/Maps.kt b/libraries/stdlib/src/kotlin/collections/Maps.kt index 7c7fc425821..10a9d03e347 100644 --- a/libraries/stdlib/src/kotlin/collections/Maps.kt +++ b/libraries/stdlib/src/kotlin/collections/Maps.kt @@ -38,7 +38,7 @@ public fun mapOf(): Map = emptyMap() * @sample test.collections.MapTest.createUsingPairs */ public fun hashMapOf(vararg values: Pair): HashMap { - val answer = HashMap(mapCapacityForValues(values)) + val answer = HashMap(mapCapacity(values.size())) answer.putAll(*values) return answer } @@ -51,7 +51,7 @@ public fun hashMapOf(vararg values: Pair): HashMap { * @sample test.collections.MapTest.createLinkedMap */ public fun linkedMapOf(vararg values: Pair): LinkedHashMap { - val answer = LinkedHashMap(mapCapacityForValues(values)) + val answer = LinkedHashMap(mapCapacity(values.size())) answer.putAll(*values) return answer } @@ -62,20 +62,16 @@ public fun linkedMapOf(vararg values: Pair): LinkedHashMap { * very large sizes, allows support non-collection classes, and provides consistency for all map based class construction. */ -private val MAX_POWER_OF_TWO: Int = 1 shl (Integer.SIZE - 2) +private val INT_MAX_POWER_OF_TWO: Int = Int.MAX_VALUE / 2 + 1 private fun mapCapacity(expectedSize: Int): Int { if (expectedSize < 3) { return expectedSize + 1 } - if (expectedSize < MAX_POWER_OF_TWO) { + if (expectedSize < INT_MAX_POWER_OF_TWO) { return expectedSize + expectedSize / 3 } - return Integer.MAX_VALUE // any large value -} - -private fun mapCapacityForValues(values: Array): Int { - return mapCapacity(values.size()) + return Int.MAX_VALUE // any large value } /** diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Sets.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Sets.kt index fba02b30868..452a5682a16 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Sets.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Sets.kt @@ -19,7 +19,7 @@ fun sets(): List { } body(ArraysOfObjects, ArraysOfPrimitives) { """ - val set = LinkedHashSet(size()) + val set = LinkedHashSet(mapCapacity(size())) for (item in this) set.add(item) return set """ diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt index d409c266944..207054efff3 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt @@ -48,9 +48,23 @@ fun snapshots(): List { doc { "Returns an ArrayList of all elements" } returns("ArrayList") body { "return toCollection(ArrayList())" } + body(Iterables) { + """ + if (this is Collection) + return this.toArrayList() + return toCollection(ArrayList()) + """ + } body(Collections) { "return ArrayList(this)" } body(Strings) { "return toCollection(ArrayList(length()))" } - body(ArraysOfObjects, ArraysOfPrimitives) { "return this.asList().toArrayList()" } + body(ArraysOfObjects) { "return this.asList().toArrayList()" } + body(ArraysOfPrimitives) { + """ + val list = ArrayList(size()) + for (item in this) list.add(item) + return list + """ + } } templates add f("toList()") {