You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In several place of modules, the concatenation of string can decrease the performance.
Currently, the concatenation is make by the operator +=. This is not a problem for the inline concatenation "a" + "b" + "c",
but when it's not, the compiler creates several StringBuilder instance.
So, below, the explanation sent in discord server :
When you add two string, this is the compiled result :
val a ="a"val b ="b"val c ="c"println(a + b + c) // StringBuilder(a).append(b).append(c).toString()
So, here you can see, there is no issue about the performance, because an inline concatenation of string create an once StringBuilder
But, when it's not inline, for each concatenation, a new StringBuilder will be create
val a ="a"
a +="b"// a = StringBuilder(a).append("b").toString()
a +="c"// a = StringBuilder(a).append("c").toString()println(a)
Or if there is loop, condition, it's the same case
var a ="a"if(Random.nextInt() ==2) {
a +="b"// a = StringBuilder(a).append("b").toString()
}
a +="c"// a = StringBuilder(a).append("c").toString()listOf(1, 2 ,3).forEach {
a += it.toString() // a = StringBuilder(a).append(it).toString() (x3)
}
println(a)
In the code i seen this for example
list.joinToString { // StringBuilder create for the resultvar x ="..;"if(..) {
x +="..."// new StringBuilder created
}
// etc.
x
}
A joinToString use an instance of StringBuilder, to concat without performance issue the result of each iteration
BUT, if you provoke the creation of a new StringBuilder in the iteration, so you loss all advantage of joinToString and StringBuilder
Code location
I post here some (not all) part of code concerned by this issue
Description
In several place of modules, the concatenation of string can decrease the performance.
Currently, the concatenation is make by the operator
+=
. This is not a problem for the inline concatenation"a" + "b" + "c"
,but when it's not, the compiler creates several StringBuilder instance.
So, below, the explanation sent in discord server :
When you add two string, this is the compiled result :
So, here you can see, there is no issue about the performance, because an inline concatenation of string create an once StringBuilder
But, when it's not inline, for each concatenation, a new StringBuilder will be create
Or if there is loop, condition, it's the same case
In the code i seen this for example
A joinToString use an instance of StringBuilder, to concat without performance issue the result of each iteration
BUT, if you provoke the creation of a new StringBuilder in the iteration, so you loss all advantage of joinToString and StringBuilder
Code location
I post here some (not all) part of code concerned by this issue
annotation-processor-1
annotation-processor-2
extra-modules
kord-extension-1
kord-extension-2
etc.
To find all, i advice to use the option in IntelliJ
Find in Files
with the value+= "
Solution
There is two choices to resolve that.
buildString { }
The method buildString allows to have a StringBuilder as a receiver variable
Same than first solution, but you need to write the variable
For the list and the method
joinToString
, if you concat in the joinToString, prefer use a StringBuilder and aforEach
insteadThe text was updated successfully, but these errors were encountered: