String construction performance


#1

Hi all !

I read one interesting article for String.concat for Oracle Java. Please see here (it is written on Russian, sorry); the same question on StackOverflow.

Short summary: chain append(x).append(y) of StringBuilder works faster than ordinal sb.append(x); sb.append(y) .

So, questions:

  • Does Kotlin have any tuning for this for inline string format (e.g. for “test, text, $myVariable, next text”)?
  • Does Kotlin have optinizations to convert developer’s code to JVM-friendly code (e.g. when dev. wrotes code in unoptimal way, Kotlin can do invisible speedup)?

#2

Nice tip, have you reported in on YouTrack?


#3

No, because this is just a question. I don’t know, how really it works. So, wating for JetBrains’s developers responce


#4

Thanks for the heads up!
BTW, here’s the original blog post (in English): http://alblue.bandlem.com/2016/04/jmh-stringbuffer-stringbuilder.html

We generate chained append calls for string templates ("blah ${expr} blah") and chained string concatenation (s1 + s2 + s3).

Currently - no, we don’t. I’ve created an issue in YT: https://youtrack.jetbrains.com/issue/KT-18558


#5

Added another issue for append call sequence to append call chain transformation:
https://youtrack.jetbrains.com/issue/KT-18572


#6

Will methods be chained in apply body?

sb.apply{
    append("ololo")
    append("alala")
    append("___")
}

#7

No. apply does not chain calls. They all go to sb.