[PATCH] trivial improvement of String.replace(CharSequence cs1, CharSequence cs2) for the case cs1 is empty

Сергей Цыпанов sergei.tsypanov at yandex.ru
Mon Jul 20 07:39:37 UTC 2020


Hello,

I'd like to contribute this simple patch that improves performance of
 String.replace(CharSequence cs1, CharSequence cs2) for the case cs1 is empty.

The idea is to get rid of StringBuilder in favour of byte[] / char[].

To measure improvement I've used benchmark [1] that demonstrates improvement as of both memory and time consumption
for the case when I have String s = "aaaa" and I need to surround each char 'a' with 'b'. For this I call

String str = s.replace("", "b");

See the results for original JDK and patched one:

before
                                                        (latin)  (length)   Mode  Cnt      Score     Error   Units
insertBetweenEachChar                                      true         5  thrpt   50  16992.105 ± 528.388  ops/ms
insertBetweenEachChar                                      true        10  thrpt   50   9119.878 ± 421.776  ops/ms
insertBetweenEachChar                                      true        50  thrpt   50   2270.251 ±  45.815  ops/ms
insertBetweenEachChar                                     false         5  thrpt   50  11468.241 ± 623.440  ops/ms
insertBetweenEachChar                                     false        10  thrpt   50   7150.812 ± 299.363  ops/ms
insertBetweenEachChar                                     false        50  thrpt   50   1613.470 ±  66.941  ops/ms

insertBetweenEachChar:·gc.alloc.rate.norm                  true         5  thrpt   50     88.010 ±   0.001    B/op
insertBetweenEachChar:·gc.alloc.rate.norm                  true        10  thrpt   50    104.015 ±   0.001    B/op
insertBetweenEachChar:·gc.alloc.rate.norm                  true        50  thrpt   50    264.046 ±   0.002    B/op
insertBetweenEachChar:·gc.alloc.rate.norm                 false         5  thrpt   50    177.618 ±   5.880    B/op
insertBetweenEachChar:·gc.alloc.rate.norm                 false        10  thrpt   50    251.229 ±   4.801    B/op
insertBetweenEachChar:·gc.alloc.rate.norm                 false        50  thrpt   50    736.091 ±   0.005    B/op

after

insertBetweenEachChar                                      true         5  thrpt   50  19249.086 ± 325.786  ops/ms
insertBetweenEachChar                                      true        10  thrpt   50  12962.483 ±  61.992  ops/ms
insertBetweenEachChar                                      true        50  thrpt   50   3232.475 ±  16.864  ops/ms
insertBetweenEachChar                                     false         5  thrpt   50  26083.880 ± 218.046  ops/ms
insertBetweenEachChar                                     false        10  thrpt   50  18305.650 ± 180.684  ops/ms
insertBetweenEachChar                                     false        50  thrpt   50   5712.175 ±  37.051  ops/ms

insertBetweenEachChar:·gc.alloc.rate.norm                  true         5  thrpt   50     88.010 ±   0.001    B/op
insertBetweenEachChar:·gc.alloc.rate.norm                  true        10  thrpt   50    104.012 ±   0.001    B/op
insertBetweenEachChar:·gc.alloc.rate.norm                  true        50  thrpt   50    264.038 ±   0.002    B/op
insertBetweenEachChar:·gc.alloc.rate.norm                 false         5  thrpt   50    136.012 ±   0.001    B/op
insertBetweenEachChar:·gc.alloc.rate.norm                 false        10  thrpt   50    192.017 ±   0.001    B/op
insertBetweenEachChar:·gc.alloc.rate.norm                 false        50  thrpt   50    592.053 ±   0.001    B/op

Full output can be found in [2].

Patch is attached.

With best regards,
Sergey Tsypanov

[1] https://github.com/stsypanov/strings/blob/master/src/jmh/java/tsypanov/strings/string/InsertCharBenchmark.java
[2] https://github.com/stsypanov/strings/blob/master/results/InsertCharBenchmark.txt
-------------- next part --------------
A non-text attachment was scrubbed...
Name: string.patch
Type: text/x-diff
Size: 1625 bytes
Desc: not available
URL: <https://mail.openjdk.java.net/pipermail/core-libs-dev/attachments/20200720/bc8d8f27/string.patch>


More information about the core-libs-dev mailing list