RFR: 8261847: performace of java.lang.Record::toString should be improved

PROgrm_JARvis duke at openjdk.java.net
Wed Nov 17 14:48:37 UTC 2021


On Tue, 16 Nov 2021 05:24:38 GMT, Vicente Romero <vromero at openjdk.org> wrote:

> Please review this PR which aims to optimize the implementation of the `toString` method we provide for records. A benchmark comparing the implementation we are providing for records with lombok found out that lombok is much faster mainly because our implementation uses `String::format`. This fix is basically delegating on StringConcatFactory::makeConcatWithConstants which is faster.
> 
> TIA
> 
> This is the result of the benchmark comparing records to lombok with vanilla JDK:
> 
> Benchmark                          Mode  Cnt    Score    Error  Units
> MyBenchmark.base                   avgt    4    0.849 ±  0.111  ns/op
> MyBenchmark.equals_record          avgt    4    7.343 ±  2.740  ns/op
> MyBenchmark.equals_value           avgt    4    6.644 ±  1.920  ns/op
> MyBenchmark.record_hash_code       avgt    4    5.763 ±  3.882  ns/op
> MyBenchmark.record_to_string       avgt    4  262.626 ± 12.574  ns/op               <------ Before
> MyBenchmark.value_class_to_string  avgt    4   30.325 ± 21.389  ns/op
> MyBenchmark.value_hash_code        avgt    4    5.048 ±  3.936  ns/op
> 
> 
> after this patch:
> 
> Benchmark                          Mode  Cnt   Score   Error  Units
> MyBenchmark.base                   avgt    4   0.680 ± 0.185  ns/op
> MyBenchmark.equals_record          avgt    4   5.599 ± 1.348  ns/op
> MyBenchmark.equals_value           avgt    4   5.718 ± 4.633  ns/op
> MyBenchmark.record_hash_code       avgt    4   4.628 ± 4.368  ns/op
> MyBenchmark.record_to_string       avgt    4  26.791 ± 1.817  ns/op                 <------- After
> MyBenchmark.value_class_to_string  avgt    4  35.473 ± 2.626  ns/op
> MyBenchmark.value_hash_code        avgt    4   6.152 ± 5.101  ns/op

By the way, considering the fact that the limit of `StringConcatFactory` (i.e. `200` arguments) is explicitly specified (at least, it is mentioned in its Javadoc) can we request to have it available to code (e.g. as a `public static final` constant) so that other code in JDK (like this PR) and foreign code can rely on it?

For example, I have to duplicate this value based on Javadoc [here](https://github.com/JarvisCraft/padla/blob/118c89e078cfc8e54faf154f2f4e658604159ab0/ultimate-messenger/src/main/java/ru/progrm_jarvis/ultimatemessenger/format/model/AsmTextModelFactory.java#L366) while there could have been something similar to `StringConcatFactory#MAX_ARGUMENTS`.

-------------

PR: https://git.openjdk.java.net/jdk/pull/6403


More information about the core-libs-dev mailing list