RFR: 8265237: String.join and StringJoiner can be improved further
plevart at openjdk.java.net
Wed Apr 14 19:45:40 UTC 2021
On Wed, 14 Apr 2021 18:58:57 GMT, Peter Levart <plevart at openjdk.org> wrote:
> While JDK-8148937 improved StringJoiner class by replacing internal use of getChars that copies out characters from String elements into a char array with StringBuilder which is somehow more optimal, the improvement was marginal in speed (0% ... 10%) and mainly for smaller strings, while GC was reduced by about 50% in average per operation.
> Initial attempt to tackle that issue was more involved, but was later discarded because it was apparently using too much internal String details in code that lives outside String and outside java.lang package.
> But there is another way to package such "intimate" code - we can put it into String itself and just call it from StringJoiner.
> This PR is an attempt at doing just that. It introduces new package-private method in `java.lang.String` which is then used from both pubic static `String.join` methods as well as from `java.util.StringJoiner` (via SharedSecrets). The improvements can be seen by running the following JMH benchmark:
> The comparative results are here:
> The jmh-result.json files are here:
> Improvement in speed ranges from 8% (for small strings) to 200% (for long strings), while creation of garbage has been further reduced to an almost garbage-free operation.
> So WDYT?
Some background: This change was motivated by Sergey Tsypanov's attempt to replace usage of StringBuilder with string concatenation in JDK-8265075 only to find out that string concatenation in java.base module is compiled down to inline usage of StringBuilder, so no improvement was possible.
StringJoiner API and String.join static utility methods lend itself to a better implementation, but in last incarnation they are implemented with StringBuilder internally: String.join -> StringJoiner -> StringBuilder. A lot of JDK internal usages of StringBuilder were already replaced with StringJoiner (JDK-8054714) but under the hood the StringBuilder is still used. There were also lots of incremental attempts to improve StringJoiner. I think this one is the last one for some time to come... :-)
More information about the core-libs-dev