website/versioned_docs/version-2.0.0-alpha.1/rules/performance.md
The performance rule set analyzes code for potential performance problems.
Using Array<Primitive> leads to implicit boxing and performance hit. Prefer using Kotlin specialized Array
Instances.
As stated in the Kotlin documentation Kotlin has
specialized arrays to represent primitive types without boxing overhead, such as IntArray, ByteArray and so on.
Active by default: Yes - Since v1.2.0
Requires Type Resolution
fun function(array: Array<Int>) { }
fun returningFunction(): Array<Double> { }
fun function(array: IntArray) { }
fun returningFunction(): DoubleArray { }
Long chains of collection operations will have a performance penalty due to a new list being created for each call. Consider using sequences instead. Read more about this in the documentation
Active by default: No
Requires Type Resolution
allowedOperations (default: 2)
The maximum number of allowed chained collection operations.
listOf(1, 2, 3, 4).map { it*2 }.filter { it < 4 }.map { it*it }
listOf(1, 2, 3, 4).asSequence().map { it*2 }.filter { it < 4 }.map { it*it }.toList()
listOf(1, 2, 3, 4).map { it*2 }
Using the forEach method on ranges has a heavy performance cost. Prefer using simple for loops.
Benchmarks have shown that using forEach on a range can have a huge performance cost in comparison to simple for loops. Hence, in most contexts, a simple for loop should be used instead. See more details here: Exploring Kotlin Hidden Costs - Part 1 Exploring Kotlin Hidden Costs - Part 2 Exploring Kotlin Hidden Costs - Part 3
To solve this code smell, the forEach usage should be replaced by a for loop.
Active by default: Yes - Since v1.0.0
(1..10).forEach {
println(it)
}
(1 until 10).forEach {
println(it)
}
(10 downTo 1).forEach {
println(it)
}
for (i in 1..10) {
println(i)
}
In most cases using a spread operator causes a full copy of the array to be created before calling a method. This has a very high performance penalty. Benchmarks showing this performance penalty can be seen here: Exploring Kotlin Hidden Costs - Part 1 Exploring Kotlin Hidden Costs - Part 2 Exploring Kotlin Hidden Costs - Part 3
The Kotlin compiler since v1.1.60 has an optimization that skips the array copy when an array constructor function is used to create the arguments that are passed to the vararg parameter. This case will not be flagged by the rule since it doesn't suffer the performance penalty of an array copy.
Active by default: Yes - Since v1.0.0
Requires Type Resolution
val strs = arrayOf("value one", "value two")
val foo = bar(*strs)
fun bar(vararg strs: String) {
strs.forEach { println(it) }
}
// array copy skipped in this case since Kotlin 1.1.60
val foo = bar(*arrayOf("value one", "value two"))
// array not passed so no array copy is required
val foo2 = bar("value one", "value two")
fun bar(vararg strs: String) {
strs.forEach { println(it) }
}
This rule applies to unnecessary binary expressions, including those in if and when conditions, as well as all predicates.
Binary expressions with || and && operator are checked.
Active by default: No
val foo = true
val bar = true
if (foo || bar || foo) {
}
val foo = true
if (foo) {
}
Avoid temporary objects when converting primitive types to String. This has a performance penalty when compared to using primitive types directly. To solve this issue, remove the wrapping type.
Active by default: Yes - Since v1.0.0
val i = Integer(1).toString() // temporary Integer instantiation just for the conversion
val i = Integer.toString(1)
Reports cast of unnecessary type casting. Cases like this can be replaced with type checking for performance reasons.
Active by default: No
fun foo() {
val objList: List<Any> = emptyList()
objList.any { it as? String != null }
}
fun foo() {
val objList: List<Any> = emptyList()
objList.any { it is String }
}