From a665b2183c3bb3d5f043df3f7fb9fffc9ead53e8 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Fri, 22 Apr 2016 20:51:44 +0300 Subject: [PATCH] Optimize operations to return special collection implementations when result is empty or has single element. #KT-9990 --- libraries/stdlib/src/generated/_Arrays.kt | 72 +++++++++---------- .../stdlib/src/generated/_Collections.kt | 16 ++--- .../src/templates/Filtering.kt | 16 ++--- .../src/templates/Ordering.kt | 6 +- 4 files changed, 55 insertions(+), 55 deletions(-) diff --git a/libraries/stdlib/src/generated/_Arrays.kt b/libraries/stdlib/src/generated/_Arrays.kt index a3c5c08c72a..ef31447cffc 100644 --- a/libraries/stdlib/src/generated/_Arrays.kt +++ b/libraries/stdlib/src/generated/_Arrays.kt @@ -2526,7 +2526,7 @@ public fun Array.drop(n: Int): List { for (index in n..size - 1) { list.add(this[index]) } - return list + return list.optimizeReadOnlyList() } /** @@ -2542,7 +2542,7 @@ public fun ByteArray.drop(n: Int): List { for (index in n..size - 1) { list.add(this[index]) } - return list + return list.optimizeReadOnlyList() } /** @@ -2558,7 +2558,7 @@ public fun ShortArray.drop(n: Int): List { for (index in n..size - 1) { list.add(this[index]) } - return list + return list.optimizeReadOnlyList() } /** @@ -2574,7 +2574,7 @@ public fun IntArray.drop(n: Int): List { for (index in n..size - 1) { list.add(this[index]) } - return list + return list.optimizeReadOnlyList() } /** @@ -2590,7 +2590,7 @@ public fun LongArray.drop(n: Int): List { for (index in n..size - 1) { list.add(this[index]) } - return list + return list.optimizeReadOnlyList() } /** @@ -2606,7 +2606,7 @@ public fun FloatArray.drop(n: Int): List { for (index in n..size - 1) { list.add(this[index]) } - return list + return list.optimizeReadOnlyList() } /** @@ -2622,7 +2622,7 @@ public fun DoubleArray.drop(n: Int): List { for (index in n..size - 1) { list.add(this[index]) } - return list + return list.optimizeReadOnlyList() } /** @@ -2638,7 +2638,7 @@ public fun BooleanArray.drop(n: Int): List { for (index in n..size - 1) { list.add(this[index]) } - return list + return list.optimizeReadOnlyList() } /** @@ -2654,7 +2654,7 @@ public fun CharArray.drop(n: Int): List { for (index in n..size - 1) { list.add(this[index]) } - return list + return list.optimizeReadOnlyList() } /** @@ -3547,7 +3547,7 @@ public fun CharArray.slice(indices: IntRange): List { */ public fun Array.slice(indices: Iterable): List { val size = indices.collectionSizeOrDefault(10) - if (size == 0) return listOf() + if (size == 0) return emptyList() val list = ArrayList(size) for (index in indices) { list.add(get(index)) @@ -3560,7 +3560,7 @@ public fun Array.slice(indices: Iterable): List { */ public fun ByteArray.slice(indices: Iterable): List { val size = indices.collectionSizeOrDefault(10) - if (size == 0) return listOf() + if (size == 0) return emptyList() val list = ArrayList(size) for (index in indices) { list.add(get(index)) @@ -3573,7 +3573,7 @@ public fun ByteArray.slice(indices: Iterable): List { */ public fun ShortArray.slice(indices: Iterable): List { val size = indices.collectionSizeOrDefault(10) - if (size == 0) return listOf() + if (size == 0) return emptyList() val list = ArrayList(size) for (index in indices) { list.add(get(index)) @@ -3586,7 +3586,7 @@ public fun ShortArray.slice(indices: Iterable): List { */ public fun IntArray.slice(indices: Iterable): List { val size = indices.collectionSizeOrDefault(10) - if (size == 0) return listOf() + if (size == 0) return emptyList() val list = ArrayList(size) for (index in indices) { list.add(get(index)) @@ -3599,7 +3599,7 @@ public fun IntArray.slice(indices: Iterable): List { */ public fun LongArray.slice(indices: Iterable): List { val size = indices.collectionSizeOrDefault(10) - if (size == 0) return listOf() + if (size == 0) return emptyList() val list = ArrayList(size) for (index in indices) { list.add(get(index)) @@ -3612,7 +3612,7 @@ public fun LongArray.slice(indices: Iterable): List { */ public fun FloatArray.slice(indices: Iterable): List { val size = indices.collectionSizeOrDefault(10) - if (size == 0) return listOf() + if (size == 0) return emptyList() val list = ArrayList(size) for (index in indices) { list.add(get(index)) @@ -3625,7 +3625,7 @@ public fun FloatArray.slice(indices: Iterable): List { */ public fun DoubleArray.slice(indices: Iterable): List { val size = indices.collectionSizeOrDefault(10) - if (size == 0) return listOf() + if (size == 0) return emptyList() val list = ArrayList(size) for (index in indices) { list.add(get(index)) @@ -3638,7 +3638,7 @@ public fun DoubleArray.slice(indices: Iterable): List { */ public fun BooleanArray.slice(indices: Iterable): List { val size = indices.collectionSizeOrDefault(10) - if (size == 0) return listOf() + if (size == 0) return emptyList() val list = ArrayList(size) for (index in indices) { list.add(get(index)) @@ -3651,7 +3651,7 @@ public fun BooleanArray.slice(indices: Iterable): List { */ public fun CharArray.slice(indices: Iterable): List { val size = indices.collectionSizeOrDefault(10) - if (size == 0) return listOf() + if (size == 0) return emptyList() val list = ArrayList(size) for (index in indices) { list.add(get(index)) @@ -3853,7 +3853,7 @@ public fun Array.take(n: Int): List { break list.add(item) } - return list + return list.optimizeReadOnlyList() } /** @@ -3870,7 +3870,7 @@ public fun ByteArray.take(n: Int): List { break list.add(item) } - return list + return list.optimizeReadOnlyList() } /** @@ -3887,7 +3887,7 @@ public fun ShortArray.take(n: Int): List { break list.add(item) } - return list + return list.optimizeReadOnlyList() } /** @@ -3904,7 +3904,7 @@ public fun IntArray.take(n: Int): List { break list.add(item) } - return list + return list.optimizeReadOnlyList() } /** @@ -3921,7 +3921,7 @@ public fun LongArray.take(n: Int): List { break list.add(item) } - return list + return list.optimizeReadOnlyList() } /** @@ -3938,7 +3938,7 @@ public fun FloatArray.take(n: Int): List { break list.add(item) } - return list + return list.optimizeReadOnlyList() } /** @@ -3955,7 +3955,7 @@ public fun DoubleArray.take(n: Int): List { break list.add(item) } - return list + return list.optimizeReadOnlyList() } /** @@ -3972,7 +3972,7 @@ public fun BooleanArray.take(n: Int): List { break list.add(item) } - return list + return list.optimizeReadOnlyList() } /** @@ -3989,7 +3989,7 @@ public fun CharArray.take(n: Int): List { break list.add(item) } - return list + return list.optimizeReadOnlyList() } /** @@ -4003,7 +4003,7 @@ public fun Array.takeLast(n: Int): List { val list = ArrayList(n) for (index in size - n .. size - 1) list.add(this[index]) - return list + return list.optimizeReadOnlyList() } /** @@ -4017,7 +4017,7 @@ public fun ByteArray.takeLast(n: Int): List { val list = ArrayList(n) for (index in size - n .. size - 1) list.add(this[index]) - return list + return list.optimizeReadOnlyList() } /** @@ -4031,7 +4031,7 @@ public fun ShortArray.takeLast(n: Int): List { val list = ArrayList(n) for (index in size - n .. size - 1) list.add(this[index]) - return list + return list.optimizeReadOnlyList() } /** @@ -4045,7 +4045,7 @@ public fun IntArray.takeLast(n: Int): List { val list = ArrayList(n) for (index in size - n .. size - 1) list.add(this[index]) - return list + return list.optimizeReadOnlyList() } /** @@ -4059,7 +4059,7 @@ public fun LongArray.takeLast(n: Int): List { val list = ArrayList(n) for (index in size - n .. size - 1) list.add(this[index]) - return list + return list.optimizeReadOnlyList() } /** @@ -4073,7 +4073,7 @@ public fun FloatArray.takeLast(n: Int): List { val list = ArrayList(n) for (index in size - n .. size - 1) list.add(this[index]) - return list + return list.optimizeReadOnlyList() } /** @@ -4087,7 +4087,7 @@ public fun DoubleArray.takeLast(n: Int): List { val list = ArrayList(n) for (index in size - n .. size - 1) list.add(this[index]) - return list + return list.optimizeReadOnlyList() } /** @@ -4101,7 +4101,7 @@ public fun BooleanArray.takeLast(n: Int): List { val list = ArrayList(n) for (index in size - n .. size - 1) list.add(this[index]) - return list + return list.optimizeReadOnlyList() } /** @@ -4115,7 +4115,7 @@ public fun CharArray.takeLast(n: Int): List { val list = ArrayList(n) for (index in size - n .. size - 1) list.add(this[index]) - return list + return list.optimizeReadOnlyList() } /** diff --git a/libraries/stdlib/src/generated/_Collections.kt b/libraries/stdlib/src/generated/_Collections.kt index 50eedadf033..7563880ec4d 100644 --- a/libraries/stdlib/src/generated/_Collections.kt +++ b/libraries/stdlib/src/generated/_Collections.kt @@ -553,7 +553,7 @@ public fun Iterable.drop(n: Int): List { for (item in listIterator(n)) list.add(item) } - return list + return list.optimizeReadOnlyList() } } else { @@ -563,7 +563,7 @@ public fun Iterable.drop(n: Int): List { for (item in this) { if (count++ >= n) list.add(item) } - return list + return list.optimizeReadOnlyList() } /** @@ -699,7 +699,7 @@ public fun List.slice(indices: IntRange): List { */ public fun List.slice(indices: Iterable): List { val size = indices.collectionSizeOrDefault(10) - if (size == 0) return listOf() + if (size == 0) return emptyList() val list = ArrayList(size) for (index in indices) { list.add(get(index)) @@ -721,7 +721,7 @@ public fun Iterable.take(n: Int): List { break list.add(item) } - return list + return list.optimizeReadOnlyList() } /** @@ -740,7 +740,7 @@ public fun List.takeLast(n: Int): List { for (item in listIterator(n)) list.add(item) } - return list + return list.optimizeReadOnlyList() } /** @@ -788,7 +788,7 @@ public fun MutableList.reverse(): Unit { * Returns a list with elements in reversed order. */ public fun Iterable.reversed(): List { - if (this is Collection && isEmpty()) return emptyList() + if (this is Collection && size <= 1) return toList() val list = toMutableList() Collections.reverse(list) return list @@ -820,7 +820,7 @@ public fun > MutableList.sortDescending(): Unit { */ public fun > Iterable.sorted(): List { if (this is Collection) { - if (size <= 1) return this.toMutableList() + if (size <= 1) return this.toList() @Suppress("CAST_NEVER_SUCCEEDS") return (toTypedArray>() as Array).apply { sort() }.asList() } @@ -853,7 +853,7 @@ public fun > Iterable.sortedDescending(): List { */ public fun Iterable.sortedWith(comparator: Comparator): List { if (this is Collection) { - if (size <= 1) return this.toMutableList() + if (size <= 1) return this.toList() @Suppress("CAST_NEVER_SUCCEEDS") return (toTypedArray() as Array).apply { sortWith(comparator) }.asList() } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Filtering.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Filtering.kt index c8e0ce9e1a0..aa354e32013 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Filtering.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Filtering.kt @@ -56,7 +56,7 @@ fun filtering(): List { for (item in listIterator(n)) list.add(item) } - return list + return list.optimizeReadOnlyList() } } else { @@ -66,7 +66,7 @@ fun filtering(): List { for (item in this) { if (count++ >= n) list.add(item) } - return list + return list.optimizeReadOnlyList() """ } @@ -105,7 +105,7 @@ fun filtering(): List { for (index in n..size - 1) { list.add(this[index]) } - return list + return list.optimizeReadOnlyList() """ } } @@ -126,7 +126,7 @@ fun filtering(): List { break list.add(item) } - return list + return list.optimizeReadOnlyList() """ } @@ -165,7 +165,7 @@ fun filtering(): List { break; list.add(item) } - return list + return list.optimizeReadOnlyList() """ } } @@ -215,7 +215,7 @@ fun filtering(): List { val list = ArrayList(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 { for (item in listIterator(n)) list.add(item) } - return list + return list.optimizeReadOnlyList() """ } } @@ -663,7 +663,7 @@ fun filtering(): List { body { """ val size = indices.collectionSizeOrDefault(10) - if (size == 0) return listOf() + if (size == 0) return emptyList() val list = ArrayList(size) for (index in indices) { list.add(get(index)) diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt index e87b479d915..edee0847f21 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt @@ -31,7 +31,7 @@ fun ordering(): List { returns { "List" } 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 { body { """ if (this is Collection) { - if (size <= 1) return this.toMutableList() + if (size <= 1) return this.toList() @Suppress("CAST_NEVER_SUCCEEDS") return (toTypedArray>() as Array).apply { sort() }.asList() } @@ -231,7 +231,7 @@ fun ordering(): List { body { """ if (this is Collection) { - if (size <= 1) return this.toMutableList() + if (size <= 1) return this.toList() @Suppress("CAST_NEVER_SUCCEEDS") return (toTypedArray() as Array).apply { sortWith(comparator) }.asList() }