-
Enhancement
-
Resolution: Duplicate
-
P4
-
None
-
None
On behalf of Dan Lutker <lutkerd@amazon.com> :
The toString function for Optional, OptionalInt, OptionalDouble and OptionalLong currently uses String.format which, while flexible is expensive for simple string append operations. Moving to a StringBuilder or a Simple concatenation can have up to >300% improvement in the toString function (throughput/average time) benchmarks.
Benchmark Mode Cnt Score Error Units
MyBenchmark.testOptional avgt 5 1453612.889 ± 60152.444 ns/op
MyBenchmark.testOptionalBuilder avgt 5 323657.276 ± 17635.166 ns/op
MyBenchmark.testOptionalConcat avgt 5 434697.691 ± 39090.595 ns/op
StringBuilder improvement ~349%
MyBenchmark.testOptionalDouble avgt 5 4287429.497 ± 121221.752 ns/op
MyBenchmark.testOptionalDoubleBuilder avgt 5 2841457.749 ± 384329.293 ns/op
MyBenchmark.testOptionalDoubleConcat avgt 5 2853596.203 ± 440987.156 ns/op
StringBuilder improvement ~50%
MyBenchmark.testOptionalInt avgt 5 1964300.758 ± 293558.572 ns/op
MyBenchmark.testOptionalIntBuilder avgt 5 443464.023 ± 26243.199 ns/op
MyBenchmark.testOptionalIntConcat avgt 5 455936.131 ± 47804.736 ns/op
StringBuilder improvement ~340%
MyBenchmark.testOptionalLong avgt 5 1824866.104 ± 414522.256 ns/op
MyBenchmark.testOptionalLongBuilder avgt 5 947936.153 ± 241442.710 ns/op
MyBenchmark.testOptionalLongConcat avgt 5 759235.350 ± 142183.130 ns/op
StringBuilder improvement ~92%
The toString function for Optional, OptionalInt, OptionalDouble and OptionalLong currently uses String.format which, while flexible is expensive for simple string append operations. Moving to a StringBuilder or a Simple concatenation can have up to >300% improvement in the toString function (throughput/average time) benchmarks.
Benchmark Mode Cnt Score Error Units
MyBenchmark.testOptional avgt 5 1453612.889 ± 60152.444 ns/op
MyBenchmark.testOptionalBuilder avgt 5 323657.276 ± 17635.166 ns/op
MyBenchmark.testOptionalConcat avgt 5 434697.691 ± 39090.595 ns/op
StringBuilder improvement ~349%
MyBenchmark.testOptionalDouble avgt 5 4287429.497 ± 121221.752 ns/op
MyBenchmark.testOptionalDoubleBuilder avgt 5 2841457.749 ± 384329.293 ns/op
MyBenchmark.testOptionalDoubleConcat avgt 5 2853596.203 ± 440987.156 ns/op
StringBuilder improvement ~50%
MyBenchmark.testOptionalInt avgt 5 1964300.758 ± 293558.572 ns/op
MyBenchmark.testOptionalIntBuilder avgt 5 443464.023 ± 26243.199 ns/op
MyBenchmark.testOptionalIntConcat avgt 5 455936.131 ± 47804.736 ns/op
StringBuilder improvement ~340%
MyBenchmark.testOptionalLong avgt 5 1824866.104 ± 414522.256 ns/op
MyBenchmark.testOptionalLongBuilder avgt 5 947936.153 ± 241442.710 ns/op
MyBenchmark.testOptionalLongConcat avgt 5 759235.350 ± 142183.130 ns/op
StringBuilder improvement ~92%
- duplicates
-
JDK-8276904 Optional.toString() is unnecessarily expensive
- Resolved