Optimize snapshot operations to return special collection implementations when result is empty or has single element.
#KT-9990 Fixed
This commit is contained in:
@@ -23,10 +23,31 @@ fun snapshots(): List<GenericFunction> {
|
||||
templates add f("toSet()") {
|
||||
doc { f -> "Returns a [Set] of all ${f.element.pluralize()}." }
|
||||
returns("Set<T>")
|
||||
body { "return toCollection(LinkedHashSet<T>(mapCapacity(collectionSizeOrDefault(12))))" }
|
||||
body(Sequences) { "return toCollection(LinkedHashSet<T>())" }
|
||||
body(CharSequences) { "return toCollection(LinkedHashSet<T>(mapCapacity(length)))" }
|
||||
body(ArraysOfObjects, ArraysOfPrimitives) { "return toCollection(LinkedHashSet<T>(mapCapacity(size)))" }
|
||||
body(Iterables) {
|
||||
"""
|
||||
if (this is Collection) {
|
||||
return when (size) {
|
||||
0 -> emptySet()
|
||||
1 -> setOf(if (this is List) this[0] else iterator().next())
|
||||
else -> toCollection(LinkedHashSet<T>(mapCapacity(size)))
|
||||
}
|
||||
|
||||
}
|
||||
return toCollection(LinkedHashSet<T>()).optimizeReadOnlySet()
|
||||
"""
|
||||
}
|
||||
body(Sequences) { "return toCollection(LinkedHashSet<T>()).optimizeReadOnlySet()" }
|
||||
|
||||
body(CharSequences, ArraysOfObjects, ArraysOfPrimitives) { f ->
|
||||
val size = if (f == CharSequences) "length" else "size"
|
||||
"""
|
||||
return when ($size) {
|
||||
0 -> emptySet()
|
||||
1 -> setOf(this[0])
|
||||
else -> toCollection(LinkedHashSet<T>(mapCapacity($size)))
|
||||
}
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
templates add f("toHashSet()") {
|
||||
@@ -90,9 +111,19 @@ fun snapshots(): List<GenericFunction> {
|
||||
returns("List<Pair<K, V>>")
|
||||
body {
|
||||
"""
|
||||
if (size == 0)
|
||||
return emptyList()
|
||||
val iterator = entries.iterator()
|
||||
if (!iterator.hasNext())
|
||||
return emptyList()
|
||||
val first = iterator.next()
|
||||
if (!iterator.hasNext())
|
||||
return listOf(first.toPair())
|
||||
val result = ArrayList<Pair<K, V>>(size)
|
||||
for (item in this)
|
||||
result.add(item.key to item.value)
|
||||
result.add(first.toPair())
|
||||
do {
|
||||
result.add(iterator.next().toPair())
|
||||
} while (iterator.hasNext())
|
||||
return result
|
||||
"""
|
||||
}
|
||||
@@ -102,7 +133,28 @@ fun snapshots(): List<GenericFunction> {
|
||||
include(CharSequences)
|
||||
doc { f -> "Returns a [List] containing all ${f.element.pluralize()}." }
|
||||
returns("List<T>")
|
||||
body { "return this.toMutableList()" }
|
||||
body { "return this.toMutableList().optimizeReadOnlyList()" }
|
||||
body(Iterables) {
|
||||
"""
|
||||
if (this is Collection) {
|
||||
return when (size) {
|
||||
0 -> emptyList()
|
||||
1 -> listOf(if (this is List) get(0) else iterator().next())
|
||||
else -> this.toMutableList()
|
||||
}
|
||||
}
|
||||
return this.toMutableList().optimizeReadOnlyList()
|
||||
"""
|
||||
}
|
||||
body(CharSequences, ArraysOfPrimitives, ArraysOfObjects) { f ->
|
||||
"""
|
||||
return when (${ if (f == CharSequences) "length" else "size" }) {
|
||||
0 -> emptyList()
|
||||
1 -> listOf(this[0])
|
||||
else -> this.toMutableList()
|
||||
}
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
templates add f("associate(transform: (T) -> Pair<K, V>)") {
|
||||
|
||||
Reference in New Issue
Block a user