String construction performance

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)?
1 Like

Nice tip, have you reported in on YouTrack?

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

Thanks for the heads up!
BTW, here’s the original blog post (in English): StringBuffer and StringBuilder performance with JMH - AlBlue’s Blog

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

1 Like

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

1 Like

Will methods be chained in apply body?

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

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