Skip to content

Optimize take() for arrays #5424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 9 additions & 72 deletions libraries/stdlib/common/src/generated/_Arrays.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4806,14 +4806,7 @@ public fun <T> Array<out T>.take(n: Int): List<T> {
if (n == 0) return emptyList()
if (n >= size) return toList()
if (n == 1) return listOf(this[0])
var count = 0
val list = ArrayList<T>(n)
for (item in this) {
list.add(item)
if (++count == n)
break
}
return list
return copyOfRange(0, n).asList()
}

/**
Expand All @@ -4828,14 +4821,7 @@ public fun ByteArray.take(n: Int): List<Byte> {
if (n == 0) return emptyList()
if (n >= size) return toList()
if (n == 1) return listOf(this[0])
var count = 0
val list = ArrayList<Byte>(n)
for (item in this) {
list.add(item)
if (++count == n)
break
}
return list
return copyOfRange(0, n).asList()
}

/**
Expand All @@ -4850,14 +4836,7 @@ public fun ShortArray.take(n: Int): List<Short> {
if (n == 0) return emptyList()
if (n >= size) return toList()
if (n == 1) return listOf(this[0])
var count = 0
val list = ArrayList<Short>(n)
for (item in this) {
list.add(item)
if (++count == n)
break
}
return list
return copyOfRange(0, n).asList()
}

/**
Expand All @@ -4872,14 +4851,7 @@ public fun IntArray.take(n: Int): List<Int> {
if (n == 0) return emptyList()
if (n >= size) return toList()
if (n == 1) return listOf(this[0])
var count = 0
val list = ArrayList<Int>(n)
for (item in this) {
list.add(item)
if (++count == n)
break
}
return list
return copyOfRange(0, n).asList()
}

/**
Expand All @@ -4894,14 +4866,7 @@ public fun LongArray.take(n: Int): List<Long> {
if (n == 0) return emptyList()
if (n >= size) return toList()
if (n == 1) return listOf(this[0])
var count = 0
val list = ArrayList<Long>(n)
for (item in this) {
list.add(item)
if (++count == n)
break
}
return list
return copyOfRange(0, n).asList()
}

/**
Expand All @@ -4916,14 +4881,7 @@ public fun FloatArray.take(n: Int): List<Float> {
if (n == 0) return emptyList()
if (n >= size) return toList()
if (n == 1) return listOf(this[0])
var count = 0
val list = ArrayList<Float>(n)
for (item in this) {
list.add(item)
if (++count == n)
break
}
return list
return copyOfRange(0, n).asList()
}

/**
Expand All @@ -4938,14 +4896,7 @@ public fun DoubleArray.take(n: Int): List<Double> {
if (n == 0) return emptyList()
if (n >= size) return toList()
if (n == 1) return listOf(this[0])
var count = 0
val list = ArrayList<Double>(n)
for (item in this) {
list.add(item)
if (++count == n)
break
}
return list
return copyOfRange(0, n).asList()
}

/**
Expand All @@ -4960,14 +4911,7 @@ public fun BooleanArray.take(n: Int): List<Boolean> {
if (n == 0) return emptyList()
if (n >= size) return toList()
if (n == 1) return listOf(this[0])
var count = 0
val list = ArrayList<Boolean>(n)
for (item in this) {
list.add(item)
if (++count == n)
break
}
return list
return copyOfRange(0, n).asList()
}

/**
Expand All @@ -4982,14 +4926,7 @@ public fun CharArray.take(n: Int): List<Char> {
if (n == 0) return emptyList()
if (n >= size) return toList()
if (n == 1) return listOf(this[0])
var count = 0
val list = ArrayList<Char>(n)
for (item in this) {
list.add(item)
if (++count == n)
break
}
return list
return copyOfRange(0, n).asList()
}

/**
Expand Down
36 changes: 4 additions & 32 deletions libraries/stdlib/common/src/generated/_UArrays.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2385,14 +2385,7 @@ public fun UIntArray.take(n: Int): List<UInt> {
if (n == 0) return emptyList()
if (n >= size) return toList()
if (n == 1) return listOf(this[0])
var count = 0
val list = ArrayList<UInt>(n)
for (item in this) {
list.add(item)
if (++count == n)
break
}
return list
return copyOfRange(0, n).asList()
}

/**
Expand All @@ -2409,14 +2402,7 @@ public fun ULongArray.take(n: Int): List<ULong> {
if (n == 0) return emptyList()
if (n >= size) return toList()
if (n == 1) return listOf(this[0])
var count = 0
val list = ArrayList<ULong>(n)
for (item in this) {
list.add(item)
if (++count == n)
break
}
return list
return copyOfRange(0, n).asList()
}

/**
Expand All @@ -2433,14 +2419,7 @@ public fun UByteArray.take(n: Int): List<UByte> {
if (n == 0) return emptyList()
if (n >= size) return toList()
if (n == 1) return listOf(this[0])
var count = 0
val list = ArrayList<UByte>(n)
for (item in this) {
list.add(item)
if (++count == n)
break
}
return list
return copyOfRange(0, n).asList()
}

/**
Expand All @@ -2457,14 +2436,7 @@ public fun UShortArray.take(n: Int): List<UShort> {
if (n == 0) return emptyList()
if (n >= size) return toList()
if (n == 1) return listOf(this[0])
var count = 0
val list = ArrayList<UShort>(n)
for (item in this) {
list.add(item)
if (++count == n)
break
}
return list
return copyOfRange(0, n).asList()
}

/**
Expand Down
9 changes: 1 addition & 8 deletions libraries/tools/kotlin-stdlib-gen/src/templates/Filtering.kt
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,7 @@ object Filtering : TemplateGroupBase() {
if (n == 0) return emptyList()
if (n >= size) return toList()
if (n == 1) return listOf(this[0])
var count = 0
val list = ArrayList<T>(n)
for (item in this) {
list.add(item)
if (++count == n)
break
}
return list
return copyOfRange(0, n).asList()
"""
}
}
Expand Down