# String construction performance

**URL:** <https://discuss.kotlinlang.org/t/string-construction-performance/3281>\
**Category:** Uncategorized\
**Created:** [June 13, 2017, 9:22am UTC](https://discuss.kotlinlang.org/t/string-construction-performance/3281 "2017-06-13T09:22:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![imanushin](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/imanushin/32/5471_2.png) [@imanushin](https://discuss.kotlinlang.org/u/imanushin)\
**Post date:** [June 13, 2017, 9:22am UTC](https://discuss.kotlinlang.org/t/string-construction-performance/3281/1 "2017-06-13T09:22:50Z")

</div>

Hi all !

I read one interesting article for String.concat for Oracle Java. Please[see here](https://habrahabr.ru/post/330220/) (it is written on Russian, sorry); [the same question on StackOverflow](https://stackoverflow.com/questions/44334233/why-is-the-stringbuilder-chaining-pattern-sb-appendx-appendy-faster-than-reg).

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

---

<div class="post-metadata">

**Author:** ![fab1an](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fab1an/32/1934_2.png) [@fab1an](https://discuss.kotlinlang.org/u/fab1an)\
**Post date:** [June 18, 2017, 4:09pm UTC](https://discuss.kotlinlang.org/t/string-construction-performance/3281/2 "2017-06-18T16:09:17Z")

</div>

Nice tip, have you reported in on YouTrack?

---

<div class="post-metadata">

**Author:** ![imanushin](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/imanushin/32/5471_2.png) [@imanushin](https://discuss.kotlinlang.org/u/imanushin)\
**Post date:** [June 18, 2017, 5:22pm UTC](https://discuss.kotlinlang.org/t/string-construction-performance/3281/3 "2017-06-18T17:22:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Dmitry\_Petrov\_1](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dmitry_petrov_1/32/1806_2.png) [@Dmitry\_Petrov\_1](https://discuss.kotlinlang.org/u/Dmitry_Petrov_1)\
**Post date:** [June 20, 2017, 7:39am UTC](https://discuss.kotlinlang.org/t/string-construction-performance/3281/4 "2017-06-20T07:39:22Z")

</div>

Thanks for the heads up!  
BTW, here’s the original blog post (in English): [StringBuffer and StringBuilder performance with JMH - AlBlue’s Blog](http://alblue.bandlem.com/2016/04/jmh-stringbuffer-stringbuilder.html)

> [@imanushin](#):
>
> Does Kotlin have any tuning for this for inline string format (e.g. for “test, text, $myVariable, next text”)?

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

> [@imanushin](#):
>
> 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)?

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

---

<div class="post-metadata">

**Author:** ![Dmitry\_Petrov\_1](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dmitry_petrov_1/32/1806_2.png) [@Dmitry\_Petrov\_1](https://discuss.kotlinlang.org/u/Dmitry_Petrov_1)\
**Post date:** [June 20, 2017, 3:11pm UTC](https://discuss.kotlinlang.org/t/string-construction-performance/3281/5 "2017-06-20T15:11:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ov7a](https://avatars.discourse-cdn.com/v4/letter/o/ecae2f/32.png) [@ov7a](https://discuss.kotlinlang.org/u/ov7a)\
**Post date:** [June 21, 2017, 3:05pm UTC](https://discuss.kotlinlang.org/t/string-construction-performance/3281/6 "2017-06-21T15:05:40Z")

</div>

Will methods be chained in `apply` body?

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

```

---

<div class="post-metadata">

**Author:** ![elizarov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/elizarov/32/2164_2.png) [@elizarov](https://discuss.kotlinlang.org/u/elizarov)\
**Post date:** [June 21, 2017, 3:38pm UTC](https://discuss.kotlinlang.org/t/string-construction-performance/3281/7 "2017-06-21T15:38:17Z")

</div>

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