RFR 8170348: Appendable.appendN(char, int) method to append multiple copies of char
Ivan Gerasimov
ivan.gerasimov at oracle.com
Sun Dec 4 12:07:17 UTC 2016
Hello!
There are several places in JDK where the same character is appended to
a StringBuilder object multiple times (usually padding).
With each append there are a few routine checks performed.
They could have been done only once, if we had a method for appending
multiple copies at a time.
A simple benchmark shows that such method may save us a few machine
cycles (see the results below).
In the benchmark, three approaches were compared:
0) Using the new appendN(char, int) method to append several chars at once,
1) Calling append(char) in a loop,
2) Appending a prepared-in-advance string
On my machine, the new method demonstrates better or comparable
performance for all sizes up to 20.
In the webrev, there are two changesets included:
- the new default Appendable.appendN(char, int) method, its overrides in
StringBuilder/Buffer and a basic test,
- several applications of the new method across JDK.
Would you please help review?
Comments, suggestions are welcome.
BUGURL: https://bugs.openjdk.java.net/browse/JDK-8170348
WEBREV: http://cr.openjdk.java.net/~igerasim/8170348/00/webrev/
Benchmark: http://cr.openjdk.java.net/~igerasim/8170348/00/MyBenchmark.java
Benchmark (size) Mode Cnt Score Error Units
MyBenchmark.test_0_New 0 thrpt 70 331922128.215 ±
16399254.452 ops/s
MyBenchmark.test_0_New 1 thrpt 70 209207932.893 ±
14955800.231 ops/s
MyBenchmark.test_0_New 5 thrpt 70 72926671.621 ±
4841791.555 ops/s
MyBenchmark.test_0_New 10 thrpt 70 67779575.053 ±
3234366.239 ops/s
MyBenchmark.test_0_New 20 thrpt 70 59731629.661 ±
2769497.288 ops/s
MyBenchmark.test_1_Old 0 thrpt 70 333467628.860 ±
15981678.430 ops/s
MyBenchmark.test_1_Old 1 thrpt 70 156126381.967 ±
9619653.294 ops/s
MyBenchmark.test_1_Old 5 thrpt 70 46550204.382 ±
2009987.637 ops/s
MyBenchmark.test_1_Old 10 thrpt 70 23309297.849 ±
1268874.282 ops/s
MyBenchmark.test_1_Old 20 thrpt 70 13143637.821 ±
662265.103 ops/s
MyBenchmark.test_2_Solid 0 thrpt 70 138548108.540 ±
6408775.462 ops/s
MyBenchmark.test_2_Solid 1 thrpt 70 63890936.132 ±
3918274.970 ops/s
MyBenchmark.test_2_Solid 5 thrpt 70 65838879.075 ±
2701493.698 ops/s
MyBenchmark.test_2_Solid 10 thrpt 70 65387238.993 ±
3131562.548 ops/s
MyBenchmark.test_2_Solid 20 thrpt 70 57528150.828 ±
3171453.716 ops/s
With kind regards,
Ivan
More information about the core-libs-dev
mailing list