Optimize operations to return special collection implementations when result is empty or has single element.

#KT-9990
This commit is contained in:
Ilya Gorbunov
2016-04-22 20:51:44 +03:00
parent 2d12ed68c8
commit a665b2183c
4 changed files with 55 additions and 55 deletions
@@ -56,7 +56,7 @@ fun filtering(): List<GenericFunction> {
for (item in listIterator(n))
list.add(item)
}
return list
return list.optimizeReadOnlyList()
}
}
else {
@@ -66,7 +66,7 @@ fun filtering(): List<GenericFunction> {
for (item in this) {
if (count++ >= n) list.add(item)
}
return list
return list.optimizeReadOnlyList()
"""
}
@@ -105,7 +105,7 @@ fun filtering(): List<GenericFunction> {
for (index in n..size - 1) {
list.add(this[index])
}
return list
return list.optimizeReadOnlyList()
"""
}
}
@@ -126,7 +126,7 @@ fun filtering(): List<GenericFunction> {
break
list.add(item)
}
return list
return list.optimizeReadOnlyList()
"""
}
@@ -165,7 +165,7 @@ fun filtering(): List<GenericFunction> {
break;
list.add(item)
}
return list
return list.optimizeReadOnlyList()
"""
}
}
@@ -215,7 +215,7 @@ fun filtering(): List<GenericFunction> {
val list = ArrayList<T>(n)
for (index in size - n .. size - 1)
list.add(this[index])
return list
return list.optimizeReadOnlyList()
"""
}
body(Lists) {
@@ -232,7 +232,7 @@ fun filtering(): List<GenericFunction> {
for (item in listIterator(n))
list.add(item)
}
return list
return list.optimizeReadOnlyList()
"""
}
}
@@ -663,7 +663,7 @@ fun filtering(): List<GenericFunction> {
body {
"""
val size = indices.collectionSizeOrDefault(10)
if (size == 0) return listOf()
if (size == 0) return emptyList()
val list = ArrayList<T>(size)
for (index in indices) {
list.add(get(index))
@@ -31,7 +31,7 @@ fun ordering(): List<GenericFunction> {
returns { "List<T>" }
body {
"""
if (this is Collection && isEmpty()) return emptyList()
if (this is Collection && size <= 1) return toList()
val list = toMutableList()
Collections.reverse(list)
return list
@@ -101,7 +101,7 @@ fun ordering(): List<GenericFunction> {
body {
"""
if (this is Collection) {
if (size <= 1) return this.toMutableList()
if (size <= 1) return this.toList()
@Suppress("CAST_NEVER_SUCCEEDS")
return (toTypedArray<Comparable<T>>() as Array<T>).apply { sort() }.asList()
}
@@ -231,7 +231,7 @@ fun ordering(): List<GenericFunction> {
body {
"""
if (this is Collection) {
if (size <= 1) return this.toMutableList()
if (size <= 1) return this.toList()
@Suppress("CAST_NEVER_SUCCEEDS")
return (toTypedArray<Any?>() as Array<T>).apply { sortWith(comparator) }.asList()
}