4 ms·
Optional is being being converted to a value class as part of Project Valhalla. I reran the benchmarks with the latest Valhalla branch [1] and added a test that
by kasperni 4y ago
Optional is being being converted to a value class as part of Project Valhalla.
I reran the benchmarks with the latest Valhalla branch [1] and added a test that used OptionalLong instead of Optional<Long>.
Without Valhalla
- OptionBenchmark.sumSimple avgt 5 328,110 us/op
- OptionBenchmark.sumNulls avgt 5 570,800 us/op
- OptionBenchmark.sumOptional avgt 5 2223,887 us/op
- OptionBenchmark.sumOptionalLong avgt 5 1201,987 us/op
With Valhalla
- OptionBenchmark.sumSimpleValhalla avgt 5 327,927 us/op
- OptionBenchmark.sumNullsValhalla avgt 5 584,967 us/op
- OptionBenchmark.sumOptionalValhalla avgt 5 572,833 us/op
- OptionBenchmark.sumOptionalLongValhalla avgt 5 326,949 us/op
OptionalLong is now as fast as simple sum. And SumOptional is now as fast as SumNulls. So the overhead of using OptionalLong and Optional<Long> seems to have gone away with Valhalla.
It would be great if boxing could be eliminated as well. But few people writes code like what is being benchmarked (in hot loops) in practice.
[1] https://github.com/openjdk/valhalla https://github.com/openjdk/valhalla
- pestatije 4y agoThose numbers don't support your statement
- kasperni 4y agoCould you tell me exactly what is the issue? sumNullsValhalla and sumOptionalValhalla returns 584,967 us/op and 572,833 us/op respectively sumSimpleValhalla and sumOptionalLongValhalla returns 327,927 us/op and 326,949 us/op respectively
- deleted 4y ago[deleted]
- rkuska 4y agoMaybe the person missed the difference because how your numbers are formatted: - OptionBenchmark.sumOptional avgt 5 2223,887 us/op vs - OptionBenchmark.sumOptionalValhalla avgt 5 572,833 us/op At least to me after the first read it seemed like comparison of two similair 5kish values.
- karmakaze 4y agoI was also confused by 2223,887 us/op Is there a missing comma or is comma being used here as a decimal point?
- kasperni 4y agoRight, it is the output from JMH (Java Microbenchmark Harness) which includes the number of non-warmup-runs before the actual score. The original article included them as well so thought I would just post the numbers in the same format here.
- stonemetal12 4y agosumOptionalLong 1,201,987 us/op | 326,949 us/op Sum Optional long is about 4x faster. 1.2Million microseconds per operation (pre Valhalla) vs 300K microseconds per operation (post Valhalla).
- Manjuuu 4y agoThe important numbers are those related to Optional/OptionalLog, there is a 4x+ improvement after Valhalla.
- misja111 4y agoI was confused at first too, but there's a '5' after each benchmark which doesn't belong to the benchmark speed but which belongs to the benchmark title (?).
- kasperni 4y agoIt it the number of non-warmup-runs for the benchmarking tool (JMH). The original article included them as well so thought I would just post the numbers in the same format here. But I totally understand that they are confusing if you are not in the Java ecosystem.
- kasperni 4y agoRealising that my formatting is difficult to understand if you have not used JMH previously. I've cleaned up the numbers: Pre Valhalla | Valhalla sumSimple 328,110 us/op | 327,927 us/op sumOptionalLong 1201,987 us/op | 326,949 us/op sumNulls 570,800 us/op | 584,967 us/op sumOptional 2223,887 us/op | 572,833 us/op
- re-thc 4y agoWe need this sooner than later!
- Pet_Ant 4y agoWhat are you doing? Our code is slow from terrible design choices and endless redirects, this is leagues below being relevant for us.
- re-thc 4y agoBuilding normal software like everyone else - just with a different mindset. Java doesn't have to be Spring or bloatware. Before the recent generics everyone wrote Golang like Java without boxing and don't complain so why not :).